Reputation: 9351
getting compile error for the "CheckBoxInfo" variable in the code below, it is correct yet i get the error,
listview = (ListView) findViewById(R.id.listView);
myAdapter = new MyAdapter(this, R.layout.row_layout, CheckBoxInfo);
listview.setAdapter(myAdapter);
from the documents it says: "the objects to represent in the Listview", however CheckBoxInfo IS the objects to represent in the listview. what is wrong here?
the rest of the code:
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
}
the other class, representing objects to populate the listview:
public class CheckBoxInfo {
Boolean checkBoxState;
String checkBoxName;
public CheckBoxInfo(){
checkBoxState = false;
checkBoxName = "";
}
}
Upvotes: 0
Views: 162
Reputation: 610
The error in the arguments you pass to the constructor
listview = (ListView) findViewById(R.id.listView);
List<CheckBoxInfo> ls = new ArrayList<CheckBoxInfo>();
ls.add(cbr);
myAdapter = new MyAdapter(ls, this);
listview.setAdapter(myAdapter);
And the override of toString function in CheckBoxInfo class
public String toString(){
return "Name : " + checkBoxName + " , Checked : " + checkBoxState;
}
Upvotes: 1
Reputation: 2528
See constructor of myAdapter class is having only 2 argument.and in your code you are calling constructor with 3 arguments.
Upvotes: 0
Reputation: 181
You should pass a list of CheckBoxInfo elements in the ArrayAdapter constructor.
ArrayList<CheckBoxInfo> items = new ArrayList<CheckBoxInfo>();
// add elements to the list
(...)
myAdapter = new MyAdapter(this, R.layout.row_layout, items);
Upvotes: 1