带有填充有 SQL 值的复选框的 ListView
ListView with CheckBoxes filled with SQL values
我在我的 Android 项目中创建了 SQL 数据库,并设法用我插入的数据填充 ListView。该项目的下一部分是为我的 ListView 中的每个项目(来自 SQL 数据库)启用复选框。我找到了一种方法来处理字符串值,但我不确定如何处理 SQL 数据库中的值。
是否可以将 SQL 值放入 String 中?或者我需要使用不同的数据值来填充我的 ListView ?
我对 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 String name;
public FoodContract.FoodEntry entry;
我在其中为我的数据库行定义了所有字符串值。
(_ID、姓名等)。 (我只看到了解决我的问题的方法)。所以,代码现在看起来像这样:
public ModelBreakfast(FoodContract.FoodEntry entry, boolean checked){
this.entry = entry;
this.checked = checked;
}
下一个 class 是 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 将显示我的 SQL 数据库值?
Should I use ArrayList instead String? And how?
还是我之前在上一个问题中说的。查看 for 循环。因此,在您的 SQLDB Activity 中以及从数据库中获取值的函数中,您需要填充一个数组列表,您将在 MainActivity.
中调用该列表
public ArrayList<String> getAirportRegion(String code)
Cursor cursor = db.rawQuery("SELECT "+ AIRPORT_NAME +
" FROM " + AIRPORT_TABLE + " WHERE " + AIRPORT_CODE + " = " + code, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
arrayList.add(cursor.getString(cursor.getColumnIndex(AIRPORT_NAME)));
cursor.moveToNext();
}
}
cursor.close();
return arrayList;
}
现在在 Main Activity 中获取对数据库的引用并将其设置为 modelBreakfastArrayList,就像这样
airportArrayList = mdb.getAirportRegion();
瞧,完成了
你看到我是如何提取数据的了吗?在大多数情况下,这是从本地数据库中提取列表的最佳方式。将这些活动分开,我也希望您将数据库 activity 作为单例,否则,您将拥有多个数据库,这会消耗资源。在下面查看我如何开始这些数据库活动。
private DBHelper(Context context) {
super(context, "db", null, DATABASE_VERSION);
}
private static DBHelper INSTANCE;
public static DBHelper getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new DBHelper(context);
}
return INSTANCE;
}
我在我的 Android 项目中创建了 SQL 数据库,并设法用我插入的数据填充 ListView。该项目的下一部分是为我的 ListView 中的每个项目(来自 SQL 数据库)启用复选框。我找到了一种方法来处理字符串值,但我不确定如何处理 SQL 数据库中的值。
是否可以将 SQL 值放入 String 中?或者我需要使用不同的数据值来填充我的 ListView ?
我对 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 String name;
public FoodContract.FoodEntry entry;
我在其中为我的数据库行定义了所有字符串值。
(_ID、姓名等)。 (我只看到了解决我的问题的方法)。所以,代码现在看起来像这样:
public ModelBreakfast(FoodContract.FoodEntry entry, boolean checked){
this.entry = entry;
this.checked = checked;
}
下一个 class 是 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 将显示我的 SQL 数据库值?
Should I use ArrayList instead String? And how?
还是我之前在上一个问题中说的。查看 for 循环。因此,在您的 SQLDB Activity 中以及从数据库中获取值的函数中,您需要填充一个数组列表,您将在 MainActivity.
中调用该列表public ArrayList<String> getAirportRegion(String code)
Cursor cursor = db.rawQuery("SELECT "+ AIRPORT_NAME +
" FROM " + AIRPORT_TABLE + " WHERE " + AIRPORT_CODE + " = " + code, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
arrayList.add(cursor.getString(cursor.getColumnIndex(AIRPORT_NAME)));
cursor.moveToNext();
}
}
cursor.close();
return arrayList;
}
现在在 Main Activity 中获取对数据库的引用并将其设置为 modelBreakfastArrayList,就像这样
airportArrayList = mdb.getAirportRegion();
瞧,完成了
你看到我是如何提取数据的了吗?在大多数情况下,这是从本地数据库中提取列表的最佳方式。将这些活动分开,我也希望您将数据库 activity 作为单例,否则,您将拥有多个数据库,这会消耗资源。在下面查看我如何开始这些数据库活动。
private DBHelper(Context context) {
super(context, "db", null, DATABASE_VERSION);
}
private static DBHelper INSTANCE;
public static DBHelper getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new DBHelper(context);
}
return INSTANCE;
}