Android ListView onItemClick

I have a ListView that has data from parsed JSON. So this is my sample code:

ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();

    /**
     * parsing JSON...
     * ...
     * ...
     * ...
     */

ListAdapter adapter = new SimpleAdapter(this, myList, R.layout.s_layout, new String[]{NAME, TEXT}, new int[]{R.id.name, R.id.text});
    setListAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view, int position, long id){


            String inName = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String inText = ((TextView) view.findViewById(R.id.text)).getText().toString();


            Intent intent = new Intent(getApplicationContext(), StaffProfileActivity.class);

            intent.putExtra("staff_name", inName);
            intent.putExtra("staff_desc", inText);
            startActivity(intent);
       }
});

My problem is how can I transfer my data to Intent without using

String inName = ((TextView) view.findViewById(R.id.name)).getText().toString(); 

I mean, how to get my data from my ListView and give it an intent?

+5
source share
2 answers

adapter.get(position)will return you HashMap<String, String>for the selected item. And use the List<T>definition insteadArrayList<T>

+6
source

From the documentation

public abstract void onItemClick (parent element of AdapterView, view view, int position, long id)

, , AdapterView . getItemAtPosition (position), , .

onItemClick :

  HashMap<String, String> elt = myList.getItemAtPosition(position);

inName inText elt.

+3

All Articles