0

以下のコードで「CheckBoxInfo」変数のコンパイル エラーが発生していますが、これは正しいのですが、エラーが発生します。

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

ドキュメントからは、「リストビューで表すオブジェクト」と書かれていますが、CheckBoxInfoはリストビューで表すオブジェクトです。ここで何が問題なのですか?

コードの残りの部分:

public class MainActivity extends Activity {

CheckBoxInfo cbr;
private ListAdapter MyAdapter;
ListView listview;
MyAdapter myAdapter;


@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) findViewById(R.id.listView);
    myAdapter = new MyAdapter(this, R.layout.row_layout, CheckBoxInfo);
    listview.setAdapter(myAdapter);

}

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

}

リストビューに入力するオブジェクトを表す他のクラス:

public class CheckBoxInfo {
 Boolean checkBoxState;
 String checkBoxName;

 public CheckBoxInfo(){
   checkBoxState = false;
   checkBoxName = "";
 }

}

4

3 に答える 3

1

ArrayAdapter コンストラクターで CheckBoxInfo 要素のリストを渡す必要があります。

ArrayList<CheckBoxInfo> items = new ArrayList<CheckBoxInfo>();
// add elements to the list
(...)
myAdapter = new MyAdapter(this, R.layout.row_layout, items);
于 2013-04-08T08:39:41.767 に答える
1

コンストラクタに渡す引数のエラー

      listview = (ListView) findViewById(R.id.listView);
      List<CheckBoxInfo> ls = new ArrayList<CheckBoxInfo>();
      ls.add(cbr);
      myAdapter = new MyAdapter(ls, this);
      listview.setAdapter(myAdapter);

そして CheckBoxInfo クラスの toString 関数のオーバーライド

public String toString(){
    return "Name : " + checkBoxName + " , Checked : " + checkBoxState;
}
于 2013-04-08T08:54:52.177 に答える
0

myAdapter クラスのコンストラクターには引数が 2 つしかないことを確認してください。コードでは、3 つの引数でコンストラクターを呼び出しています。

于 2013-04-08T08:49:29.050 に答える