我有一个列表视图,每行有两个文本视图和一个编辑文本,列表视图setOnItemClickListener()不起作用。
这里是我的Java代码。
public class CreateChallan extends Activity {
ListView lstCreate;
String[] strmainItemCode;
String[] strItem;
String[] strQuantity;
Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createchallan);
lstCreate= (ListView) findViewById(R.id.createlist);
lstCreate.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
strmainItemCode= new String[]{"555551","255555","355555","455555","555555"};
strItem =new String[]{"A","B","C","D","F"};
strQuantity =new String[]{"100","200","30","400","500"};
CreateAdapter adapter= new CreateAdapter(this, strmainItemCode, strItem, s trQuantity);
lstCreate.setAdapter(adapter);
lstCreate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position1, long id) {
// TODO Auto-generated method stub
Toast.makeText(context, "Position", Toast.LENGTH_LONG).show();
}
});
}
// Create List Adapter
class CreateAdapter extends ArrayAdapter<String>
{
TextView txtItecode, txtItem;
EditText editQuantity;
String[] strItecode;
String[] strItem;
String[] strQuantity;
Context context;
CreateAdapter(Context context, String[] strItemcode, String[] strItem, String[] strQauntity)
{
super(context,R.layout.create_list_item,R.id.txtItemcode,strItemcode);
this.context= context;
this.strItecode= strItemcode;
this.strItem= strItem;
this.strQuantity= strQauntity;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View row;
row=mInflater.inflate(R.layout.create_list_item, parent,false);
txtItecode= (TextView) row.findViewById(R.id.txtItemcode);
txtItem =(TextView) row.findViewById(R.id.txtItem);
editQuantity = (EditText) row.findViewById(R.id.editcreateQuantity);
txtItecode.setText(strItecode[position]);
txtItem.setText(strItem[position]);
editQuantity.setText(strQuantity[position]);
txtItecode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "click", Toast.LENGTH_LONG).show();
}
});
return row;
}
}
}
此处列出了我的xml代码
<ListView
android:id="@+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true"
android:focusable="false"
android:focusableInTouchMode="false"
>
</ListView>
请建议我如何解决这个问题。
提前致谢
答案 0 :(得分:10)
设置这些属性
android:focusable="false"
android:focusableInTouchMode="false"
显示 create_list_item xml文件中的所有UI元素。
还从ListView中删除该属性。
所以你的ListView将是
<ListView
android:id="@+id/createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:cacheColorHint="#00000000"
android:divider="#adb8c2"
android:dividerHeight="1dp"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
答案 1 :(得分:5)
在我看来,因为您的自定义列表项布局中有Button或ImageButton。
所以有两个解决方案,
将按钮替换为其他元素,并在适配器代码中删除onClick侦听器。 这是解决这个问题的简单方法。
禁用按钮焦点,如下所示
android:focusable="false" android:focusableInTouchMode="false"
请参阅此ListView setOnItemClickListener not working by adding button
玩得开心。 @。@
答案 2 :(得分:0)
这个自定义适配器适合你在这里你可以实现onclick和项目点击监听器....这里我使用自定义监听器,用于传递所选项目的对象..这就是#s;如果您有任何查询评论......一切顺利
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
@Override
public Sample getItem(int position) {
return mlist.get(position);
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.linearlayoutSample);;
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
layout.setBackgroundColor(Color.GREEN);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
convertView.setOnClickListener(new ListenerT(Model m) {
@Override
public void onClick(View v) {
Model m = study;
}
});
return convertView;
}
private class ListenerT implements OnClickListener {
Model study;
public ListenerT(Model nm) {
study = nm;
}
@Override
public void onClick(View v) {
}
}
}
答案 3 :(得分:0)
检查 setOnItemClickListener
中的 Log.i() 总是更好,因为您的事件/代码可能没有发生,可能是因为异常。
因此,如果您的日志打印出来,但您的代码不工作,则很可能存在异常,并且可能没有由于错误处理或 try catch 导致应用程序崩溃。如果存在,还建议将 Log.e 放在 catch 块中。