balaji
balaji

Reputation: 1575

list view onitemselectedlistener error?

I created one simple list view. i have two list view, my 1st list view display only one item that item name is fruit. if i click fruit in list view it will go to 2nd list view. 2nd list view display many number of fruits name. if i select one fruit i want show that fruit name in my 1st list view(i want bind that fruit name in my 1st list view) so, i am using onitemselectedlistener but it's not working. if i click my 2nd list view fruit name nothing happend....please check my source code and help me....

1st list view:

     public class bview extends ListActivity {    

 /** Called when the activity is first created. */    

     @Override     

    public void onCreate(Bundle savedInstanceState) {         
    super.onCreate(savedInstanceState);         

    setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, cat));         getListView().setTextFilterEnabled(true);     }      
    static final String[] cat = new String[] {         "Category", }; 

    protected void onListItemClick(ListView parent, View v, int position,long id) {                        
    Intent intent= new Intent(bview.this,listview.class);             
    startActivity(intent);             
    //intent.putExtra("value", value);                              }} 

2nd list view:

public class listview extends Activity
{    


    @Override    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submain);


        ListView mlistView = (ListView) findViewById(R.id.listview);
        mlistView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                new String[] {"orange", "apple","mango","banana","grapes"}));

 mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){
         Intent in2 =new Intent(listview.this, bview.class);
         startActivity(in2);
     }
public void onNothingSelected(AdapterView<?>arg0){
 }});

}}

Upvotes: 0

Views: 1054

Answers (1)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Your problem is that your not passing what was selected in the second list back to the first list. You need to include what was selected in your intent and then in your first activity you need to check if that was included in the Intent it was started with. So you'd so something like this.

In your second activity:

mlistView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3){
     Intent in2 =new Intent(listview.this, bview.class);
     in2.putExtra("SELECTED_ITEM", (String)arg0.getItemAtPosition(arg2));
     startActivity(in2);
  }
  public void onNothingSelected(AdapterView<?>arg0){
}});

In your first activity you'll need to add this code in your onCreate method:

Intent givenIntent = getIntent();
if(givenIntent.hasExtra("SELECTED_ITEM")){
  String selectedItem = givenIntent.getStringExtra("SELECTED_ITEM");
  //now do what ever you want with the face that you have the item that was selected
}
else{
  //in here you know the activity was started by selecting something from your second
  //activity. so do what you need to do in that case here.
}

Upvotes: 1

Related Questions