对于初学者,这组代码有效:)
活动XML //list_data.xml
<ListView
android:id="@+id/ListView"
android:layout_width="368dp"
android:layout_height="200dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<RadioGroup
android:id="@+id/RadioGroup"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
</RadioGroup>
Database Helper dbHelper.java
package com.example.excgen.taxi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Excgen on 9/25/2017.
*/
public class dbHelper extends SQLiteOpenHelper {
private static final String TAG = "databaseHelper";
private static final String TABLE_NAME = "units";
private static final String COL1 = "id";
private static final String COL2 = "plate";
public dbHelper(Context context){
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, "+COL2+" TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean addData(String item){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
Log.d(TAG,"addData: Adding "+item+" to "+TABLE_NAME);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ TABLE_NAME;
Cursor data = db.rawQuery(query,null);
return data;
}
}
活动类listData.java
package com.example.excgen.taxi;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
public class listData extends AppCompatActivity {
public static String TAG = "listDataActivity";
dbHelper mdbHelper;
private ListView mlistView;
private RadioGroup mRadioGroup;
private RadioButton mRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
mlistView = (ListView) findViewById(R.id.ListView);
mdbHelper = new dbHelper(this);
populateListView();
populateRadioGroup();
}
private void populateListView(){
Log.d(TAG,"Populate List View; Displaying Data in the List View");
Cursor data = mdbHelper.getData();
ArrayList<String> dataList = new ArrayList<>();
while(data.moveToNext()){
dataList.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,dataList);
mlistView.setAdapter(adapter);
}
private void populateRadioGroup(){
Cursor data = mdbHelper.getData();
ArrayList<String> dataList = new ArrayList<>();
while(data.moveToNext()){
dataList.add(data.getString(1));
}
for (int i = 0; i < dataList.size()-1; i++) {
mRadioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
mRadioButton = new RadioButton(this);
mRadioButton.setId(i);
mRadioButton.setText(dataList.get(i));
mRadioGroup.addView(mRadioButton);
}
}
}
摘要:数据库值为arraylist,然后为listView或RadioGroup 请注意,此集合无法从数据表中的列ID获取实际“id”。 要获取实际ID,请使用data.getString(0),因为data.getString(1)从第二列获取值
现在真正的问题是,这有更简单的方法吗? :d
答案 0 :(得分:0)
不是运行循环两次,而是创建两种不同的方法,您可以将它们合并为一种方法,使其简单而有效。
将计数器维护为全局变量并继续更新它,直到获取所有数据库项并将其添加到arraylist中。
private int count=0;
private void populateListView(){
Log.d(TAG,"Populate List View; Displaying Data in the List View");
Cursor data = mdbHelper.getData();
ArrayList<String> dataList = new ArrayList<>();
while(data.moveToNext()){
dataList.add(data.getString(1));
mRadioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
mRadioButton = new RadioButton(this);
mRadioButton.setId(i);
mRadioButton.setText(dataList.get(i));
mRadioGroup.addView(mRadioButton);
count++;
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,dataList);
mlistView.setAdapter(adapter);
}