【问题标题】:How can I get click events for items within Listview in CustomAdapter and in the activity where Listview is declared如何在 CustomAdapter 中的 Listview 和声明 Listview 的活动中获取项目的点击事件
【发布时间】:2014-02-03 05:36:27
【问题描述】:

在我的应用程序中,我使用的是具有 customAdapter 的 ListView。在 Listview 中,项目是 TextView 和 ImageButton。在 ImageButton 的 OnClick 事件中,我删除了我在 CustomAdapter 中定义的列表视图中的一个条目。在 TextView 的 OnClick 事件中,我使用 Intents 开始了一个新活动。问题是没有在活动中调用 List.OnItemClick 事件,我无法在 CustomAdapter 中启动新活动。如何获得 TextView 的 onClick 事件的解决方案。

这是我的代码的一部分:

在 Activity A 中(其中 ListView 出现在其布局中)

public class RecipientsActivity extends Activity {
....

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
        Person new_person = (Person) data.getSerializableExtra("Person");
        Log.e("Before adding", "size = " + recipientArray.size());
        recipientArray.add(new_person);
        Log.e("Add new person", "size = " + recipientArray.size());
        this.m_adapter = new CustomListAdapter(this,
                R.layout.recipients_list, recipientArray);
        list = (ListView) findViewById(R.id.rec_list);
        list.setAdapter(m_adapter);
//This event is not being called and I'm not sure if Textview's click event comes here          
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {

                Log.e("Get item clicked", "position may be " + pos);
                Intent rec_Intent = new Intent(RecipientsActivity.this,
                        RecipientAddressActivity.class);
                rec_Intent.putExtra("Recipient", recipientArray.get(pos));
                startActivity(rec_Intent);

            }

        });

    }
}

这是我的适配器代码:

public class CustomListAdapter extends ArrayAdapter<Person> {

static class ViewHolder {
    public TextView txtRecName;
    public ImageButton btn_rec_del;
}

private ArrayList<Person> recipientArray;

public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<Person> objects) {
    super(context, textViewResourceId, objects);
    this.recipientArray = objects;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    final int index = position;
    View row = view;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        row = vi.inflate(R.layout.recipients_list, null);
        holder = new ViewHolder();
        holder.txtRecName = (TextView) row.findViewById(R.id.rec_name);
        holder.btn_rec_del = (ImageButton) row
                .findViewById(R.id.btn_rec_delete);
        holder.btn_rec_del.setFocusable(false);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    Person p = recipientArray.get(position);
    if (p != null) {

        if (holder.txtRecName != null) {
            holder.txtRecName.setText(p.getName());
        }
    }

    holder.btn_rec_del.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {               
            //Log.e("Hey","position "+index);
            recipientArray.remove(index);
            notifyDataSetChanged();     

        }
    });

    /*holder.txtRecName.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Hola", "hola");
            Intent rec_Intent = new Intent(getContext(), RecipientAddressActivity.class);
            rec_Intent.putExtra("Current_Recipient", recipientArray.get(index));
            startActivity(rec_Intent);

        }
    });*/
    return row;

}

}

这里是activity的部分布局文件:

<ListView
        android:id="@+id/rec_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingTop="20dp" />

ListView的布局文件:

<?xml version="1.0" encoding="utf-8"?>

    <TextView android:id="@+id/rec_name" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/fill_rece"
        android:focusable="false" android:textColor="#FFFFFF" android:ems="15"
        android:padding="10dp" />

    <ImageButton android:id="@+id/btn_rec_delete"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:contentDescription="@string/content" android:src="@drawable/ipad_postcare_landscape_from" />
</TableRow>

【问题讨论】:

  • 在你的适配器类中使用holder.txtRecName.setOnClickListener()
  • 为什么你在 onActivityResult 中调用 setOnitemclicklistner 可能是这个问题。将其放在 onActivityResult 之外并尝试...
  • 当您在适配器中应用 onClickListener 时,您将获得 listview 项单击侦听器,因此请尝试在单击侦听器上实现其中一种方式 1。在适配器中声明单击侦听器或仅实现列表项单击侦听器...跨度>
  • @Shayanpourvatan 我试过了。如果你看到它的注释代码。holder.TxtREcName.setonclicklistener 的问题是我无法从那里开始一个新的活动。 StartActivity 无法实现。
  • @user2688158 你需要上下文因为 startActivity 是活动类的一个方法

标签: android android-layout android-listview onclick


【解决方案1】:

当您单击列表项时 ImageButton 获得焦点。所以在xml中添加android:focusable="false"ImageButton

另外如果你需要使用注释代码

 Context context;
 public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<Person> objects) {
    super(context, textViewResourceId, objects);
    this.recipientArray = objects;
    this.context = context;  // initialize
}

holder.txtRecName.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Hola", "hola");
            Intent rec_Intent = new Intent(context, RecipientAddressActivity.class);
            rec_Intent.putExtra("Current_Recipient", recipientArray.get(index));
            context.startActivity(rec_Intent); 

        }
    });

【讨论】:

  • 谢谢 :) 你太棒了。
【解决方案2】:

在 getview() 中,在函数末尾使用以下代码。

row.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {               
            //Log.e("Hey","position "+index);
            Intent intent=new Intent(CurrentActivity.this,TargetActivity.class);
    intent.putExtra("personData", (Serializable) recipientArray.get(position));
context.startActivity(intent);


        }
    });

并在您的 Person 类中应用 Serializable 接口。

享受吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 2021-04-05
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多