我在我的Android項目中創建了SQL數據庫,並設法用插入的數據填充ListView。該項目的下一部分是爲ListView中的每個項目(來自SQL數據庫)啓用CheckBoxes。我已經找到了一種方法來處理字符串值,但我不確定如何使用SQL數據庫中的值來完成此操作。帶有填充了SQL值的複選框的ListView
是它在某種程度上可以把SQL值轉換成String?或者我需要使用不同的數據值來填充我的ListView?
我仍然nooby與Android的SQL,所以每一個建議將是有益的。
這裏是代碼:
public class ModelBreakfast {
public String name; //This String need to be filled with SQL datas. If it's possible.
public boolean checked;
public ModelBreakfast(String name, boolean checked){
this.name = name;
this.checked = checked;
}
}
只需要說,我試圖用我的ContractClass public FoodContract.FoodEntry entry;
,我定義的所有字符串值,我的數據庫行來替換public String name;
。 (_ID,NAME等)。 (我只看到這種方式來解決我的問題)。所以,代碼現在看起來像這樣:
public ModelBreakfast(FoodContract.FoodEntry entry, boolean checked){
this.entry = entry;
this.checked = checked;
}
下節課是CustomAdapter
public class CustomAdapterBreakfast extends ArrayAdapter<ModelBreakfast> {
private ArrayList<ModelBreakfast> dataSet;
Context mContext;
private static class ViewHolder {
TextView txtName;
CheckBox checkBox;
}
public CustomAdapterBreakfast(ArrayList<ModelBreakfast> data, Context context){
super(context, R.layout.activity_breakfast_checkbox, data);
this.dataSet = data;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_breakfast_checkbox, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
ModelBreakfast item = getItem(position);
viewHolder.txtName.setText(item.name); //Need to replace or modify this part
viewHolder.checkBox.setChecked(item.checked);
return result;
}}
最後一部分是在MainActivity
public class BreakfastActivity extends AppCompatActivity {
ArrayList<ModelBreakfast> modelBreakfastArrayList;
private CustomAdapterBreakfast customAdapterBreakfast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breakfast);
ListView listView = (ListView) findViewById(R.id.listBreakfast);
modelBreakfastArrayList = new ArrayList<>();
modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView. So I need to somehow replace that String with SQL datas.", false));
customAdapterBreakfast = new CustomAdapterBreakfast(modelBreakfastArrayList, getApplicationContext());
listView.setAdapter(customAdapterBreakfast);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ModelBreakfast modelBreakfast= modelBreakfastArrayList.get(position);
modelBreakfast.checked = !modelBreakfast.checked;
customAdapterBreakfast.notifyDataSetChanged();
}
});
}}
我與我的ContractClass public FoodContract.FoodEntry entry;
更換public String name;
後,我明白,我不能使用
modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView", false));
。但比我需要設置什麼,所以我的ListView與CheckBoxes將顯示我的SQL數據庫值?
我應該使用ArrayList,而不是字符串?如何?