Android ListView with onClick items

前端 未结 9 1551
梦如初夏
梦如初夏 2020-11-27 03:33

I\'m a new programmer and new in Android. I\'m using this example http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ and it works great.

相关标签:
9条回答
  • 2020-11-27 04:05

    well in your onitemClick you will send the selected value like deal , and send it in your intent when opening new activity and in your new activity get the sent data and related to selected item will display your data

    to get the name from the list

    String item = yourData.get(position).getName(); 
    

    to set data in intent

    intent.putExtra("Key", item);
    

    to get the data in second activity

    getIntent().getExtras().getString("Key")
    
    0 讨论(0)
  • 2020-11-27 04:10
    listview.setOnItemClickListener(new OnItemClickListener(){
    
        @Override
        public void onItemClick(AdapterView<?>adapter,View v, int position){
        Intent intent;
        switch(position){
          case 0:
            intent = new Intent(Activity.this,firstActivity.class);
            break;
          case 1:
            intent = new Intent(Activity.this,secondActivity.class);
            break;
         case 2:
            intent = new Intent(Activity.this,thirdActivity.class);
            break;
        //add more if you have more items in listview
       //0 is the first item 1 second and so on...
        }
        startActivity(intent);
      }
    
    });
    
    0 讨论(0)
  • 2020-11-27 04:14

    In your activity, where you defined your listview

    you write

    listview.setOnItemClickListener(new OnItemClickListener(){   
        @Override
        public void onItemClick(AdapterView<?>adapter,View v, int position){
            ItemClicked item = adapter.getItemAtPosition(position);
    
            Intent intent = new Intent(Activity.this,destinationActivity.class);
            //based on item add info to intent
            startActivity(intent);
        }
    });
    

    in your adapter's getItem you write

    public ItemClicked getItem(int position){
        return items.get(position);
    }
    
    0 讨论(0)
提交回复
热议问题