The below is my testing code to create the list view, the list view display successfully, however, there is error in click event. I would like to create an intent to send a hardcode message to an new activity. However, it show error for the line
Intent intent = new Intent(context, SendMessage.class);
So , the problem is , what should I provide for this class?
Also , instead of hard code the output message, how to capture the data in list view row and pass to the new activity? e.g. BBB,AAA,R.drawable.tab1_hdpi
for the first row.
Thanks.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
members.add(new ListEntry("BcxvBB","AcxvAA"));
members.add(new ListEntry("BvBB","AcxsvAA"));
members.add(new ListEntry("BcxvBB","AcxsvzAA"));
members.add(new ListEntry("Bcxadv","AcsxvAA"));
members.add(new ListEntry("BcxcxB","AcxsvAA"));
ListView lv = (ListView)findViewById(R.id.listView1);
Log.i("testTag","before start adapter");
StringArrayAdapter ad = new StringArrayAdapter (members,this);
Log.i("testTag","after start adapter");
Log.i("testTag","set adapter");
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(context, SendMessage.class);
String message = "abc";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
I can not see where do you declare
context
. For the purpose of the intent creation you can useMainActivity.this
To retrieve the object upon you have clicked you can use the
AdapterView
:Error is coming in your code from this statement as you said
This is due to you are providing context of OnItemClickListener anonymous class into the Intent constructor but according to constructor of Intent
You have to provide context of you activity in which you are using intent that is the MainActivity class context. so your statement which is giving error will be converted to
Also for sending your message from this MainActivity to SendMessage class please see below code
Please let me know if this helps you
EDIT:- If you are finding some issue to get the value of list do one thing declear your array list
globally i.e. before oncreate and change your listener as below
So your whole code will look as
Where getMessage() will be a getter method specified in your ListEntry class which you are using to get message which was previously set.
ListView has the Item click listener callback. You should set the
onItemClickListener
in theListView
. Callback containsAdapterView
andposition
as parameter. Which can give you theListEntry
.According to my test,
implements OnItemClickListener -> works.
setOnItemClickListener -> works.
ListView is clickable by default (API 19)
The important thing is, "click" only works to TextView (if you choose simple_list_item_1.xml as item). That means if you provide text data for the ListView, "click" works when you click on text area. Click on empty area does not trigger "click event".
First, the class must implements the click listenener :
Then set a listener to the ListView
And finally, create the clic method: