一旦按下按钮,我想用不同的内容更新列表视图中的单行.我知道我可以使用notifydatasetchanged()但这会更新整个listView.
我已经阅读了这个answer,它非常适合我想做的事情.
我已经完成了5行的列表视图,当我按下按钮时,我想用不同的文本更新第4行.我不想以编程方式设置文本,因为这只是一个虚拟项目,只是为了查看是否可以刷新单行,而我的真实项目比仅仅是textview要复杂得多.
所以我的问题是:我可以使用getView()来更新listview中的单行吗?
这是我的代码:
我的活动:
public class MainActivity extends Activity { public ListView list1; public listAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list1 = (ListView) findViewById(R.id.my_list); adapter = new listAdapter(this); list1.setAdapter(adapter); Button button1 = (Button) findViewById(R.id.my_button); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { adapter.setText("Different text"); View row2Update = list1.getChildAt(3); list1.getAdapter().getView(3,row2Update,list1); } }); } }
我的适配器:
public class listAdapter extends BaseAdapter{ public Activity activity; public String text="Normal Text"; public listAdapter(Activity activity){ this.activity = activity; } public void setText(String text){ this.text = text; } public int getCount() { return 5; } public Object getItem(int position) { return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position,View convertView,ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout,null); TextView textView = (TextView) rowView.findViewById(R.id.row_text); textView.setText(text); return rowView; } }
这就是活动的样子:
但是当我按下我的按钮时没有任何变化
解决方法
您不能自己调用适配器的getView()方法.适配器的getView()方法仅在调用时调用
>列表视图是创建的
>当用户滚动列表视图时
>调用notifysetdatachanged()时.
所有这些都是由操作系统完成的.为列表视图中的所有行调用GetView().它不仅仅需要一行.因此,如果要更改行,则必须在String [],ArrayList<>中再次提供数据.等等
如果要为单行显示不同的文本,按钮的onClick() – 您可以执行此操作
public class listAdapter extends BaseAdapter {
public Activity activity; public ArrayList<String> text; public listAdapter(Activity activity){ this.activity = activity; } public void setText(ArrayList<String> text){ this.text = text; } public int getCount() { return 5; } public Object getItem(int position) { return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position,ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout,null); TextView textView = (TextView) rowView.findViewById(R.id.row_text); textView.setText(text[position]); return rowView; }
}
在您的活动中:
list1 = (ListView) findViewById(R.id.my_list); adapter = new listAdapter(this); String[] entries={"Normal Text","Normal Text","Normal text","Normal text"}; ArrayList<String> text=Arrays.asList(entries); adapter.setText(text); list1.setAdapter(adapter); Button button1 = (Button) findViewById(R.id.my_button); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { text.set(3,"Different Text"); adapter.setText(text); list1.setAdapter(adapter); } });
正如@Andy在其中一条评论中所建议的那样,还有另一种方法:
listViewPeople.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a,View v,int position,long l) { //IF YOU WANT TO CHANGE THE CONTENT OF THE ROW CLICKED if(position == someNumber) { text.set(position,"different Text"); list1.setAdapter(text); } } });