ListView setOnItemClickListener 在自定义列表视图中不起作用 [英] ListView setOnItemClickListener not working in custom list view

查看:15
本文介绍了ListView setOnItemClickListener 在自定义列表视图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,每行有两个文本视图和一个编辑文本,列表视图 setOnItemClickListener() 不起作用.

I have a list view with two text view and one edit text in each row , list view setOnItemClickListener() is not working.

这是我的 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>

请建议我如何解决这个问题.

Please Suggest me How i can fix this problem.

提前致谢

推荐答案

设置这些属性

 android:focusable="false"
 android:focusableInTouchMode="false"

用于 create_list_item xml 文件中的所有 UI 元素.

for your all UI elements in your create_list_item xml file.

同时从 ListView 中删除该属性.

Also remove that properties from ListView.

所以你的 ListView 将是

So your ListView will be

 <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>

这篇关于ListView setOnItemClickListener 在自定义列表视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆