当项目单击另一个时,更改recyclerview中的项目

时间:2016-07-30 12:29:49

标签: android android-recyclerview recycler-adapter

我正在创建一个应用,其中一个布局中有2个invokedynamic。我为类别设置了一个,为类别设置了第二个文本。当我调试我的代码,让我在recyclerview中显示null值在适配器的这一行../看到图像

我想选择类别项,然后给出包含该类别的文本。

RecyclerView

HorizantalAdapter

recyclerView.setAdapter(adapter);

VerticalAdapter

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private List<TextJson> listtext;
    private List<CategoryJson> horizentallylist;
    private RecyclerView recyclerView , horizantalrcview;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;

    String textUrl = "http:www.extample.com/texturl.php";
    String categoryUrl ="http:www.extample.com/categoryurl.php";
    private static final String TAG = MainActivity.class.getSimpleName();
    private JSONArray result;
    ArrayAdapter<String> adapterSpin;
    CategoryJson categoryJson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_text);
        horizantalrcview = (RecyclerView)findViewById(R.id.recyclerView_cate);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        listtext = new ArrayList<>();
        horizentallylist = new ArrayList<>();
        getData();
        verticalData();
    }

    private void getData() {
        final ProgressDialog loading = ProgressDialog.show(this, "Loading Data", "Please wait...", false, false);
        loading.setCancelable(true);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(textUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.e("text", "resopnce is =>" + response);
                        loading.dismiss();
                        verticalParse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

    private void verticalParse(JSONArray array) {

        for (int i = 0; i < array.length(); i++) {
            TextJson jsonitem = new TextJson();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                String text = json.getString("txt");
                String txtcate = json.getString("category");
                jsonitem.setText_status(text);
                jsonitem.setCategory(txtcate);
                Log.e("text", "is==>  " + text);
                Log.e("cate", "is==>  " + txtcate);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            listtext.add(jsonitem);
        }

        //Finally initializing our adapter
        adapter = new VerticalAdapter(listtext, this);
        adapter.notifyDataSetChanged();
        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }



    private void verticalData() {
        final ProgressDialog loading = ProgressDialog.show(this, "Loading Data", "Please wait...", false, false);
        loading.setCancelable(true);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(categoryUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.e("Response is", "first==>" + response);
                        loading.dismiss();
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error instanceof NoConnectionError) {
                            Log.e("NoConnectionError>>", "NoConnectionError....");
                        } else if (error instanceof AuthFailureError) {
                            Log.e("AuthFailureError>>>", "AuthFailureError...");
                        } else if (error instanceof ServerError) {
                            Log.e("ServerError>>>>>>>>>", "ServerError.......");
                        } else if (error instanceof NetworkError) {
                            Log.e("NetworkError>>>>>>>>>", "NetworkError.......");
                        } else if (error instanceof ParseError) {
                            Log.e("ParseError>>>>>>>>>", "ParseError.......");
                        } else if (error instanceof TimeoutError) {
                            Log.d("TimeoutError>>>>>>>>>", "TimeoutError.......");
                        }
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            CategoryJson model1 = new CategoryJson();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                String imgurl = json.getString("cat_img");
                String imgname = json.getString("category");
                Log.e("value in","link=>"+imgurl);
                Log.e("value in","imagename=>"+imgname);
                model1.setImageUrl(imgurl);
                model1.setName(imgname);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            horizentallylist.add(model1);
        }
        //Finally initializing our adapter
        adapter = new HorizantalAdapter(horizentallylist, this);
        adapter.notifyDataSetChanged();
        //Adding adapter to recyclerview
        horizantalrcview.setAdapter(adapter);
    }


    private void selectData() {
        NewModel newModel = new NewModel();
        final ProgressDialog loading = ProgressDialog.show(this, "Loading Data", "Please wait...", false, false);
        loading.setCancelable(true);
        String txtselect =newModel.getItem().toString();
        String select_cate ="http:www.extample.com/filter.php"+selectitem;

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(select_cate ,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.e("text", "resopnce is =>" + response);
                        loading.dismiss();
                        selectParse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

    private void selectParse(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            TextJson jsonitem = new TextJson();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                String text = json.getString("txt");
                String txtcate = json.getString("category");
                jsonitem.setText_status(text);
                jsonitem.setCategory(txtcate);
                Log.e("text", "is==>  " + text);
                Log.e("cate", "is==>  " + txtcate);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            listtext.add(jsonitem);
        }
        //Finally initializing our adapter
        adapter = new VerticalAdapter(listtext, this);
        adapter.notifyDataSetChanged();
        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }
}

日志

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

    private ImageLoader imageLoader;
    private Context context;
    List<CategoryJson> listmodel;
    NewModel newModel = new NewModel();
    String link;

    public HorizantalAdapter(List<CategoryJson> listmodel, Context context) {
        super();
        this.listmodel = listmodel;
        this.context = context;
        setHasStableIds(false);
    }

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

    @Override
    public void onBindViewHolder(final HorizantalAdapter.ViewHolder holder, final int position) {
        CategoryJson imgModel = listmodel.get(position);
        imageLoader =CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(imgModel.getImageUrl(),imageLoader.getImageListener(holder.imageView,R.drawable.placeholder,android.R.drawable.ic_dialog_alert));
        holder.imageView.setImageUrl(imgModel.getImageUrl(),imageLoader);
        holder.textViewName.setText(imgModel.getName());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                link = listmodel.get(position).getName();
                newModel.setItem(link);
                Toast.makeText(context, "select item select"  +link, Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return listmodel.size();
    }

    public long getItemId(int position) {
        return position;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView imageView;
        public TextView textViewName;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageView_img);
            imageView.setScaleType(NetworkImageView.ScaleType.FIT_CENTER);
            textViewName = (TextView) itemView.findViewById(R.id.text_cate);


             }
        }
     }

图片

enter image description here

调试后

debug app

1 个答案:

答案 0 :(得分:0)

这是一个广泛的问题,所以我可以建议一些基本的方法,即将click侦听器传递给适配器的构造函数。

public class HorizantalAdapter extends RecyclerView.Adapter<HorizantalAdapter.ViewHolder> 

    /*** Store a reference ***/
    private View.OnClickListener clickListener; 

    /*** Add that reference ***/
    public HorizantalAdapter(List<CategoryJson> listmodel, Context context, View.OnClickListener clickListener) {
        super();
        this.listmodel = listmodel;
        this.context = context;
        setHasStableIds(false);

        /*** Set the reference ***/
        this.clickListener = clickListener; 
    }

    .......

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

        /*** Use the reference ***/
        holder.imageView.setOnClickListener(clickListener); 
    }

然后在Activity中设置水平适配器

private void parseData(JSONArray array) {
    .........

    /*** Define the listener ***/
    final View.OnClickListener horizontalClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            link = listmodel.get(position).getName();
            newModel.setItem(link);
            Toast.makeText(context, "select item select"  +link, Toast.LENGTH_SHORT).show();

            // TODO: Make Volley request for Categories here...
            // Should be able to set other adapter in onResponse there
        }
    };

    /*** Pass in the listener reference ***/
    adapter = new HorizantalAdapter(horizentallylist, this, horizontalClickListener);
}

作为对编辑的回应,消息来自这些行

Log.e("text", "is==>  " + text);
Log.e("cate", "is==>  " + txtcate);