mardi 26 avril 2016

SQLite data in ListView inside Fragment

I have created half of the app and I am stuck where I need to retrieve the data from the SQLite database and show them in a listView inside a Tab fragment.I have two tabs and one of them will show the list of data from SQLite database.

Some people on stackoverflow suggested to use recyclerview instead of listview but it is difficult for me to use listview and I no nothing about the recyclerview. I am new to android and I feel that these problems aren't that difficult for the professional developers but for me it is quite challenging.

I am able to show the data from database in Fragment but I am able to show only one row which is the latest entry. I want to display the whole list in the Fragment from latest --> old , top --> bottom.The other part of the problem is that the list doesn't update automatically. I need to close the app and reopen it to see the latest entry in the Fragment.

Here is my code -

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.kevin.sugar.DataBase.DateBaseHelper;
import com.example.kevin.sugar.DataBase.Information;
import com.example.kevin.sugar.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class PeopleFragment extends Fragment {

    DateBaseHelper dateBaseHelper;
    SQLiteDatabase sqLiteDatabase;
    Cursor cursor;

    TextView name,weight,height;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mview = inflater.inflate(R.layout.person_row, container, false);

        name = (TextView)mview.findViewById(R.id.person_name);
        weight = (TextView)mview.findViewById(R.id.heatbeat);
        height = (TextView)mview.findViewById(R.id.bpm_upper_lower);

        dateBaseHelper = new DateBaseHelper(getContext());
        sqLiteDatabase = dateBaseHelper.getReadableDatabase();
        cursor = dateBaseHelper.getUserInformation();
        if (cursor.moveToFirst()){
            do{
                String user_name,user_weight,user_height;
                user_name = cursor.getString(cursor.getColumnIndex(Information.NAME));
                user_weight = cursor.getString(cursor.getColumnIndex(Information.WEIGHT));
                user_height = cursor.getString(cursor.getColumnIndex(Information.HEIGHT));

                Log.e("App",user_name+ "" + user_height + " " +user_weight);
                name.setText(user_name);
                weight.setText(user_weight);
                height.setText(user_height);

            }while (cursor.moveToNext());
        }

        return mview;
    }

}

Aucun commentaire:

Enregistrer un commentaire