2017-07-06 60 views
0

我在我的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,而不是字符串?如何?

回答

0

再次爲我的最後一個問題之前說。看看for循環。因此,在您的SQLDB活動以及將值從數據庫中取出的函數中,您需要填充將在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; 
} 

現在的主要活動獲取數據庫的引用,將其設置爲modelBreakfastArrayList像這樣

airportArrayList = mdb.getAirportRegion(); 

瞧它完成

你怎麼看我提取數據?大部分情況下,這是從本地數據庫中提取列表的最佳方式。保持這些活動是分開的,我也希望你將數據庫活動作爲一個單獨的活動,否則,你將擁有多個數據庫,並且會激增資源。請看下面我如何開始這些數據庫活動。

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