0

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.

2
  • plese give ans about how you get hash map which u retrive from previous activity Commented Dec 24, 2012 at 6:28
  • Can you be clear on what you want exactly? Commented Dec 24, 2012 at 6:29

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer.its work. but i want to adding array data to OnItemClickListener(). i use Hashmap in ListActivity, how do i implement it when use Activity?
already mentioned in answer(take explicit ListView), you can refer this vogella.com/articles/AndroidListView/article.html
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.