Android 如何获取CustomAdapter中Listview中的项目以及声明Listview的活动中的项目的单击事件

Android 如何获取CustomAdapter中Listview中的项目以及声明Listview的活动中的项目的单击事件,android,android-layout,android-listview,onclick,Android,Android Layout,Android Listview,Onclick,在我的应用程序中,我使用的是一个具有customAdapter的ListView。在Listview中,项目是一个TextView和一个ImageButton。在ImageButton的OnClick事件中,我删除了在CustomAdapter中定义的listview中的一个条目。在TextView的OnClick事件中,我使用意图启动了一个新的活动。问题是活动中未调用List.OnItemClick事件,并且我无法在CustomAdapter中启动新活动。如何获得TextView的onClic

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

以下是我的部分代码:

在活动A中(其中ListView显示在其布局中)

公共类Recipients活动扩展了活动{
....
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==REC\u INFO&&resultCode==RESULT\u OK&&data!=null){
Person new_Person=(Person)data.getSerializableExtra(“Person”);
Log.e(“添加前”,“size=“+recipientArray.size());
recipientArray.add(新用户);
Log.e(“添加新人”、“大小=“+recipientArray.size());
this.m_adapter=新的CustomListAdapter(此,
R.layout.recipients\u列表,recipientArray);
列表=(ListView)findViewById(R.id.rec_列表);
列表.设置适配器(m_适配器);
//未调用此事件,我不确定Textview的单击事件是否出现在此处
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、,
内部位置,长arg3){
Log.e(“点击获取项目”,“位置可以是”+pos);
意向记录意向=新意向(RecipientsActivity.this,
RecipientAddressActivity.class);
rec_Intent.putExtra(“接收者”,recipientArray.get(pos));
startActivity(记录意图);
}
});
}
}
以下是我的适配器代码:

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;

}

}
公共类CustomListAdapter扩展了ArrayAdapter{
静态类视窗夹{
公共文本视图txtRecName;
公共图像按钮btn_rec_del;
}
私有ArrayList recipientArray;
公共CustomListAdapter(上下文,int textViewResourceId,
ArrayList对象){
超级(上下文、textViewResourceId、对象);
this.recipientArray=对象;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
最终int指数=位置;
视图行=视图;
ViewHolder=null;
if(行==null){
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(
上下文。布局(充气机和服务);
行=vi.充气(R.layout.recipients\u list,空);
holder=新的ViewHolder();
holder.txtRecName=(TextView)row.findViewById(R.id.rec_name);
holder.btn_rec_del=(ImageButton)行
.findviewbyd(R.id.btn\u rec\u delete);
holder.btn\u rec\u del.setFocusable(假);
row.setTag(支架);
}否则{
holder=(ViewHolder)row.getTag();
}
Person p=recipientArray.get(位置);
如果(p!=null){
if(holder.txtRecName!=null){
holder.txtRecName.setText(p.getName());
}
}
holder.btn_rec_del.setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图v){
//Log.e(“嘿”,“位置”+索引);
recipientArray.remove(索引);
notifyDataSetChanged();
}
});
/*holder.txtRecName.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.e(“Hola”,“Hola”);
Intent rec_Intent=新的Intent(getContext(),RecipientAddressActivity.class);
rec_Intent.putExtra(“当前_收件人”,recipientArray.get(index));
startActivity(记录意图);
}
});*/
返回行;
}
}
以下是活动布局文件的一部分:

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



当您单击列表项时,ImageButton会聚焦。因此,将
android:focusable=“false”
添加到xml中的
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); 

        }
    });
语境;
公共CustomListAdapter(上下文,int textViewResourceId,
ArrayList对象){
超级(上下文、textViewResourceId、对象);
this.recipientArray=对象;
this.context=context;//初始化
}
holder.txtRecName.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.e(“Hola”,“Hola”);
Intent rec_Intent=新的Intent(上下文,RecipientAddressActivity.class);
rec_Intent.putExtra(“当前_收件人”,recipientArray.get(index));
背景。起始触觉(记录意图);
}
});
在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类中应用可序列化接口


享受。

使用
holder.txtRecName.setOnClickListener()
在适配器类中,为什么在onActivityResult内部调用setOnitemclicklistner可能是问题所在。将其放在onActivityResult外部,然后尝试…当在适配器中应用onClickListener时,您将获得listview项click listener,因此尝试在click listener上实现方法1之一。在adapter或JU中声明click listenert实现列表项click listener…@Shayanpourvatan我已经尝试过了..如果你看到它注释的代码..holder.TxtREcName.setonclicklistener的问题是我无法从那里开始新的活动。StartActivity无法实现。@user2688158你需要上下文,因为StartActivity是活动类的一种方法谢谢:)你是aweso
 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); 

        }
    });
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);


        }
    });