简体   繁体   中英

Access to a TextView in Activity from Listview Adapter in android

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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