简体   繁体   English

listview onitemclicklistener用于自定义布局

[英]listview onitemclicklistener for a custom layout

I have a listview : 我有一个listview

   <ListView
        android:clickable="true"
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

the elements that go inside it are like this: 里面的元素是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >




    <TextView
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.76"
        android:text="Choose"
        android:clickable="true" />


    <EditText
        android:id="@+id/edittext_share"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.12"
        android:ems="10"
        android:hint="share"
        android:inputType="number" >

        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/edittext_spent"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.11"
        android:ems="10"
        android:hint="spent"
        android:inputType="number" />

</LinearLayout>

I want that when I click on the TextView , something should happen. 我希望当我点击TextView ,会发生一些事情。 I have set the setOnItemClickListener via the getListView() but it does not work. 我通过getListView()设置了setOnItemClickListener但它不起作用。

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"clicked id="+arg2, Toast.LENGTH_SHORT).show();
}

and

        ListView lv = getListView();
    lv.setOnItemClickListener(this);

What do I do to make the textview clicable? 我该怎么做才能使textview变得复杂?

EDIT : my adapter code: 编辑 :我的适配器代码:

    private class PeopleListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    public Cursor cursor;
    public ArrayList<PersonInfo> peopleList;
    public ArrayList<PersonInfo> unsavedPeopleList;

    public PeopleListAdapter(Context context, Cursor cursor) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        this.cursor = cursor;
        peopleList = new ArrayList<PersonInfo>(cursor.getCount());
    }

    public PeopleListAdapter(Context context, Cursor cursor,
            ArrayList<PersonInfo> unsavedList) {
        this(context, cursor);
        this.unsavedPeopleList = unsavedList;
    }

    /**
     * The number of items in the list is determined by the number of
     * speeches in our array.
     * 
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return peopleList.size() + unsavedPeopleList.size();
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficent to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     * 
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {

        return position;
    }


    /**
     * Use the array index as a unique id.
     * 
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid
        // unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is
        // no need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_person_info,
                    null);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.name = (TextView) convertView
                    .findViewById(R.id.textview_name);
            holder.share = (EditText) convertView
                    .findViewById(R.id.edittext_share);
            holder.spent = (EditText) convertView
                    .findViewById(R.id.edittext_spent);
            convertView.setTag(holder);

        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        PersonInfo p = getPersonInfo(position);
        holder.name.setText(p.name);
        holder.name.setTag(p.lookupUri);
        holder.share.setText(p.share + "");
        holder.spent.setText(p.spent + "");

        return convertView;
    }

    PersonInfo getPersonInfo(int position) {
        if (position < peopleList.size()) {
            PersonInfo pInfo = peopleList.get(position);
            if (pInfo == null) {
                pInfo = new PersonInfo();
                cursor.moveToPosition(position);
                pInfo.lookupUri = Uri.parse(cursor.getString(0));
                Cursor c = NewEventActivity.this.getContentResolver()
                        .query(pInfo.lookupUri,
                                new String[] { Contacts._ID,
                                        Contacts.DISPLAY_NAME }, null,
                                null, null);
                pInfo.share = cursor.getInt(1);
                pInfo.spent = cursor.getInt(2);
                try {
                    if (c.moveToFirst()) {
                        pInfo.name = c.getString(1);
                    }
                } finally {
                    cursor.close();
                }
                peopleList.set(position, pInfo);

            }

            return pInfo;
        } else {
            return unsavedPeopleList.get(position - peopleList.size());
        }
    }

You need to set the Click listener Listener to TextView in Adapter in getView() function ...... 您需要在getView()函数中将Click侦听器侦听器设置为适配器中的TextView ......

see this link 看到这个链接

        private class PeopleListAdapter extends BaseAdapter implements
OnClickListener {

     ..............................
      ............................


public View getView(int position, View convertView, ViewGroup parent) {
       
             ......................
           .....................
        holder.name.setText(p.name);
        //holder.name.setTag(p.lookupUri);
        holder.share.setText(p.share + "");
        holder.spent.setText(p.spent + "");

        holder.name.setOnClickListener(this);
       holder.name.setTag(position);



        return convertView;
    }

     @Override
    public void onClick(View v) {
        Log.d("Sample", "Clicked on tag: " + v.getTag());
        ////get PersonInfo using getPersonInfo(position) 
    }

If you are using ListActicvity then there is a default method available called, onListItemClick() . 如果您正在使用ListActicvity,那么有一个名为onListItemClick()的默认方法。 You have to override this method in your ListActivity, 您必须在ListActivity中覆盖此方法,

protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {

        //find which item is clicked using the view object
    };

Set on ClickListener In your getListView() method TextView. 在ClickListener上设置在getListView()方法TextView中。

Thanks. 谢谢。

From what I have understood,this is what you want : You want the TextView inside the ListView to be clickable perform some operation based on that,and you have the items with a custom layout inside the ListView 根据我的理解,这就是你想要的:你希望ListView中的TextView可以点击执行基于它的一些操作,并且你在ListView中有自定义布局的项目

So : setOnItemClickListener will not work because it adds a listener for an entire item inside the ListVIew and not on the elements inside that. 所以:setOnItemClickListener 将无法工作,因为它为ListVIew中的整个项添加了一个侦听器,而不是其中的元素。

To achieve that you can do this : add a tag to the elements inside your ListView and inside the getView method you are being provided with the position by default, using that and using the tag by using getTag() method you can distinguish what was clicked and in the xml you can simpy declare onClick() and provide with its definition 为此,您可以执行此操作:向ListView内部的元素添加标记,并在默认情况下为您提供位置的getView方法内部,使用该标记并使用getTag()方法使用标记可以区分所点击的内容在xml中你可以简单地声明onClick()并提供它的定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM