如何在其他活动中访问onbindviewholder()适配器类的位置变量

时间:2017-11-20 09:57:52

标签: android android-recyclerview android-adapter

我想在回收站视图中使用改装来从api获取数据我只显示商店名称和时间当我点击其他活动中的项目我必须显示我想要的位置变量的商店的完整细节获取我选择的位置数据

      @Override
            public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

                TextView textViewName = holder.textViewName;
                TextView textViewVersion = holder.textViewTime;
                textViewName.setText(dataSet.get(listPosition).getShop_name());
                textViewVersion.setText(dataSet.get(listPosition).getShop_time());
}

私有类MyOnClickListener实现了View.OnClickListener {

    private final Context context;

    private MyOnClickListener(Context context) {
        this.context = context;
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(MainActivity.this,ViewMoreActivity.class);
        startActivity(intent);
    }
    }
    public void fetchingData()
    {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please Wait..");
        progressDialog.setCancelable(false);
        progressDialog.show();

        Retrofit adapter = new Retrofit.Builder()
                .baseUrl(Constandapi.ROOT_URL)
                .build();

        ShopDetailsInterface report = adapter.create(ShopDetailsInterface.class);

        Call<ResponseBody> post_register;

        post_register = report.post_register("gow@gmail.com",
                "202cb962ac59075b964b07152d234b70","tw_puncture");
        post_register.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {

                    try {

                        String result = response.body().string();

                        Log.d("resultdisplayLogin", "***   " + result);

                        JSONArray jsonArray=new JSONArray(result);
                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            DataModel dataModel = new DataModel();

                            dataModel.setShop_name(jsonObject.getString("shop_name"));
                            dataModel.setShop_time(jsonObject.getString("shop_time"));
                            data.add(dataModel);
                            progressDialog.dismiss();
                            Log.d("datafetched", data+"");
                        }

                        detailadapter.notifyDataSetChanged();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }else
                {
                    progressDialog.dismiss();
                   Toast.makeText(getApplicationContext(),"try Again........",Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }

2 个答案:

答案 0 :(得分:0)

  

如何在其他活动中访问onbindviewholder()适配器类的位置变量

假设您的Recyclerview有一个viewHolder MyViewHolder

class MyViewHolder extends ViewHolder implements View.OnClickListener {
    // Assuming your row layout is having parent having id parent
    public MyViewHolder (View v) {
        super(v);
        parent = v.findViewByID(R.id.parentID);
        parent.setOnClickListener(this);
    }

    @Override
    public void OnClick(View v) {
        int position = getAdapterPosition(); // Use this position 
    }
}

答案 1 :(得分:0)

使用接口。它应该包含一个类似 void onItemClick(int position);

的方法