Reputation: 55
Hello everyone, I have a little problem regrading to list activity. I want to create a simple list of ( 1 , 2 , 3 , 4 , 5 ) and when I clicked on them a Toast will pop up and says clicked. But the application is not running. When I remove list (extends activity rather then ListActivity ) . The app just simply runs and shows a list. I want to apply OnlistItemClick. Hope you help. Here is xml and java code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.annotationap.MainActivity$PlaceholderFragment" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="256dp" >
</ListView>
</LinearLayout>
java code
public class MainActivity extends ListActivity {
private String[] array = {"1", "2" ,"3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1;
listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> aa = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, array );
listView1.setAdapter(aa);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(this,"Clicked" , Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 97
Reputation: 485
You are not setting a listener for listView1. In onCreate you want to set something like this:
listView1.setOnItemClickListener(listener);
However are a few ways you can do this:
I always do the following:
protected void onCreate(Bundle savedInstanceState) {
//Standard onCreate stuff
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//this will run on click.
}
});
How can i attach an listener to listview?
Info on listeners: http://martin.cubeactive.com/android-onclicklitener-tutorial/
Upvotes: 0
Reputation: 30814
It sounds like you want to implement an AdapterView.OnItemClickListener
in a normal Activity
. In that case, here's a quick example:
public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(this);
list.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Long clicked!", Toast.LENGTH_SHORT).show();
return true;
}
}
Upvotes: 0
Reputation: 1206
I think its not a big problem, you should search first before posting, just change your id of listView in XML file
android:id="@android:id/list"
if you're going to use ListActivity then you must have to care about the listView's ID, you can change it but then you'll have to use simple Activity else than ListActivity.
Upvotes: 1