我正在使用以下代码,我想在 ListView Item 单击后显示一些文本。但是 Toast 没有出现在点击。我使用 ArrayList 来存储列表项
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
linear_top_right = new LinearLayout(this);
linear_top_right.setOrientation(LinearLayout.HORIZONTAL);
linear_top_left = new LinearLayout(this);
linear_top_left.setOrientation(LinearLayout.HORIZONTAL);
linear_top = new LinearLayout(this);
linear_top.setOrientation(LinearLayout.HORIZONTAL);
linear_list = new LinearLayout(this);
linear_list.setOrientation(LinearLayout.VERTICAL);
linear_All = new LinearLayout(this);
linear_All.setOrientation(LinearLayout.VERTICAL);
btn_delete = new Button(this);
btn_delete.setBackgroundResource(R.drawable.select_minus);
btn_delete.setWidth(15);
btn_delete.setHeight(15);
btn_add = new Button(this);
btn_add.setBackgroundResource(R.drawable.select_add);
btn_add.setWidth(15);
btn_add.setHeight(15);
linear_top_right.addView(btn_delete);
linear_top_right.addView(btn_add);
chk_select_all = new CheckBox(this);
chk_src_to_dest = new CheckBox(this);
chk_select_all.setText("All");
chk_src_to_dest.setText("SRC to Dest");
linear_top_left.addView(chk_select_all);
linear_top_left.addView(chk_src_to_dest);
listView = new ListView(this);
linear_list.addView(listView);
dataAdapter = new MyCustomAdapter(this, R.layout.row_create_list,
allWordList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(dataAdapter);
linear_top_right.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linear_top_right.setGravity(Gravity.RIGHT);
linear_top_left.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linear_top.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linear_list.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
RelativeLayout relative1 = new RelativeLayout(this);
relative1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linear_top.addView(linear_top_left);
linear_top.addView(linear_top_right);
linear_All.addView(linear_top);
linear_All.addView(linear_list);
relative1.addView(linear_All);
setContentView(relative1);
final Thread thread = new Thread(){
@Override
public void run() {
try {
Thread.sleep(5000); // As I am using LENGTH_LONG in Toast
PracticeTest.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
//这里我使用了listview click
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(PracticeTest.this,"BETA VERSION",
Toast.LENGTH_LONG).show();
//thread.start();
WordList list = (WordList) parent.getItemAtPosition(position);
String listId = list.ListId;
String listName = list.ListName;
Intent intent = new Intent(PracticeTest.this,
TestListActivity.class);
intent.putExtra(Constant.LIST_ID, listId);
intent.putExtra(Constant.LIST_NAME, listName);
PracticeTest.this.startActivity(intent);
}
});
chk_src_to_dest
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
FromSrcToDes = checked;
}
});
chk_select_all
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
for (WordList list : allWordList) {
list.Ischecked = checked;
}
dataAdapter.notifyDataSetChanged();
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int itemCount = allWordList.size();
for (int i = itemCount - 1; i >= 0; i--) // for(WordList list :
// allWordList)
{
WordList list = allWordList.get(i);
if (list.Ischecked == true) {
Util.deleteListId(PracticeTest.this, list.ListId);
}
}
loadAllLists(); // dataAdapter.notifyDataSetChanged(); //
chk_select_all.setChecked(false);
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PracticeTest.this,
AddWordsToList.class);
intent.putExtra(Constant.LIST_ID, "");
PracticeTest.this.startActivity(intent);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (chk_select_all != null)
chk_select_all.setChecked(false);
loadAllLists();
}
private void loadAllLists() {
allWordList.clear();
String[] listIds = Util.getListIds(this);
for (int i = 0; i < listIds.length; i++) {
String listid = listIds[i];
if (!"".equals(listid)) {
WordList list = null;
list = Util.getListData(listid, this);
if (list != null)
allWordList.add(list);
}
}
dataAdapter = new MyCustomAdapter(this, R.layout.row_create_list,
allWordList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
// give test
WordList list = (WordList) parent.getItemAtPosition(position);
String listId = list.ListId;
String listName = list.ListName;
Intent intent = new Intent(PracticeTest.this,
TestListActivity.class);
intent.putExtra(Constant.LIST_ID, listId);
intent.putExtra(Constant.LIST_NAME, listName);
PracticeTest.this.startActivity(intent);
}
});
}
private class MyCustomAdapter extends ArrayAdapter<WordList> {
private ArrayList<WordList> list;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<WordList> list) {
super(context, textViewResourceId, list);
this.list = list;
}
private class ViewHolder {
CheckBox checkBx;
TextView txt_result;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_create_list, null);
holder = new ViewHolder();
holder.checkBx = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.txt_result = (TextView) convertView
.findViewById(R.id.txt_result);
holder.checkBx
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
list.get(position).Ischecked = checked;
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBx.setText(list.get(position).ListName);
holder.checkBx.setChecked((list.get(position).Ischecked));
if (list.get(position).TotalWordsAttampted > 0) {
holder.txt_result.setText("Last attempt "
+ list.get(position).RightAnswers + "/"
+ list.get(position).TotalWordsAttampted);
}
return convertView;
}
}