RecyclerAdapter设置适配器位置 [英] RecyclerAdapter set adapter position

查看:71
本文介绍了RecyclerAdapter设置适配器位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以显示数据库中的所有记录没有问题.当我们尝试限制要通过sql显示的项目时,请选择搜索,然后RecyclerView Adapter会正确填充. 在列表视图中选择该项时,代码将失败.列表视图未获得有关此记录位于什么位置的消息,因此当我们从ListActivity导航到DetailActivity视图时,该视图不是ListView中的项目 我的问题是如何管理适配器使用的位置变量? 此代码流如下,在MainActivity上单击按钮,将搜索变量转到ListActivity,该变量将调用DBHelper,然后返回带有modelList和Array List的ListActivity,是设计,是MVP,因此我们在下面有与模型类相关的代码

We can show all records in the DB no issue. When we try to limit the items to show with a sql Select the search and the RecyclerView Adapter populates correctly. The code fails when the item is selected in the list view. The list view did not get the message about what position this record is at so the view when we navigate to the DetailActivity view from ListActivity is not the item in the ListView My question is how to manage the position variable that the Adapter is using? This code flow is as follows a button click on MainActivity sets the search variable goes to ListActivity that makes a call to DBHelper which returns to ListActivity with modelList which is and Array List Yes the design is MVP so we have a Model Class relevant code below

主要活动btn单击

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

对DBHelper的ListActivity调用注释掉了行,获取了所有数据

ListActivity call to DBHelper commented out line gets all data

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

DBHelper代码以最终捕获一个或多个选定记录

DBHelper code to grab the selected record or records eventually

    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,但这是失败的

Now we use an intent to go to DetailsActivity and this is the fail

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);
   }
}

关于从DBHelper发回数据的想法,不确定如何在该类中编写Intent.这变成了意大利面条代码!

Thought about sending the data back from the DBHelper not sure how to write an Intent in that Class. This is turning into spaghetti code!

推荐答案

此问题的解决方案是,开发人员在DBHelper中具有多个搜索设计,每个搜索设计都由搜索上的不同按钮触发. ArrayLists都具有相同的名称,这将RecycleAdapter绑定到ArrayList上,使它疯狂了,所以OLD Boolean先生来了!这是修改后的设计代码功能 在搜索活动中,声明public static Boolean use = false; 并在需要的地方导入导入静态com..MainActivity.use;

The solution to this issue is the developer had multiple search designs in the DBHelper each being triggered by different buttons on the search Activity this design in the DBHelper lead to multiple ArrayLists all with the same name this drove the RecycleAdapter crazy as it is bound to ArrayList so OLD Mr. Boolean to the rescue! Here is the revised design code features In the Search Activity declare public static Boolean use = false; and Import where needed import static com..MainActivity.use;

这是每个搜索按钮的代码

Here is the code for each search button

    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中进行所需的搜索

Now we do the desired Search in 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,则不会显示新记录 我们正在为这个Stay Tuned做准备哈哈,好工作James_Duh

The only stumble with this is that if if you do a search by date and do an add to the DB and jump back to the ListActivity the new record is not displayed We are working on this Stay Tuned ha ha Good Job James_Duh

这篇关于RecyclerAdapter设置适配器位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆