单击按钮时如何更新列表视图?
Posted
技术标签:
【中文标题】单击按钮时如何更新列表视图?【英文标题】:How to update the listview when i click the button? 【发布时间】:2013-01-13 22:13:33 【问题描述】: public class MainActivity extends Activity
ListView list;
String[] abc="1","2","3";
MyCustomAdapter adapter;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView) findViewById(R.id.list);
refresh=(Button) findViewById(R.id.refresh);
adapter=new MyCustomAdapter(MainActivity.this,abc);
list.setAdapter(adapter);
private class MyCustomAdapter extends BaseAdapter
private final Context context;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private String[] hashmap;
ViewHolder holder = null;
public MyCustomAdapter(Context context,String[] hashMaps)
super();
this.context = context;
this.hashmap = hashMaps;
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public int getCount()
return hashmap.length;
public Object getItem(int position)
return hashmap.length;
public long getItemId(int position)
return position;
@Override
public void notifyDataSetChanged() // Create this function in your adapter class
super.notifyDataSetChanged();
@Override
public View getView(final int position, View convertView, ViewGroup parent)
System.out.println("getView " + position + " " + convertView);
// final ViewHolder holder = null;
if (convertView == null)
convertView = mInflater.inflate(R.layout.view1, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.text);
holder.btn = (Button)convertView.findViewById(R.id.refresh);
convertView.setTag(holder);
else
holder = (ViewHolder)convertView.getTag();
holder.textView.setText(hashmap[position]);
holder.btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.textView = (TextView)rl.getChildAt(position);
Log.i("position",String.valueOf(position));
holder.textView.setText("10");
);
return convertView;
public static class ViewHolder
public TextView textView;
public Button btn;
View.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:id="@+id/real"
>
<TextView
android:id="@+id/text"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:text="Text"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
/>
<Button
android:id="@+id/refresh"
android:layout_
android:layout_
android:layout_toRightOf="@id/text"
android:text="button"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>
在我的 ListView 中,当我单击按钮时,适配器视图中的值未更改。如何解决此问题?该列表包含每个位置的 3 个值。因此,当我单击单击位置的按钮时,该值将更改而不是旧值。
【问题讨论】:
看看这个***.com/a/8309715/779408 【参考方案1】:修改getView方法代码如下。
@Override
public View getView(final int position, View convertView,ViewGroup parent)
View vi = convertView;
final TextView textView;
if (convertView == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.vi, null);
textView = (TextView) vi.findViewById(R.id.text);
Button btn = (Button) vi.findViewById(R.id.refresh);
textView.setText(hashmap[position]);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
Log.i("position ", position + "");
textView.setText("10");
);
return vi;
【讨论】:
【参考方案2】:试试这个:
holder.btn.setTag(convertView);
holder.btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
View tempView = v.getTag();
TextView tv = (TextView)tempView.findViewById(R.id.text);
tv.setText("your string");
);
【讨论】:
【参考方案3】:叫这个
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, your_new_value));
欲了解更多信息,请尝试link
【讨论】:
【参考方案4】:对于更新值列表视图,您可以使用adapter.notifyDataSetChanged()
这将刷新您的列表适配器。但是在更改数据源中的值后调用它。
【讨论】:
【参考方案5】:你应该使用 notifyDataSetChanged() 方法来刷新你的列表视图...
在适配器上调用 notifyDataSetChanged()
refresh.setOnClickListener(new OnClickListener() @Override public void onClick(View p_v) // TODO Auto-generated method stub adapter.notifyDataSetChanged(); );
【讨论】:
我正在使用 holder.its 在行 list.notifyDataSetChanged() 中不起作用;错误ListView类型的notifyDataSetChanged()方法未定义以上是关于单击按钮时如何更新列表视图?的主要内容,如果未能解决你的问题,请参考以下文章