简体   繁体   中英

How to Populate RadioGroup and ListView from Database Android Studio

For Starters, this set of codes work :)

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

Activity Class 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);
        }
    }
}

Summary: database values to arraylist and then to listView or RadioGroup Note that this set wasn't able to get the actual "id" from column ID from the data table. To get the actual ID use data.getString(0) since data.getString(1) gets values from the second column

now the real question is, does this have a simpler way? :D

Instead of running loop two times and also creating two different methods you can merge those into one and make it simple and also efficient.

Maintain a counter as global variable and keep on updating it until all db items are fetched and added into 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM