2017-10-17 80 views
0

我們可以顯示數據庫中的所有記錄沒有問題。當我們嘗試限制要用sql顯示的項目時選擇搜索並且RecyclerView適配器填充正確。 在列表視圖中選擇項目時代碼失敗。列表視圖沒有得到關於這個記錄在什麼位置的消息,所以當我們從ListActivity導航到DetailActivity視圖不是ListView中的項時,我的問題是如何管理Adapter使用的位置變量? 此代碼流如下按鈕單擊MainActivity設置搜索變量轉到ListActivity調用DBHelper返回到ListActivity與modelList是陣列列表是設計是MVP所以我們有一個模型類相關的代碼如下RecyclerAdapter設置適配器位置

主要活動BTN點擊

public void findAllData(View view){ 
    selectSTRING = etFromDate.getText().toString(); 
    Intent intent = new Intent(MainActivity.this, ListActivity.class); 
    startActivity(intent); 
} 

ListActivity調用DBHelper註釋掉行獲取所有數據

 helpher = new DBHelper(this); 
    dbList = new ArrayList<>(); 
    dbList = helpher.getRangeDataFromDB(); 
    //dbList = helpher.getDataFromDB(); 

DBHelper代碼獲取所選擇的記錄或記錄,最終

public List<DBModel> getRangeDataFromDB() { 
    List<DBModel> modelList = new ArrayList<>(); 
    db = this.getReadableDatabase(); 
    String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " ='" + selectSTRING + "'"; 
    Cursor cursor = db.rawQuery(query, null); 
    //Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + "='" + str + "'" , null); 
    String newBACK = selectSTRING; 

    if (cursor.moveToFirst()) { 
     DBModel model = new DBModel(); 

     while (!cursor.isAfterLast()) { 
      if (newBACK == selectSTRING) { 
       model.setRowid(cursor.getString(0)); 
       model.setStation_Name(cursor.getString(1)); 
       model.setDate_of_Purchase(cursor.getString(2)); 
       model.setGas_Cost(cursor.getString(3)); 
       modelList.add(model); 
       cursor.moveToNext(); 
      } 
     } 
    } 
    int sz = modelList.size(); 
    System.out.println("========= SIZE "+sz); 
    db.close(); 
    return modelList; 
} 

現在我們使用的意圖去DetailsActivity,這是失敗有關發送數據從DBHelper不知道如何寫在類的意圖回

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 

static List<DBModel> dbList; 
static private Context context; 

RecyclerAdapter(Context context, List<DBModel> dbList) { 

    RecyclerAdapter.dbList = new ArrayList<>(); 
    RecyclerAdapter.context = context; 
    RecyclerAdapter.dbList = dbList; 
} 

@Override 
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, null); 

    // create ViewHolder 
    ViewHolder viewHolder = new ViewHolder(itemLayoutView); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) { 

    holder.rowid.setText(dbList.get(position).getRowid()); 
    holder.station.setText(dbList.get(position).getStation_Name()); 
    System.out.println("========== new position "+position); 


} 

@Override 
public int getItemCount() { 
    return dbList.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public TextView station, rowid; 

    public ViewHolder(View itemLayoutView) { 
     super(itemLayoutView); 
     rowid = (TextView) itemLayoutView.findViewById(R.id.rvROWID); 
     station = (TextView) itemLayoutView.findViewById(R.id.rvSTATION); 
     // Attach a click listener to the entire row view 
     itemLayoutView.setOnClickListener(this); 

    } 

    @Override // When an item in DetailsActivity is touched (selected) the RecyclerView has 
    // a OnClickListener attached in the above Code that implements the method below 
    public void onClick(View v) { 
     System.out.println("======RowID "+rowid); 
     Intent intentN = new Intent(context, DetailsActivity.class); 
     Bundle extras = new Bundle(); 
     extras.putInt("POSITION", getAdapterPosition()); 
     extras.putString("FROM_LIST_ACTIVITY", "false"); 
     ///position = getAdapterPosition(); 
     ///position = getLayoutPosition();// Both work the same 
     intentN.putExtras(extras); 
     context.startActivity(intentN); 
    } 
} 

思想。這變成意大利麪代碼!

回答

1

解決這個問題是開發商有多個搜索設計在DBHelper分別由不同的按鈕對搜索活動這樣的設計在DBHelper導致多的ArrayList都具有相同的名稱此開車RecycleAdapter瘋狂的,因爲它觸發綁定到ArrayList所以OLD先生布爾救援!這裏是修改後的設計代碼功能 在搜索活動中declare public static Boolean use = false; 並導入需要的地方import static com..MainActivity.use;

下面是每個搜索按鈕的代碼

public void findAllData(View view){ 
    helper = new DBHelper(this); 
    helper.getDataFromDB(); 
    use = false; 
    // Set Mr. Boolean 
    Intent intent = new Intent(MainActivity.this, ListActivity.class); 
    // ListActivity shows Results of the Search 
    startActivity(intent); 
} 

public void findSelect(View v){ 
    selectSTRING = etFromDate.getText().toString(); 
    // Get your Search variable 
    helper = new DBHelper(this); 
    helper.getDataFromDB(); 
    etToDate.setText(sendBACK); 
    use = true; 
    Intent intent = new Intent(MainActivity.this, ListActivity.class); 
    startActivity(intent); 
} 

現在我們做DBHelper所需的搜索

/* Retrive ALL data from database table named "TABLE_INFO" */ 
public List<DBModel> getDataFromDB(){ 
    //String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " > 0 " + " ORDER BY " + Col_ID + " DESC "; 
    /* Notice the SPACES before AND after the words WHERE ORDER BY ASC or DESC most of all the condition " > 0 "*/ 
    /* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*/ 
    Cursor cursor = null; 
    List<DBModel> modelList = new ArrayList<>(); 
    if(use == true){ 
     String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " ='" + selectSTRING + "'"; 
     db = this.getWritableDatabase(); 
     cursor = db.rawQuery(query,null); 
    } 
    if(use == false){ 
     String query = "SELECT * FROM " + TABLE_INFO; 
     db = this.getWritableDatabase(); 
     cursor = db.rawQuery(query,null); 
    } 

    if (cursor.moveToFirst()){ 

     do { 
      DBModel model = new DBModel(); 
      model.setRowid(cursor.getInt(0)); 
      model.setStation_Name(cursor.getString(1)); 
      model.setDate_of_Purchase(cursor.getString(2)); 
      model.setGas_Cost(cursor.getString(3)); 
      modelList.add(model); 

      int sz = modelList.size(); 
      int out = model.setRowid(cursor.getInt(0)); 
      String out1 = model.setStation_Name(cursor.getString(1)); 
      String out2 = model.setDate_of_Purchase(cursor.getString(2)); 
      String out3 = model.setGas_Cost(cursor.getString(3)); 
      System.out.println("==============getDataFromDB ID "+out); 
      System.out.println("==============getDataFromDB Station "+out1); 
      System.out.println("==============getDataFromDB Date "+out2); 
      System.out.println("==============getDataFromDB Cost "+out3); 
      System.out.println("======= ======getDataFromDB SIZE "+sz); 

     }while (cursor.moveToNext()); 
    } 
    db.close(); 
    cursor.close(); 
    return modelList; 
} 

這個唯一的失足是,如果如果你按日期搜索,並做一個添加到數據庫並跳回到ListActivity的新記錄不顯示 我們正在努力保持這種狀態敬請關注哈哈好工作James_Duh

+0

我有很多關於設計的問題所以這使得問題如何篩選或添加選定的項目到綁定到ViewHolder的ArrayList看起來VH不與ArrayList同步這很奇怪,如果你刪除它會同步並進行尺寸調整 –

0

你應該設置你的OnClickListener這裏:

@Override 
public void onBindViewHolder(ReportAdapter.ViewHolderReport holder, int position) { 
    final Object object = objects.get(position); 
    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Do Something with the object at this position 
     } 
    }); 
} 

,而不是你ViewHolder因爲你不應該相信在一個時間適配器位置。

+0

@sokarcreactive我會嘗試這是截至目前不知道如何修改代碼,所以我編輯帖子,並顯示RecyclerAdapter類的所有代碼你能告訴我這將如何解決這個問題?我的綁定是在RecyclerAdapter中,你會放在哪裏? –

+0

當ViewHolders在recyclerview中繪製,屏幕上可見時,已爲每個視圖調用了OnBindViewHolder。所以你可以確定這個位置上的物體是正確的物體。 – sokarcreative

+0

讓我試試你發佈的代碼謝謝如果我能看到它的話,我會投票它不知道如何設置爲我的對象,因爲我膨脹CardView來創建與HOLDER綁定的兩個TextView你是否使用這個CardView的設計理念作爲自己的XML文件? –