私はAndroidプロジェクトでSQLデータベースを作成し、挿入したデータでListViewを作成することができました。プロジェクトの次の部分は、ListView内のすべての項目(SQLデータベースから)に対してCheckBoxを有効にすることです。私は文字列の値でそれを行う方法を発見したが、私はSQLデータベースからの値でそれを行う方法がわからない。CheckBoxがSQL値で埋められたListView
SQL値をStringに入れることはどういうことですか?または、私はListViewを作成するために別のデータ値を使用する必要がありますか?
私はまだAndroidのSQLでnoobyですので、すべてのアドバイスが役に立ちます。
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;
}
次のクラスでは、最後の部分は、私は私のContractClass public FoodContract.FoodEntry entry;
でpublic String name;
を交換した後、私はあることを理解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();
}
});
}}
ある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;
}}
です私は使用できません
modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView", false));
。しかし、私は何を設定する必要がありますので、私のリストボックスでCheckBoxesは私のSQLデータベースの値を表示しますか?
代わりにStringを使用しますか?そしてどうやって?