如何在 Button Click-[Android] 上的同一个列表视图上加载不同的适配器?

How to load Different adapters on same listview on Button Click-[Android]?

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

我试过了

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);
    }

}

}

我收到以下错误:

FATAL EXCEPTION: main Process: com.example.nike.assignmentadapter, PID: 2565 java.lang.NullPointerException: Attempt to get length of null array at com.example.nike.assignmentadapter.food.getCount(food.java:41) at android.widget.ListView.setAdapter(ListView.java:487) at com.example.nike.assignmentadapter.MainActivity.onClick(MainActivity.java:74) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

食物class

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();
    }
}

}

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);
}

}