gpt4 book ai didi

java - Android 从 BaseAdapter 刷新 ListActivity

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:06 25 4
gpt4 key购买 nike

我有一个 ListActivity 和一个扩展 BaseAdapter 的自定义适配器。

在 ListView 的每一行上,都有一个按钮和几个 View 。 onClickListener 在适配器中声明。

单击按钮后如何刷新 ListView

这是代码

列表 Activity :

 .........
super.onCreate(savedInstanceState);


here im reading content from a file and then creating adapter with that content

adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
..........

适配器:

.......

public class UvArrayAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater linflater;
private TextView txt_1, txt_2, txt_3;
private ProgressBar pr1;
private String[] str1;
private String[] str2;
private String[] str3;
private Integer[] int1;

private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
Log.v("TAG", "onItemClick at position" + mPosition);
}
}



public UvArrayAdapter(Context context, String[] s1, String[] s2, Integer[] i1) {
mContext = context;
str1 = s1;
str2 = s2;
int1 = i1;
linflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return str1.length;
}

@Override
public Object getItem(int arg0) {
return str1[arg0];
}

@Override
public long getItemId(int arg0) {
return arg0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {

convertView = linflater.inflate(R.layout.uv_rowlayout, null);

}
convertView.setOnClickListener(new OnItemClickListener(position));
txt_1 = (TextView) convertView.findViewById(R.id.textView1);
pr1 = (ProgressBar) convertView.findViewById(R.id.progressBar1);
pr1.setProgress(int1[position]);
txt_1.setText(str1[position]);

txt_2 = (TextView) convertView.findViewById(R.id.textView2);
txt_2.setText(str2[position]+"mV");
Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Log.v("onClickListener", "button - clicked at position " position);

do some changes in mentioned file and after that i need to update the list view with new data
}});




return convertView;

}



}

最佳答案

adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated

注意:如果您在上述行中遇到任何错误,您可以尝试以下代码(其中 mListView 是我的 ListView 对象的名称)

((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged(); 

另一种方法是使列表无效,以便它再次从更新的数据集中重新绘制。

mListView=(ListView) findViewById(R.id.MyListView);
mListView.invalidate();

关于java - Android 从 BaseAdapter 刷新 ListActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11972586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com