我有一个带有ListView的Activity和下面的一些TextViews,

我想在适配器的fill()方法中的OnClickListener中调用TextViews的setText()方法。但我无法访问这些TextViews ...!

怎么办呢?

ActivityMoshtari.class:

public class ActivityMoshtari extends Activity {

public ArrayList<StructMoshtariItem>    moshtariItems   = new ArrayList<StructMoshtariItem>();
public ArrayAdapter                     adaptermoshtari;
ListView                                lstMoshtari;
TextView                                txtInfoMoshtariName;
TextView                                txtInfoMoshtariTel;
TextView                                txtInfoMoshtariMob;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_moshtari);
    txtInfoMoshtariName= (TextView) findViewById(R.id.txtInfoMoshtariName);
    txtInfoMoshtariMob = (TextView) findViewById(R.id.txtInfoMoshtariMob);
    txtInfoMoshtariTel = (TextView) findViewById(R.id.txtInfoMoshtariTel);

    lstMoshtari = (ListView) findViewById(R.id.lstMoshtari);
    adaptermoshtari = new AdapterMoshtariItem(moshtariItems);
    lstMoshtari.setAdapter(adaptermoshtari);

    for (int i = 0; i < 10; i++) {
        StructMoshtariItem moshtariitem = new StructMoshtariItem();
        moshtariitem.id = "" + i;
        moshtariitem.name = "some name" + i;
        moshtariitem.tel = "someTel" + i;

        moshtariItems.add(moshtariitem);
    }
        adaptermoshtari.notifyDataSetChanged();
}}


activity_moshtari.xml:

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

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lstMoshtari"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         >
    </ListView>
</LinearLayout>

<LinearLayout
    android:id="@+id/layInfoMoshtari"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtInfoMoshtariTel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:textColor="#000"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/txtInfoMoshtariMob"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:textColor="#000"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/txtInfoMoshtariName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:textColor="#000"
        android:textSize="26sp" />
</LinearLayout>




我的ListView有适配器:

AdapterMoshtariItem.class

public class AdapterMoshtariItem extends ArrayAdapter<StructMoshtariItem> {

public AdapterMoshtariItem(ArrayList<StructMoshtariItem> array) {
    super(G.context, R.layout.moshtari_item, array);
}


private static class ViewHolder {

    public ViewGroup    layoutRoot;
    public TextView     txtMoshtariID;
    public TextView     txtMoshtariName;
    public TextView     txtMoshtariTel;
    public ImageView    imgMoshtariRecordView;

    public ViewHolder(View view) {
        layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
        txtMoshtariID = (TextView) view.findViewById(R.id.txtMoshtariID);
        txtMoshtariName = (TextView) view.findViewById(R.id.txtMoshtariName);
        txtMoshtariTel = (TextView) view.findViewById(R.id.txtMoshtariTel);
        imgMoshtariRecordView = (ImageView) view.findViewById(R.id.imgMoshtariRecordView);
    }

    public void fill(final ArrayAdapter<StructMoshtariItem> adapter, final StructMoshtariItem item, final int position) {
        txtMoshtariID.setText(item.id);
        txtMoshtariName.setText(item.name);
        txtMoshtariTel.setText(item.tel);

        layoutRoot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


            }
        });

    }
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    StructMoshtariItem item = getItem(position);
    if (convertView == null) {
        convertView = G.inflater.inflate(R.layout.moshtari_item, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.fill(this, item, position);
    return convertView;
}


}

最佳答案

您可以将视图传递给适配器,以便可以在需要时进行更新。
    为此,您需要在适配器构造函数中添加三个TextView参数。
    适配器的变化
    在适配器类中声明Textview变量

TextView name,tel,mob;

public AdapterMoshtariItem(ArrayList<StructMoshtariItem> array,TextView txtInfoMoshtariName,TextView txtInfoMoshtariTel
                                    ,TextView txtInfoMoshtariMob) {
super(G.context, R.layout.moshtari_item, array);
this.name=txtInfoMoshtariName;
this.tel=txtInfoMoshtariTel;
this.mob=txtInfoMoshtariMob;
}


并更改填充数据功能

public void fill(final ArrayAdapter<StructMoshtariItem> adapter, final StructMoshtariItem item, final int position) {
        name.setText(item.id);
        tel.setText(item.name);
        mob.setText(item.tel);

        layoutRoot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


            }
        });

    }


最后,从类文件传递适配器中的textViews。

 adaptermoshtari = new AdapterMoshtariItem(moshtariItems,txtMoshtariName,txtInfoMoshtariTel,txtInfoMoshtariMob);
 lstMoshtari.setAdapter(adaptermoshtari);

09-27 18:36