从Android中的Listview Adapter访问Activity中的TextView [英] Access to a TextView in Activity from Listview Adapter in android
问题描述
我有一个带有ListView和一些TextView的Activity,如下所示,
I have an Activity with a ListView and some TextViews like below,
我想在适配器的fill()方法中的OnClickListener中调用TextViews的setText()方法.但我无法访问这些TextViews ...!
I want to call setText() method of TextViews in OnClickListener in fill() method of adapter. but I don't access to these TextViews ...!
该怎么办?
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>
我的ListView有适配器:
And I have Adapter for my 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;
}
}
推荐答案
您可以将视图传递给适配器,以便可以在需要时进行更新.
为此,您需要在适配器构造函数中添加三个TextView
参数.
适配器的变化
在适配器类中声明Textview
变量
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;
}
并更改填充数据功能
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.
Finally pass the textViews in adapter from class file.
adaptermoshtari = new AdapterMoshtariItem(moshtariItems,txtMoshtariName,txtInfoMoshtariTel,txtInfoMoshtariMob);
lstMoshtari.setAdapter(adaptermoshtari);
这篇关于从Android中的Listview Adapter访问Activity中的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!