lichienmine
lichienmine

Reputation: 63

Adding array value to OnItemClickListener() in Activity

I am using this code :

final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")

            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Intent intent = new Intent(TestActivity.this, TestDetailActivity.class);
            intent.putExtra("id_card", o.get("id_card"));
            intent.putExtra("card_title", o.get("card_title"));
            intent.putExtra("card_number", o.get("card_number"));
            startActivity(intent);
        }
    });

I'm using HashMap<String, String> o = (HashMap<String, String>) to get some values which i will be passing to another activity with intent.

It works when I use ListActivity but I don't know how to implement it in Activity, please help me.

Upvotes: 0

Views: 96

Answers (2)

Because getListView() is the method of ListActivity

Which means when you using

final ListView lv = getListView();

ListView will get initialized with super, while your simple Activity class doesn't contain any ListView.

ListActivity-> ListActivity hosts a ListView object that can be bound to different data sources

To use the same in Activity class then you must use explicit ListView and mapped it with ID

<ListView android:id="@+id/list_view"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

now you can initialized like this

final ListView lv = (ListView) findViewById(R.id.list_view);

Upvotes: 3

Phil
Phil

Reputation: 36289

The call to getListView() only works in ListActivity. You will need to get access to your ListView a different way. Implement this method in your Activity:

public ListView getListView()
{
    return findViewById(android.R.id.list);
}

(or if your list id is something else, use that id instead)

Upvotes: 0

Related Questions