How to start a new activity form ListView and give it multiple parameters

前端 未结 3 724
滥情空心
滥情空心 2021-01-16 15:25

Below is my code which displays data in listview which is parses from json.
I want to start new activity when the user clicks on any item in the list.

I followe

相关标签:
3条回答
  • 2021-01-16 16:21

    You start an activty with startActivity(intent);

    You set the OnClickListner on your ListView which is not included in your code so Im implying:

    OnItemClickListener listener = new OnItemClickListener (){
    
      @Override
      onItemClick(AdapterView<?> parent, View view, int position, long id){
          String name = ((TextView) view.findViewById(R.id.txtText)).getText();
          Intent intent = new Intent(context,WhatEverYouWant.class);
          intent.putExtra("name",name);
          startActivity(intent);
      }
    
    }
    
    ListView listView = getView().findViewById(R.id.listview);
    listView.setOnItemClickListener (listener);
    
    0 讨论(0)
  • 2021-01-16 16:22

    I thing you shold put objJson.getString(NAME); after onItemClick...... to take the clicked item string name not other item

    0 讨论(0)
  • 2021-01-16 16:30

    create array list like this

        public ArrayList<String> Id = new ArrayList<String>();
    public  ArrayList<String> Name = new ArrayList<String>();
       public  ArrayList<String> Gender= new ArrayList<String>();
    
    
         for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject objJson = jsonArray.getJSONObject(i);
    
                    // here you can get id,name,city...
    
              Id.add(objJson.getInt("id"));
    
             Name.add(objJson.getString("name")); 
             Gender.add(objJson.getString("Gender"));
    
    
    
    
     //You need to use this code in the class where you have the view , 
    
    // list item click 
         List_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            // TODO Auto-generated method stub
    
    
    
            Intent i = new Intent(this, abc.class); 
    // here arg2 is argument of onitemclick method
    // this will pick the same item from array list that is clicked on list view 
                    i.putExtra("key_name" , Id.get(arg2));
                    i.putExtra("key_name" , Name.get(arg2));
                    i.putExtra("key_name" , Gender.get(arg2));
    
                    startActivity(i);       
    
    
                    }
    
    
    
    
                    });
    

    can see this also

    http://www.ezzylearning.com/tutorial.aspx?tid=1351248

    and

    http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php

    0 讨论(0)
提交回复
热议问题