我使用自定义列表视图来显示sqlite数据库中的数据。我的清单项目 有文本视图,按钮,复选框。请建议我将执行以下任务的适配器,
当我点击按钮时,它会将我带到新屏幕并传递按钮的ID。
请建议我现在该做什么。新的想法也欢迎。
关注我的代码。
public class GuestListAdapter extends BaseAdapter implements OnClickListener{
Context context;
ArrayList<String> arrayListFirstName;
ArrayList<String> arrayListLastName;
ArrayList<String> arrayListGuests;
ArrayList<String> arrayCustomOne;
ArrayList<String> arrTicketId;
private ArrayList<Boolean> checks=new ArrayList<Boolean>();
public GuestListAdapter(Context mcontext, ArrayList<String> arrListFN,
ArrayList<String> arrListLN, ArrayList<String> arrListGuest,
ArrayList<String> arrTicketID,ArrayList<String> arrCustOne) {
arrayListFirstName = arrListFN;
arrayListLastName = arrListLN;
arrayListGuests = arrListGuest;
arrayCustomOne = arrCustOne;
context = mcontext;
for (int i = 0; i < arrayListFirstName.size(); i++) {
checks.add(i, false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayListFirstName.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayListFirstName.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.guest_list_item, parent, false);
}
final String dataModel = (String) arrayListFirstName.get(position);
TextView txtFirstName = (TextView) view
.findViewById(R.id.txt_firstname);
txtFirstName.setText(arrayListFirstName.get(position));
TextView txtLastName = (TextView) view.findViewById(R.id.txt_lastname);
txtLastName.setText(arrayListLastName.get(position));
TextView txtGuestCount = (TextView) view
.findViewById(R.id.txt_guest_count);
txtGuestCount.setText(arrayListGuests.get(position));
Button btnInfo = (Button) view.findViewById(R.id.btn_info);
// btnInfo.setId(position);
CheckBox checkBoxCheckins = (CheckBox) view
.findViewById(R.id.checkBoxIsCheckedIn);
checkBoxCheckins.setTag(Integer.valueOf(position));
btnInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// int id = Integer.parseInt(v.getTag().toString());
// Log.d("", "Clicked ID "+id);
Intent intent = new Intent(context, InfoScreen.class);
context.startActivity(intent);
System.out.println("Adap button **********");
}
});
checkBoxCheckins
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
System.out.println("Adap checked **********");
Integer integ = (Integer)buttonView.getTag();
checks.set(integ.intValue(), false);
}
});
if (checks.get(position)) {
checkBoxCheckins.setChecked(checks.get(position));
// do some stuff if wanted..
} else {
checkBoxCheckins.setChecked(checks.get(position));
// do some stuff if wanted..
}
return view;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Integer index = (Integer)view.getTag();
boolean state = checks.get(index.intValue());
checks.set(index.intValue(), !state);
}
}
this is my setter class
public class GuestList {
String mFirstName;
String mLastName;
String mTicketI;
String mGuestCount;
public String getmFirstName() {
return mFirstName;
}
public void setmFirstName(String mFirstName) {
this.mFirstName = mFirstName;
}
public String getmLastName() {
return mLastName;
}
public void setmLastName(String mLastName) {
this.mLastName = mLastName;
}
public String getmTicketI() {
return mTicketI;
}
public void setmTicketI(String mTicketI) {
this.mTicketI = mTicketI;
}
public String getmGuestCount() {
return mGuestCount;
}
public void setmGuestCount(String mGuestCount) {
this.mGuestCount = mGuestCount;
}
}
在listview类中发送像这样的arraylist,
cursorAdapter = new GuestListAdapter(GuestListScreen.this,
arrFirstName, arrLastName, arrGuests, arrTicketId, arrCustomOne);
listViewGuests.setAdapter(cursorAdapter);
答案 0 :(得分:0)
您应该使用BaseAdapter。创建一个类调用MyAdapter并扩展BaseAdater。在该类中重写所需的方法。并且在类的getView方法中膨胀你的xml文件并根据位置放置数据。
如果值来自数据库,则获取Cursor并迭代它并获得所需的值。
希望这会有所帮助