-1

如何将此 arrayAdapter 设置为我的 listView,以便在我的屏幕上填充 listView?

例如:

   listview.setAdapter(MyAdapter);

这是我的 MainActivity 和 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

}

4

3 回答 3

0
 ListView listview = (ListView) findViewById(R.id.listView);
 listview.setAdapter(MyAdapter);
于 2013-04-08T07:59:27.927 回答
0

调用适配器类的构造函数

MyAdapter mAdapter=new MyAdapter(listOfInfo,MainActivity.this);
listView.setAdapter(mAdapter);
于 2013-04-08T08:05:11.180 回答
0

在要为 ListView 设置适配器的类文件中执行以下操作:

listview = (ListView) findViewById(R.id.myListView);
myAdapter = new MyAdapter(this, R.layout.row_layout, listInfo);
listview.setAdapter(myAdapter);

我建议您使用 View holder 和 convertview 模式来创建 listView,因为它会更有效。是一个很好的解释它是如何工作的。如果您想参考我在Github上提供的代码示例。

希望这可以帮助。

于 2013-04-08T08:11:03.873 回答