0

嗨,我在列表视图中使用列表视图,我有蔬菜和非蔬菜。所以当我点击蔬菜时,它应该移动到下一个活动并显示项目,当我点击非蔬菜时,它应该到下一个活动。我有为两个列表项编写了 setonclickitemlistener() 但是当我按下 veg 或 non-veg 时,它只会进入 Non-veg 活动。这是代码

public class MainActivity extends Activity {

String[] items={"veg","non-veg"};


protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

     ListView lv= (ListView)findViewById(R.id.listView1);

  ArrayAdapter<Object> adapter1 = new ArrayAdapter<Object (this,android.R.layout.simple_list_item_1,items);

  lv.setAdapter(adapter1);      

    lv.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {

            {
            Toast.makeText(getBaseContext(),"YOU HAVE SELECTED VEG", 4000).show();
        Intent i=new Intent(MainActivity.this,veg.class);
        startActivity(i);
            }
        }
    });




  lv.setOnItemClickListener(new OnItemClickListener() {         

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Toast.makeText(getBaseContext(),"YOU HAVE SELECTED NON-VEG", 4000).show();
            Intent i=new Intent(MainActivity.this,Nonveg.class);
            startActivity(i);


            }
        });

} }

请告诉我如何识别蔬菜和非蔬菜的 id 以及如何编写 setonclicklistener() 方法。

4

3 回答 3

0

你只能有一个 OnItemClickListener... 这将不得不检查视图以确定去哪里。

于 2013-04-05T12:50:56.537 回答
0

此代码完全正常工作:

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        final String[] values = new String[] { "Veg", "Non-veg" };

                // Define a new Adapter
                // First parameter - Context
                // Second parameter - Layout for the row
                // Third parameter - ID of the TextView to which the data is written
                // Forth - the Array of data

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, android.R.id.text1, values);


                // Assign adapter to ListView
                lv.setAdapter(adapter); 

                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        if (arg2 == 0) {
                        Intent intent = new Intent(MainActivity.this, veg.class);
                        startActivity(intent);
                        }
                        if (arg2 ==1) {
                            Intent intent = new Intent(MainActivity.this, Nonveg.class);
                            startActivity(intent);
                        }
                    }
                });
}

arg2 是列表中项目的位置

于 2013-04-05T12:54:44.637 回答
0

单击列表视图项目时,在列表视图中的位置获取项目。检查它是蔬菜还是非蔬菜

public class MainActivity extends Activity {
String[] items={"veg","non-veg"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 lv= (ListView)findViewById(R.id.listView1);
 setContentView(lv);
 ArrayAdapter<Object> adapter1 = new ArrayAdapter<Object> (this,android.R.layout.simple_list_item_1,items);

 lv.setAdapter(adapter1);      

 lv.setOnItemClickListener(new OnItemClickListener() {


    public void onItemClick(AdapterView<?> arg0, View v, int arg2,
            long arg3) {

        {
             String itemselected =(String) (lv.getItemAtPosition(arg2));
             if(itemselected.equals("veg")){

             Toast.makeText(getBaseContext(),"YOU HAVE SELECTED VEG", 4000).show();
             Intent i=new Intent(MainActivity.this,veg.class);
             startActivity(i); 
          }
            else if(itemselected .equals("non-veg"))
            {
                Toast.makeText(getBaseContext(),"YOU HAVE SELECTED NON VEG",4000).show();
                     Intent i=new Intent(MainActivity.this,nonveg.class);
                      startActivity(i); 
            }

        }
    }


});

}
});
于 2013-04-05T12:57:40.717 回答