我正在尝试制作一个列表视图,您可以在其中单击列表中的各个项目以显示删除对话框。但是由于某种原因,当我单击列表时,我没有得到听众的任何反应。这是主要活动的代码
public class LifeRpgMain extends Activity {
ArrayList<String> List_Data=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_rpg_main);
if(savedInstanceState!=null){
System.out.println("this shouldnt happen");
ArrayList<String> inp = (ArrayList<String>) savedInstanceState.get("positive");
System.out.println("game made");
List_Data.addAll(inp);
}
else{
List_Data.add("run a mile");
List_Data.add("do homework");
List_Data.add("make this stupid app");
List_Data.add("make money");
}
ListAdapter adapter = new ListAdapter(this,
R.layout.list_item, List_Data);
ListView gameList = (ListView) findViewById(R.id.main_list_view);
gameList.setAdapter(adapter);
//this should allow us to choose individual list items for deletion
gameList.setChoiceMode(1);
gameList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> paren, View view,
int position, long id) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
DeleteButtonFragment del = new DeleteButtonFragment(position, List_Data);
del.show(fm, "delete button");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.life_rpg_main, menu);
return true;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList("positive", List_Data);
}
public void listAdd(View v){
FragmentManager fm = getFragmentManager();
AddButtonFragment add = new AddButtonFragment(findViewById(R.id.activityname),List_Data);
add.show(fm, "teampicker");
}
}
这是列表适配器的代码
public class ListAdapter extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
ArrayList<String> data = null;
public ListAdapter(Context context, int layoutResourceId, ArrayList<String> List_Data) {
super(context, layoutResourceId, List_Data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = List_Data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.name = (TextView)row.findViewById(R.id.ItemName);
row.setTag(holder);
}
else
{
holder = (ItemHolder)row.getTag();
}
String s = data.get(position);
holder.name.setText(s);
return row;
}
static class ItemHolder{
TextView name;
}
}