带有CheckBox的ListView填充了SQL值

时间:2017-07-06 13:44:16

标签: android sql listview android-checkbox

我在Android项目中创建了SQL数据库,并设法用我插入的数据填充ListView。该项目的下一部分是为ListView中的每个项目(来自SQL数据库)启用CheckBoxes。我找到了一种方法如何使用String值,但我不知道如何使用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;我在其中定义了数据库行的所有String值。 (_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 String name;替换public FoodContract.FoodEntry entry;后,我明白我无法使用

modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView", false));。但是我需要设置什么,所以带CheckBoxes的ListView会显示我的SQL数据库值吗?

  

我应该使用ArrayList而不是String吗?如何?

1 个答案:

答案 0 :(得分:0)

正如我之前在最后一个问题中所说的那样。看看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(); 

Voila已经完成了

您是否了解我如何提取数据?在大多数情况下,这是从本地数据库中提取列表的最佳方法。将这些活动分开,我希望您将数据库活动作为单例,否则,您将拥有多个数据库,这将扼杀资源。请看下面我如何开始这些数据库活动。

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;
}