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

时间:2015-03-13 16:19:02

标签: button android-listview onclick adapter baseadapter

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

我试过这个

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:尝试获取null数组的长度               在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)               at java.lang.reflect.Method.invoke(Native Method)               在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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

}