我正在使用以下代码,我想在ListView项目单击后显示一些文本。但是Toast没有出现在点击上。我使用ArrayList来存储List项目
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点击
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;
}
}
答案 0 :(得分:2)
添加此
android:descendantFocusability="blocksDescendants"
到xml
中的根元素当您单击列表项时,该复选框将成为焦点。所以将上面的内容添加到xml中的RelativeLayout
并重新运行应用程序。
编辑:
仔细观察,您已经拥有android:focusableInTouchMode="false"
,因此上述内容可能无效。
编辑2:
我尝试使用具有相同布局的字符串
public class MainActivity extends Activity
{
LinearLayout linear_top_right,linear_top_left,linear_top,linear_All,linear_list;
Button btn_delete,btn_add;
CheckBox chk_select_all, chk_src_to_dest;
ListView listView;
MyCustomAdapter dataAdapter;
String allWordList[]= {"A","B","C"};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (chk_select_all != null)
chk_select_all.setChecked(false);
}
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.setWidth(15);
btn_delete.setHeight(15);
btn_add = new Button(this);
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.m,
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
MainActivity.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
//Here I have used listview click
chk_src_to_dest
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
}
});
chk_select_all
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean checked) {
}
});
btn_delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
dataAdapter = new MyCustomAdapter(this, R.layout.m,
allWordList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,"BETA VERSION",
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<String> {
private String[] list;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] allWordList) {
super(context, textViewResourceId, allWordList);
this.list = allWordList;
}
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.m, null);
holder = new ViewHolder();
holder.checkBx = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.txt_result = (TextView) convertView
.findViewById(R.id.txt_result);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
m.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="checkbox" />
<TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text=" " />
</RelativeLayout>
对齐
答案 1 :(得分:0)
将吐司的上下文更改为:
Toast.makeText(getApplicationContext(),"BETA VERSION",
Toast.LENGTH_LONG).show();
答案 2 :(得分:0)
由于ListView项目中可用的click listerners(在这种情况下为复选框),会发生这种情况。
您必须将以下内容添加到您的复选框(它们将正常工作:)),
android:focusable="false"
android:focusableInTouchMode="false"
类似的解决方案here。