带有CheckBox的ListView填充了SQL值

时间:2022-04-10 09:14:38

I have created SQL database in my Android project and managed to populate ListView with data that I inserted. Next part of the project is to enable CheckBoxes for every item (from SQL database) in my ListView. I have found a way how to do it with String values, but I am not sure how to do it with values from SQL database.

我在我的Android项目中创建了SQL数据库,并设法使用我插入的数据填充ListView。该项目的下一部分是为ListView中的每个项目(来自SQL数据库)启用CheckBoxes。我找到了一种方法如何使用String值,但我不知道如何使用SQL数据库中的值。

Is it somehow possible to put SQL values into String ? Or I need to use different data values to populate my ListView ?

是否有可能将SQL值放入String?或者我需要使用不同的数据值来填充我的ListView?

I am still nooby with SQL in Android, so every advice would be helpfull.

我在Android中仍然不熟悉SQL,所以每个建议都会有所帮助。

Here is code:

这是代码:

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

Just need to say that I tried to replace public String name; with my ContractClass public FoodContract.FoodEntry entry; where I defined all String values for my database rows. (_ID, NAME, etc). (I only saw that way to solve my problem). So, code is now looking like this:

只需要说我试图替换公共String名称;与我的ContractClass公共FoodContract.FoodEntry条目;我为数据库行定义了所有String值。 (_ID,NAME等)。 (我只看到了解决问题的方法)。所以,代码现在看起来像这样:

public ModelBreakfast(FoodContract.FoodEntry entry, boolean checked){
    this.entry = entry;
    this.checked = checked;
}

Next class is CustomAdapter

下一课是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;
}}

Last part is the MainActivity

最后一部分是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();
        }
    });
}}

After I replaced public String name; with my ContractClass public FoodContract.FoodEntry entry; I understand that I can't use

我替换公共String名称后;与我的ContractClass公共FoodContract.FoodEntry条目;我明白我不能用

modelBreakfastArrayList.add(new ModelBreakfast("This string will show in ListView", false));. But than what do I need to set, so my ListView with CheckBoxes will displaying my SQL database values ?

modelBreakfastArrayList.add(new ModelBreakfast(“此字符串将在ListView中显示”,false));.但是我需要设置什么,所以带CheckBoxes的ListView会显示我的SQL数据库值?

Should I use ArrayList instead String? And how?

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

1 个解决方案

#1


0  

Again as I said before in the last question. Look at the for loops. So within your SQLDB Activity and in the function that is taking the values out of the database, you need to populate an array list that you will call in the MainActivity.

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

Now in the Main Activity get a reference to the database and set it to modelBreakfastArrayList like so

现在在Main Activity中获取对数据库的引用并将其设置为modelBreakfastArrayList

airportArrayList = mdb.getAirportRegion(); 

Voila it is done

瞧它完成了

Do you see how I am extracting the data? For the most part, this is the best way to extract lists from the local database. Keep these Activities separate, also I hope you have the Database activity as a singleton, otherwise, you will have multiple databases and that will guzzle up resources. Look below for how I start these database activities.

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

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

#1


0  

Again as I said before in the last question. Look at the for loops. So within your SQLDB Activity and in the function that is taking the values out of the database, you need to populate an array list that you will call in the MainActivity.

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

Now in the Main Activity get a reference to the database and set it to modelBreakfastArrayList like so

现在在Main Activity中获取对数据库的引用并将其设置为modelBreakfastArrayList

airportArrayList = mdb.getAirportRegion(); 

Voila it is done

瞧它完成了

Do you see how I am extracting the data? For the most part, this is the best way to extract lists from the local database. Keep these Activities separate, also I hope you have the Database activity as a singleton, otherwise, you will have multiple databases and that will guzzle up resources. Look below for how I start these database activities.

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

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