Access to a TextView in Activity from Listview Adapter in android

我怕爱的太早我们不能终老 提交于 2019-12-23 03:14:21

问题


I have an Activity with a ListView and some TextViews like below,

I want to call setText() method of TextViews in OnClickListener in fill() method of adapter. but I don't access to these TextViews ...!

How can do this?

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>

And I have Adapter for my 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;
}

}


回答1:


You can pass the view to adapter so that you can able to update it when ever needed. for that you need to add three TextView params in your adapter constructor. changes in adapter declare Textview variables in adapter class

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

And change the fill data function

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


            }
        });

    }

Finally pass the textViews in adapter from class file.

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


来源:https://stackoverflow.com/questions/31529317/access-to-a-textview-in-activity-from-listview-adapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!