简体   繁体   中英

How to fetch data to listview?

fragment_history.xml (consisting of one listview):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HistoryFragment">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_View"
        />

</LinearLayout>

I'm trying to fetch data from database into list view.

HistoryFragment.java:

public class HistoryFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_history, container, false);

        ListView lw = view.findViewById(R.id.list_View);
        MyDatabaseHelper myDB = new MyDatabaseHelper(getActivity());
        List<ContactModel> data = myDB.fetchData();
        ArrayAdapter<ContactModel> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, data);
        lw.setAdapter(adapter);

        return view;
    }

}

Fetch function in MyDatabaseHelper.java ( full code ):

public List<ContactModel> fetchData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    List<ContactModel> data = new ArrayList<ContactModel>();
    if(cursor.moveToFirst()){
        do {
            data.add(new ContactModel(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5)));
        }while(cursor.moveToNext());
    }
    cursor.close();
    return data;
}

ContactModel.java:

public class ContactModel {
    private String id;
    private String weight;
    private String height;
    private String age;
    private String gender;
    private String bmi;

    public ContactModel(String id, String weight, String height, String age, String gender,String bmi) {
        this.id = id;
        this.weight = weight;
        this.height = height;
        this.age = age;
        this.gender = gender;
        this.bmi = bmi;
    }

    public String getID() {
        return this.id;
    }

    public String getWeight() {
        return this.weight;
    }

    public String getHeight() {
        return this.height;
    }

    public String getAge() {
        return this.age;
    }

    public String getGender() {
        return this.gender;
    }
    public String getBmi() {
        return this.bmi;
    }
}

Output . When I print cursor to logcat it showed same text.

I found this method that does the trick without a custom layout. fetch function(found in MyDatabaseHelper.java):

public Cursor getListContents(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }

HistoryFragment.java

ListView lw = view.findViewById(R.id.list_View);
ArrayList<String> theList = new ArrayList<>();
            Cursor data = myDB.getListContents();
 
                while(data.moveToNext()){
                    theList.add("ID: " + data.getString(0) +"\nWeight: " + data.getString(1) + "\t\tHeight: " + data.getString(2) + "\t\tAge: " + data.getString(3)
                            + "\nGender: " + data.getString(4) + "\t\tBMI: " + data.getString(5));
                    ListAdapter listAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,theList);
                    lw.setAdapter(listAdapter);
                }

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