我有以下问题。我构建了一个listView,然后使用自定义laytout item_event显示某些数据。这一切都有效。现在我想让app每次点击listView中的一个元素时都允许我移动到新的活动。我的MainMenu扩展了ListActivity,因此我可以使用setListAdapter。我现在如何使用setOnItemClickListener?我试图创建ListView对象,然后引用它,但我无法访问它,因为它的android:id =“@ android:id / list”/>。怎么做?
400
提前谢谢你, 约翰
以下是错误日志:
package com.mysampleapp;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.app.ListActivity;
import com.userpreferencesom.AmazonClientManager;
import com.userpreferencesom.DynamoDBManager;
import java.util.ArrayList;
public class MainMenu extends ListActivity {
//ListView eventsList;
Button newEvent;
Event event= new Event();
//ArrayList<Event> eventList = new ArrayList();
public static AmazonClientManager clientManager = null;
private ArrayAdapter<String> arrayAdapter = null;
private ArrayList<String> labels = null; // list of names of events manly for test)
private ArrayList<Event> items = null; //stores events
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
clientManager = new AmazonClientManager(this); //CIPEK
new GetEventList().execute(); //calling GetEventList from included class below
newEvent = (Button) findViewById(R.id.new_event_button);
newEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenu.this, EventCreationActivity.class);
startActivity(intent);
}
});
}
private void setupActivity() {
Log.v("MOJEMOJEMOJE", event.getName());
}
private class GetEvent extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... voids) {
event = DynamoDBManager.getEvent(""); //in bracket ID number
return null;
}
protected void onPostExecute(Void result) {
setupActivity();
}
}
private class GetEventList extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... inputs) {
labels = new ArrayList<String>();
items = DynamoDBManager.getEventList();
for (Event up : items) {
labels.add(up.getName());
}
return null;
}
protected void onPostExecute(Void result) {
Log.v("Events:", labels.toString());
updateDisplay();
}
}
public void updateDisplay(){
//Using Event Adapter to display data
EventAdapter adapter = new EventAdapter(this, R.layout.item_event, items);
setListAdapter(adapter);
setOnItemClickListener(new AdapterView.OnItemClickListener() { //HERE IT CANNOT BUILD
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainMenu.this, EventViewActivity.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:1)
当您点击Override
onListItemClick
ListView
处理事件
public class MainMenu extends ListActivity
...
...
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "click at " + position,Toast.LENGTH_LONG).show();
}
}