活动和适配器之间的数据 [英] Data between activity and adapter

查看:123
本文介绍了活动和适配器之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BaseAdapter显示的ListView一行。
活性和适配器writen在两个不同的类。
在ListActivity,我使用下面的行code组数据。

 的String []文件名= {A,B,C};
MyAdapter一个=新MyAdapter(这一点,文件名);
setListAdapter(一);

在适配器,它显示每行中的文件名。
但它可能更改文件名。
因此,在活动中,我想这已经改变了数组名。
我该怎么办?

适配器code:

 私有String []名= NULL;
公共BookmarkListAdapter(上下文ctxt,字符串[] S){
名称= S;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewTag viewTag;
如果(convertView == NULL){
convertView = myInflater.inflate(R.layout.row_bookmark_list,NULL);
viewTag =新ViewTag((的TextView)convertView.findViewById(R.id.row_bookmark_info));
convertView.setTag(viewTag);
}
其他{
viewTag =(ViewTag)convertView.getTag();
}
viewTag.n.setText(名称[位置]);
名字[位置] =名称[(位置+ 1)%3]。 //更改
}
类ViewTag {
TextView的N;
公共ViewTag(TextView的T){
this.n = T;
}
}

在本实施例中,阵列变化为{B,C,一个}。
在活动中,如何让这改变了数组?


解决方案

 但它可以更改文件名。

你的意思是在的文件名然后你可以叫 youradapter.notifyDataSetChanged(),并将其值修改将更新你的价值观。

或者,如果你的意思改变变量,那么你需要重新实例您的适配器,并通过新的变量名吧。

注意:如果没有必要改变varible名称,然后不这样做,每一个你需要时间更新值

I use BaseAdapter to show row of ListView. The activity and adapter are writen in two different class. In ListActivity, I use below code set data of row.

String[] FileName = {"a", "b", "c"};
MyAdapter a = new MyAdapter(this, FileName);
setListAdapter(a);

In adapter, it show FileName in each row. But it may change the FileName. So in activity, I want to get the array FileName which has been changed. How can I do?

Adapter code:

private String[] name = null;
public BookmarkListAdapter(Context ctxt, String[] s) {
name = s;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewTag viewTag;
if(convertView == null) {
convertView = myInflater.inflate(R.layout.row_bookmark_list, null);
viewTag = new ViewTag((TextView)convertView.findViewById(R.id.row_bookmark_info));
convertView.setTag(viewTag);
}
else {
viewTag = (ViewTag) convertView.getTag();
}
viewTag.n.setText(name[position]);
name[position] = name[(position + 1) % 3];   //change
}
class ViewTag {
TextView n;
public ViewTag(TextView t) {
this.n = t;
}
}

In this example, the array change to {"b", "c", "a"}. In activity, how to get the array which changed?

解决方案

But it may change the FileName. 

Do you mean to change in values of the FileName then you can call youradapter.notifyDataSetChanged() and it will update your values.

Or if your mean to change the variable then you need to reinstantiate your adapter and pass the new variable name to it.

Note: If not necessary to change the varible name then don't do it and update the value every time you need.

这篇关于活动和适配器之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆