I have to a make the list view items clickable ie start an activity. I am using the following code. I have no clue on how to proceed.
package org.example.androidsdk.demo;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
String [] mTestArray;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.myArray);
/*
* Assign the name array to that adapter and also choose a simple layout
* for the list items
*/
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
Just Override onListItemClick
in your Activity
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
转到此链接,了解如何处理ListView http://wiresareobsolete.com/2011/08/clickable-zones-in-listview-items/
You need to implement OnItemClickListener
And call getListView().setOnItemClickListener(this)
Finally, do what you want in overrided onItemClick
method.
try below code :-
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
Read below link for more information :-
http://www.vogella.com/tutorials/AndroidListView/article.html
Override onListItemClick
and write your Activity open code in it.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(this,MyClass.class);
startActivity(i);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.