SQLite数据未在recycleview

时间:2018-02-03 07:10:20

标签: android sqlite android-recyclerview android-sqlite

我一直试图在片段中使用recycleview在Android应用中显示我的sqlite数据。但是,虽然logcat显示我正在从表中获取数据,但cardview并未显示任何数据。 / p>

这是我的memberActivity,我调用了recycleview并创建了一个适配器类 MemberActivity.java:

package com.aust.austpc.austpcbeta6.database;

import ...
public class MemberActivity extends Fragment {
     RecyclerView recyclerView;
     ArrayList<MemberDesc> listItems;
     MemAdapter adapter = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.activity_member,container,false);
        recyclerView=(RecyclerView) v.findViewById(R.id.member_activity);
        recyclerView.setHasFixedSize(true);
        listItems=new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        DatabaseHandler databaseHandler = new DatabaseHandler(v.getContext());
        SQLiteDatabase sqLiteDatabase= databaseHandler.getReadableDatabase();
        Cursor cursor = databaseHandler.getAllData();
        cursor.moveToFirst();
        Log.i(TAG, cursor.getColumnName(0));
        do{
            MemberDesc memberDesc=new MemberDesc();
            listItems.add(memberDesc);
            Log.i(TAG, "SHOWING");

        }while (cursor.moveToNext());
        databaseHandler.close();
        adapter=new MemAdapter(listItems);
        recyclerView.setAdapter(adapter);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("Member");
    }
    }


    class MemAdapter extends RecyclerView.Adapter<MemAdapter.ViewHolder>
{
    private ArrayList<MemberDesc> listItems;
    private Context context;

    public MemAdapter(ArrayList<MemberDesc> listItems) {
        this.listItems = listItems;
    }


    @Override
    public MemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.member_row,parent,false);
        return new MemAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MemAdapter.ViewHolder holder, int position) {
        MemberDesc memberDesc=listItems.get(position);
        holder.textViewName.setText(memberDesc.getName());
        holder.textViewRoll.setText(memberDesc.getRoll());
        holder.textViewPhone.setText(memberDesc.getPhone());
        holder.textViewYear.setText(memberDesc.getYear());
        holder.textViewSem.setText(memberDesc.getSem());
        Log.i(TAG, holder.textViewName.getText().toString());
    }

    @Override
    public int getItemCount() {
        //int i=listItems.size();
        //Log.i(TAG, Integer.toString(i));
        return listItems.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView textViewName;
        public TextView textViewRoll;
        public TextView textViewPhone;
        public TextView textViewYear;
        public TextView textViewSem;
        public ViewHolder(View itemView) {
            super(itemView);

            textViewName=(TextView)itemView.findViewById(R.id.memName);
            textViewRoll=(TextView)itemView.findViewById(R.id.memRoll);
            textViewPhone=(TextView)itemView.findViewById(R.id.memPhone);
            textViewYear=(TextView)itemView.findViewById(R.id.memYear);
            textViewSem=(TextView)itemView.findViewById(R.id.memSem);
        }
    }
}

member_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/memName" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/memRoll"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/memPhone"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/memYear"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/memSem"
            />
    </LinearLayout>

</android.support.v7.widget.CardView>

MemberDesc.java:

package com.aust.austpc.austpcbeta6.database;

/**
 * Created by USER on 2/3/2018.
 */

public class MemberDesc {
    private String name, roll, phone, year, sem;

    public MemberDesc(String name, String roll, String phone, String year, String sem) {
        this.name = name;
        this.roll = roll;
        this.phone = phone;
        this.year = year;
        this.sem = sem;
    }
    public MemberDesc()
    {

    }

    public String getName() {
        return name;
    }

    public String getRoll() {
        return roll;
    }

    public String getPhone() {
        return phone;
    }

    public String getYear() {
        return year;
    }

    public String getSem() {
        return sem;
    }

}

DatabaseHandler.java:

package com.aust.austpc.austpcbeta6.database;

import ...
/**
 * Created by USER on 2/3/2018.
 */

public class DatabaseHandler extends SQLiteOpenHelper
{



    public static final String Database_Name="MEMBER.db";
    public static final String TABLE_NAME="MEMBER_TABLE";
    public static final String KEY_ID="ID";
    public static final String KEY_NAME ="NAME";
    public static final String KEY_ROLL="ROLL";
    public static final String KEY_CONTACT="CONTACT";
    public static final String KEY_YEAR="YEAR";
    public static final String KEY_SEMESTER="SEMESTER";





    public DatabaseHandler(Context context) {
        super(context,Database_Name,null,1);

    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("create table "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ROLL TEXT,CONTACT TEXT,YEAR TEXT,SEMESTER TEXT)");
        Log.i(TAG, "Datbase CREATED");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);



    }
    public boolean insertData(String NAME,String  ROLL, String CONTACT, String YEAR, String SEMESTER)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME,NAME);
        contentValues.put(KEY_ROLL,ROLL);
        contentValues.put(KEY_CONTACT,CONTACT);
        contentValues.put(KEY_YEAR,YEAR);
        contentValues.put(KEY_SEMESTER,SEMESTER);
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result==-1)
            return false;
        else
            return true;

    }
    public Cursor getAllData(){
        SQLiteDatabase  db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " +TABLE_NAME, null);
        return cursor;
    }

}

2 个答案:

答案 0 :(得分:0)

Pass the data as parameters to the DATA OBJECT Class Constructor MemberDesc()

try this .

do{
            String name = cursor.getString(cursor.getColumnIndex("NAME");
            String roll = cursor.getString(cursor.getColumnIndex("ROLL");
            String phone = cursor.getString(cursor.getColumnIndex("CONTACT");
            String year = cursor.getString(cursor.getColumnIndex("YEAR");
            String sem = cursor.getString(cursor.getColumnIndex("SEMESTER");
            MemberDesc memberDesc=new MemberDesc(name,roll,phone,year sem);
            listItems.add(memberDesc);
            Log.i(TAG, "SHOWING");

        }while (cursor.moveToNext());

Hope this may solve your problem

答案 1 :(得分:0)

I think you should Create a Method like this

    public ArrayList<CartData> getAllData(){
    ArrayList<CartData> d = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();

    String select = "SELECT * FROM " +TABLE_NAME;
    Cursor cursor = db.rawQuery(select,null);
    if (cursor.moveToFirst()){
        do{
            CartData dta = new CartData(cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getBlob(5));
            d.add(dta);
        }while (cursor.moveToNext());
    }
    db.close();
    return d;
}

now add this in getAllData() method in your adapter

finalCartAdapter = new FinalCartAdapter(FinalOrderActivity.this, sqLiteHandler.getAllData());
    finalCartAdapter.notifyDataSetChanged();
    finalItems.setAdapter(finalCartAdapter);

but lastly u require a CartData.class

public class CartData {

String title;
String printname;
String mrp;
String qty;
byte[] image;

public CartData(String title, String printname, String mrp, String qty,byte[] image) {
    this.title = title;
    this.printname = printname;
    this.mrp = mrp;
    this.qty = qty;
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getPrintname() {
    return printname;
}

public void setPrintname(String printname) {
    this.printname = printname;
}

public String getMrp() {
    return mrp;
}

public void setMrp(String mrp) {
    this.mrp = mrp;
}

public String getQty() {
    return qty;
}

public void setQty(String qty) {
    this.qty = qty;
}

public byte[] getImage() {
    return image;
}

public void setImage(byte[] image) {
    this.image = image;
}
}

U got a readymade code by the way. just change cartData items to the fields mapped in your Sqlite.