使用类的回收站列表中未显示 Sqlite 数据

Posted

技术标签:

【中文标题】使用类的回收站列表中未显示 Sqlite 数据【英文标题】:Sqlite Data Not Showing In Recycler List Using Class 【发布时间】:2019-10-05 10:54:57 【问题描述】:

我正在解决一个问题,我想向回收商列表显示特定的 sqlite 表数据。当我运行应用程序时,为了显示 sqlite 表数据,应用程序没有向我显示任何内容...

这是我的应用程序数据库助手类

 

private static final int DATABASE_VERSION = 2;

private static final String DATABASE_NAME = "DynamicERP.db";

public static final String table_orders = "TABLEORDERS";
public static final String table_poducts_records = "PRODUCTSRECORDS";


public static final String code = "ORDERCODE";
public static final String barcode = "BARCODE";
public static final String nametblp = "NAME";
public static final String size = "SIZE";
public static final String uname = "UNAME";
public static final String retail = "RETAIL";
public static final String wholesale = "WHOLESALE";
public static final String trade = "TRADE";
public static final String percentage = "PERCENTAGE";
public static final String tax = "TAX";
public static final String subcatagary = "SUBCATAGARY";
public static final String categary = "CATEGARY";
public static final String company = "COMPANY";
public static final String type = "TYPE";
public static final String packsize = "PACKSIZE";
public static final String weight = "WEIGHT";
public static final String weightunit = "WEIGHTUNIT";
public static final String currentstock = "CURRENTSTOCK";


public static final String salecode = "SALECODE";
public static final String mcode = "MCODE";
public static final String fname = "FIRSTNAME";
public static final String lname = "LASTNAME";
public static final String urduname = "URDUNAME";
public static final String address = "ADDRESS";
public static final String contact = "CONTACT";
public static final String cnic = "CNIC";
public static final String saletype = "TYPE";
public static final String route = "ROUTE";
public static final String area = "AREA";
public static final String zone = "ZONE";
public static final String balance = "BALANCE";
public static final String creditlimit = "CREDITLIMIT";
public static final String spl = "SPL";
public static final String saletax = "SALETAX";

private static final String DATABASE_ORDERS = "CREATE TABLE " + table_orders + "("
        + code + " INTEGER, " + barcode + " VARCHAR," + nametblp + " VARCHAR," + size + " VARCHAR," + uname +
        " VARCHAR," + retail + " VARCHAR," + wholesale + " VARCHAR," + trade + " VARCHAR,"
        + percentage + " VARCHAR," + tax + " VARCHAR," + subcatagary + " VARCHAR," + categary + " VARCHAR," +
        company + " VARCHAR," + type + " VARCHAR," + packsize + " VARCHAR," +
        weight + " VARCHAR," + weightunit + " VARCHAR, " + currentstock + " VARCHAR );";


private static final String DATABASE_PRODUCTS = "CREATE TABLE " + table_poducts_records + "("
        + salecode + " INTEGER, " + mcode + " VARCHAR, " + fname + " VARCHAR, " + lname + " VARCHAR, " + urduname +
        " VARCHAR, " + address + " NVARCHAR, " + contact + " VARCHAR," + cnic + " VARCHAR, " + saletype + " VARCHAR,"
        + route + " VARCHAR, " + area + " VARCHAR," + zone + " VARCHAR," +
        balance + " VARCHAR, " + creditlimit + " VARCHAR," + spl + " VARCHAR, " + saletax + " VARCHAR);";

private String DROP_ORDER_REC = "DROP TABLE IF EXISTS " + table_orders;
private String DROP_PRODUCTS_REC = "DROP TABLE IF EXISTS " + table_poducts_records;

public DatabaseHelper(Context context) 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);



@Override
public void onCreate(SQLiteDatabase db) 
    db.execSQL(DATABASE_ORDERS);
    db.execSQL(DATABASE_PRODUCTS);




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    db.execSQL(DROP_PRODUCTS_REC);
    db.execSQL(DROP_ORDER_REC);
    onCreate(db);



public void addProductRec() 
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(salecode, "123456");
    values.put(mcode, "123654698");
    values.put(fname, "Usman");
    values.put(lname, "Ali");
    values.put(urduname, "Usman");
    values.put(contact, "021354545");
    values.put(cnic, "54548787515");
    values.put(saletype, "credit");
    values.put(route, "Peshawer Road");
    values.put(area, "wah");
    values.put(zone, "Rawalpindi");
    values.put(balance, "1000");
    values.put(creditlimit, "10000");
    values.put(spl, "212");
    values.put(saletax, "10");
    values.put(address, "wah");
    db.insert(table_poducts_records, null, values);
    db.close();



public void addOrders() 
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(code, "098766");
    values.put(barcode, "5544444445");
    values.put(nametblp, "Usman");
    values.put(size, "12");
    values.put(uname, "Ali");
    values.put(retail, "500");
    values.put(wholesale, "whole");
    values.put(trade, "credit");
    values.put(percentage, "7");
    values.put(tax, "1000");
    values.put(subcatagary, "laptop");
    values.put(categary, "computers");
    values.put(company, "hp");
    values.put(type, "laptop");
    values.put(packsize, "12");
    values.put(weight, "5");
    values.put(weightunit, "kg");
    values.put(currentstock, "57");

    db.insert(table_orders, null, values);
    db.close();


public List<GetSetOrders> getAllOrderData() 

    String[] columns = 
            nametblp,
            trade,
            retail,
            subcatagary,
            company
    ;

    String sortOrder = nametblp + " ASC";

    List<GetSetOrders> clientlist = new ArrayList<GetSetOrders>();

    SQLiteDatabase db = this.getReadableDatabase();


    Cursor cursor = db.query(table_orders,
            columns,
            null,
            null,
            null,
            null,
            sortOrder);

    if (cursor.moveToFirst()) 
        do 
            GetSetOrders orders = new GetSetOrders();

            orders.setNAME(cursor.getString(cursor.getColumnIndex(nametblp)));
            orders.setTRADE(cursor.getString(cursor.getColumnIndex(trade)));
            orders.setRETAIL(cursor.getString(cursor.getColumnIndex(retail)));
            orders.setCATEGARY(cursor.getString(cursor.getColumnIndex(subcatagary)));
            orders.setCOMPANY(cursor.getString(cursor.getColumnIndex(company)));
            clientlist.add(orders);

         while (cursor.moveToNext());
    
    cursor.close();
    db.close();

    return clientlist;

这是我的客户端类,这个类也使用适配器...


RecyclerView recyclerViewClients;
Button btnAll;
ClientRecyclerAdapter clientRecyclerAdapter;
List<GetSetOrders> listclients;
DatabaseHelper databaseHelper;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    View view =  inflater.inflate(R.layout.clients, container, false);

    btnAll=view.findViewById(R.id.searchall);
    recyclerViewClients=view.findViewById(R.id.Viewclients);

    listclients = new ArrayList<>();

    clientRecyclerAdapter = new ClientRecyclerAdapter(listclients,recyclerViewClients);
    recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
    recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
    recyclerViewClients.setHasFixedSize(true);
    recyclerViewClients.setAdapter(clientRecyclerAdapter);

    databaseHelper = new DatabaseHelper(getActivity());

    btnAll.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            getClientFromSqlite();
            Toast.makeText(getActivity(), "Usman", Toast.LENGTH_SHORT).show();
        
    );

    return view;


private void getClientFromSqlite() 

    new AsyncTask<Void, Void, Void>() 
        @Override
        protected Void doInBackground(Void... params) 
            listclients.clear();
            listclients.addAll(databaseHelper.getAllOrderData());
           // clientRecyclerAdapter.notifyDataSetChanged();
            return null;
        

        @Override
        protected void onPostExecute(Void aVoid) 
            super.onPostExecute(aVoid);
            clientRecyclerAdapter.notifyDataSetChanged();
        
    .execute();

这是我的回收器适配器类...



private List<GetSetOrders> listOrders;
Context mContext;
RecyclerView mRecyclerView;
View itemView;

public ClientRecyclerAdapter(List<GetSetOrders> listOrders, RecyclerView recyclerView) 
    this.listOrders =  listOrders;
    mRecyclerView = recyclerView;


@Override
public ClientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    mContext = parent.getContext();
    itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.items_products_table, parent, false);

    return new ClientViewHolder(itemView);



public class ClientViewHolder extends RecyclerView.ViewHolder 

    public TextView lblPName;
    public TextView lblTrade;
    public TextView lblRetail;
    public TextView lblSubCategary;
    public TextView lblCompany;
    RelativeLayout layout;


    public ClientViewHolder(View view) 
        super(view);


        lblPName =  view.findViewById(R.id.lblPName);
        lblTrade =  view.findViewById(R.id.lblTrade);
        lblRetail =  view.findViewById(R.id.lblRetail);
        lblSubCategary = view.findViewById(R.id.lblSubCategary);
        lblCompany =  view.findViewById(R.id.lblCompany);
        layout = view.findViewById(R.id.listprod);
    


@Override
public void onBindViewHolder(ClientViewHolder holder, final int position) 

    holder.lblPName.setText(listOrders.get(position).getNAME());
    holder.lblTrade.setText(listOrders.get(position).getORDERCODE());
    holder.lblRetail.setText(listOrders.get(position).getRETAIL());
    holder.lblSubCategary.setText(listOrders.get(position).getSUBCATAGARY());
    holder.lblCompany.setText(listOrders.get(position).getCOMPANY());
    holder.layout.setOnLongClickListener(new View.OnLongClickListener() 
        @Override
        public boolean onLongClick(View v) 
            Toast.makeText(mContext, "ABC", Toast.LENGTH_SHORT).show();
            return false;
        
    );



@Override
public int getItemCount() 
    Log.v(ClientRecyclerAdapter.class.getSimpleName(), "" + listOrders.size());
    return listOrders.size();


在客户端类中按下按钮后它没有显示任何数据...

【问题讨论】:

【参考方案1】:

您忘记将布局管理器添加到您的recyclerViewClients

添加这一行。

recyclerViewClients.setLayoutManager(new LinearLayoutManager(getActivity()));

你的代码一定是这样的

clientRecyclerAdapter = new ClientRecyclerAdapter(listclients,recyclerViewClients);
recyclerViewClients.setItemAnimator(new DefaultItemAnimator());
recyclerViewClients.setItemAnimator(new DefaultItemAnimator()); //multi copy paste!
recyclerViewClients.setLayoutManager(new LinearLayoutManager(getActivity()));//Add this line
recyclerViewClients.setHasFixedSize(true);
recyclerViewClients.setAdapter(clientRecyclerAdapter);

【讨论】:

这条红线表示红线下的错误...线性布局管理器(android.content.client) @AlizaShah 将其替换为上下文。我将编辑我的答案 处理它并尝试运行它 很高兴为您提供帮助

以上是关于使用类的回收站列表中未显示 Sqlite 数据的主要内容,如果未能解决你的问题,请参考以下文章

回收站视图项目中未显示分隔线

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview

如何对回收站查看 SQLite 内容进行排序

Recycler视图在android studio中未显示一张卡片视图

React-native Sqlite,强制列表视图更新新数据

将 json 数据存储到 sqlite 并检索到回收站视图