简体   繁体   English

从Android中的Listview Adapter访问Activity中的TextView

[英]Access to a TextView in Activity from Listview Adapter in android

I have an Activity with a ListView and some TextViews like below, 我有一个带有ListView的Activity和下面的一些TextViews,

I want to call setText() method of TextViews in OnClickListener in fill() method of adapter. 我想在适配器的fill()方法中的OnClickListener中调用TextViews的setText()方法。 but I don't access to these TextViews ...! 但我无法访问这些TextViews ...!

How can do this? 怎么办呢?

ActivityMoshtari.class: 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: 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: 我的ListView有适配器:

AdapterMoshtariItem.class 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. 为此,您需要在适配器构造函数中添加三个TextView参数。 changes in adapter declare Textview variables in adapter class 适配器中的更改在适配器类中声明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;
}

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. 最后,从类文件传递适配器中的textViews。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android ListView Adapter的多个活动 - Android ListView Adapter multiple Activity Android-具有imageview和textview的自定义适配器不显示listview - Android - Custom adapter with imageview and textview not displaying listview 从片段更新ListView适配器内的TextView - Updating TextView inside ListView Adapter from a Fragment Android-如何将Textview值从Activity-A传递到适配器 - Android - How do you pass textview values from Activity-A to an adapter Android:通过单击按钮,将一个活动的TextView添加到另一个活动的ListView中 - Android: Add TextView from one activty, to ListView in another activity through a button click 从Adapter类填充Activity的TextView的问题 - Problem with filling Activity's TextView from the Adapter class 将列表大小从活动转移到适配器 class 并使用该数量更新 TextView - Transfer list size from Activity to Adapter class and update TextView with that quantity Android如何在Activity中访问Fragment Adapter? (适配器为空) - Android how to access Fragment Adapter in Activity? (Adapter is null) TextView没有出现在带有自定义适配器的ListView中 - TextView not appearing in ListView with Custom Adapter 如何从适配器访问arrayList到活动? - How to access an arrayList from an adapter to an activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM