0

Hi I am using list view in my list view i have veg and non-veg.so when i click on veg it should move to next activity and display the items and when i click on Non-veg it should to next activity.I have written setonclickitemlistener() for both list items but when i press veg or non-veg it is going to Non-veg activity only.Here is the code

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


            }
        });

} }

please tell me how to identify id for veg and non veg and how to write setonclicklistener() method.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
SANJEEV REDDY
  • 37
  • 1
  • 4
  • 17

3 Answers3

0

You can only have one OnItemClickListener... That is going to have to check the view to determine where to go.

snowCrabs
  • 785
  • 5
  • 11
  • Then what about other item if i give only one onitemclick – SANJEEV REDDY Apr 05 '13 at 12:56
  • The list view can only have one registered OnClickListener. This is registered for whenever ANY item is clicked. You have to determine from getting the information from the adapter what view you have and determine how to proceed from there. – snowCrabs Apr 05 '13 at 12:59
0

this code is fully working:

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 is position of item in list

Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

On click of listview item get the item at the position in listview.. Check if its veg or non veg

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

        }
    }


});

}
});
Raghunandan
  • 132,755
  • 26
  • 225
  • 256