我怎么设置在ListView到一个ArrayAdapter,Android的 [英] how do I set the listView to the ArrayAdapter, Android
本文介绍了我怎么设置在ListView到一个ArrayAdapter,Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么设置这个arrayAdapter我的ListView所以它填充我的屏幕上ListView的?
how do I set this arrayAdapter to my listView so it populates the listView on my screen?
例如:
listview.setAdapter(MyAdapter);
这里是code为我MainActivity和ListAdapter:
here is the code for my MainActivity and ListAdapter:
public class MainActivity extends Activity {
CheckBoxInfo cbr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbr = new CheckBoxInfo();
cbr.checkBoxName = "dfdjklfjdkljf";
cbr.checkBoxState = true;
ListView listview = (ListView) findViewById(R.id.listView);
}
public class MyAdapter extends ArrayAdapter<CheckBoxInfo> {
private List<CheckBoxInfo> checkBoxList;
private Context context
public MyAdapter(List<CheckBoxInfo> infoList, Context context) {
super(context, R.layout.row_layout, infoList);
this.checkBoxList = infoList;
this.context = context;
for(int i = 0; i <=12; i++){
checkBoxList.add(cbr);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
// First let's verify the convertView is not null
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, parent, false);
}
// Now we can fill the layout with the right values
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
CheckBoxInfo cbi = checkBoxList.get(position);
tv.setText(cbi.checkBoxName);
return convertView;
}
} // end MyAdapter
}
推荐答案
不要在类文件下面要设置为ListView适配器:
Do the following in the class file where you want to set the adapter for the ListView :
listview = (ListView) findViewById(R.id.myListView);
myAdapter = new MyAdapter(this, R.layout.row_layout, listInfo);
listview.setAdapter(myAdapter);
我会建议您使用视图架和convertview模式创建列表视图,因为它会更有效率。的这里是它如何工作的一个很好的解释。如果你想引用一个code样品我有它在 Github上。
I would recommend that you use the View holder and convertview pattern to create the listView as it will be more efficient.Here is a good explanation of how it works. If you want to refer to a code sample I have it on Github .
希望这有助于。
这篇关于我怎么设置在ListView到一个ArrayAdapter,Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文