【问题标题】:How to load Different adapters on same listview on Button Click-[Android]?如何在 Button Click-[Android] 上的相同列表视图上加载不同的适配器?
【发布时间】:2015-03-13 16:19:01
【问题描述】:

我的要求是在同一个列表视图上加载不同的适配器,使用按钮在适配器之间切换。

我试过了

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

ImageButton fo,fl,ci,ani;
ListView l;
String flo[],foo[],cit[],an[];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  String flo[] = {"Rose","Lily","SunFlower","Lotus","Tulips"};
    String foo[] = {"Maggie","Manchurian","Pizza","Burger","French Fries"};
    String cit[] = {"New York","Los Angeles","Las Vegas","Texas","Manhattan"};
    String an[] = {"Lion","Tiger","Penguins","Panda","Elephant"};

    fo = (ImageButton)findViewById(R.id.imageButton2);
    fl = (ImageButton)findViewById(R.id.imageButton);
    ci = (ImageButton)findViewById(R.id.imageButton3);
    ani = (ImageButton)findViewById(R.id.imageButton4);
    l =(ListView)findViewById(R.id.listView);


    fo.setOnClickListener(this);
    fl.setOnClickListener(this);
    ci.setOnClickListener(this);
    ani.setOnClickListener(this);



}

@Override
public void onClick(View v) {

    if (v.getId()==R.id.imageButton2)
    {
        food f = new food(this,foo);
        l.setAdapter(f);
    }

}

}

我收到以下错误:

致命异常:主要 进程:com.example.nike.assignmentadapter,PID:2565 java.lang.NullPointerException:尝试获取空数组的长度 在 com.example.nike.assignmentadapter.food.getCount(food.java:41) 在 android.widget.ListView.setAdapter(ListView.java:487) 在 com.example.nike.assignmentadapter.MainActivity.onClick(MainActivity.java:74) 在 android.view.View.performClick(View.java:4756) 在 android.view.View$PerformClick.run(View.java:19749) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5221) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

食品类

public class food extends BaseAdapter implements View.OnClickListener {

String s[];
Context c;
Button li;
Button sh ;
int a[] = {R.drawable.fl1,R.drawable.fl2,R.drawable.fl3,R.drawable.fl4,R.drawable.fl5};

public food(Context context, String[] xperia) {

    s = xperia;
    c = context;

}



@Override
public int getCount() {
    return s.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return s.length;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater ln = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = ln.inflate(R.layout.food, null);
    TextView t= (TextView)v.findViewById(R.id.textView4);
    ImageView i = (ImageView)v.findViewById(R.id.imageView4);
    li = (Button)v.findViewById(R.id.like4);
     sh = (Button)v.findViewById(R.id.share4);



    String s1=s[position];
    t.setText(s1);
    i.setImageResource(a[position]);

    li.setOnClickListener(this);
    sh.setOnClickListener(this);




    return v;
}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.like4)
    {

        AlertDialog.Builder builder= new AlertDialog.Builder(c);
        builder.setTitle("Thanks Note");
        builder.setMessage("Thank you for Liking ");
        builder.setIcon(R.drawable.foo_t);
        builder.setPositiveButton("To Share", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                Uri webpage = Uri.parse("http://www.sonymobile.com");
                Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
                c.startActivity(webIntent);
            }
        });
        builder.show();

               }
    else
    {
        AlertDialog.Builder builder= new AlertDialog.Builder(c);
        builder.setTitle("Thanks Note");
        builder.setMessage("Thank you for Sharing on Your Facebook");
        builder.setIcon(R.drawable.foo_t);
        builder.setPositiveButton("To Facebook", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                Uri webpage = Uri.parse("https://www.facebook.com");
                Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
                c.startActivity(webIntent);
            }
        });
        builder.show();
    }
}

}

【问题讨论】:

    标签: button android-listview onclick adapter baseadapter


    【解决方案1】:
    public class MainActivity extends ActionBarActivity implements OnClickListener {
    
    
        ImageButton fo,fl,ci,ani;
        ListView l;
    
        String flo[],foo[],cit[],an[];  
        // these are your global variables available to access 
        // in any part of the coding.  Currently they have no 
        // data assigned to them in other words they are null
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // here we have assigned data to the global variables declared
            // outside of the OnCreate function and available to the onClick 
            // call.
            // Using String flo[] = {John, Doe} means you are creating
            // new local versions of the String arrays that are not available
            // globally. Instead of assigning the data to the variables
            // available globally, and to the onClick call, the data is given  
            // to the local variables only accessible in the OnCreate function 
            // function and available until OnCreate finishes.
    
            flo = new String[]{"Rose","Lily","SunFlower","Lotus","Tulips"};
            foo = new String[]{"Maggie","Manchurian","Pizza","Burger","French Fries"};
            cit = new String[]{"New York","Los Angeles","Las Vegas","Texas","Manhattan"};
            an = new String[]{"Lion","Tiger","Penguins","Panda","Elephant"};
    
            fo = (ImageButton)findViewById(R.id.imageButton1);
            l =(ListView)findViewById(R.id.listView1);
    
    
            fo.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
    
            if (v.getId()==R.id.imageButton1)
            {
                System.out.println("foo: " + foo);
                food f = new food(this,foo);
                l.setAdapter(f);
            }
    
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    }

    【讨论】:

    • food.java 类发布
    • 最终我认为食物类中的字符串数组没有正确填充。
    • 适配器工作正常。唯一的问题是当我尝试使用按钮调用它时它没有被调用。如果我定义食物 f = new food(this,foo); l.setAdapter(f);在 OnCreate 中代码有效但我的要求是在按钮单击时加载适配器,因此在 OnClick 下添加上面显示的行时会出错,我已经添加了
    • 继续错误输出时它会在调用 getcount 时崩溃,因为数组为空。所以在某些时候字符串数组 s 正在丢失它的数据。希望有更多经验的人能尽快了解一下
    • 这将是同样的错误,但您应该有一些额外的行来发布 xperia 和 s 的值。
    猜你喜欢
    • 2014-02-14
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多