Android无法获得recyclerview点击的项目

时间:2017-01-14 08:00:16

标签: android android-recyclerview

我有一个包含EditText的片段,当输入特定字符串时,编程创建的RecyclerView直接显示在EditText下面。单击该项时,将使用SharedPreference保存它。但是我无法访问片段中的项目。 Java代码:

HomePageFrag:

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Toast.makeText(getContext(),"textchanged",Toast.LENGTH_SHORT).show();
              if(charSequence.toString().equals("Taxi"))
              {
                  RelativeLayout.LayoutParams layoutParams1=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                  layoutParams1.setMargins(40,0,40,0);
                  recyclerView.setAdapter(new CustomListAdapter(getContext()));
                  layoutParams1.addRule(RelativeLayout.BELOW,R.id.srch_qry);
                  recyclerView.setLayoutParams(layoutParams1);
                  recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                  relativeLayout.addView(recyclerView);
                  recyclerView.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          sharedPreferences=getContext().getSharedPreferences("Clck", Context.MODE_PRIVATE);
                          if(sharedPreferences.contains("Clck")) {
                              clicked_itm=sharedPreferences.getString("itm", null);
                              Log.e("Item",clicked_itm);
                              if(clicked_itm!=null){
                                  relativeLayout.removeView(recyclerView);
                              }
                          }

                      }
                  });
                  Toast.makeText(getContext(),"Taxi",Toast.LENGTH_SHORT).show();

              }
                else {
                  relativeLayout.removeView(recyclerView);
              }
            }
            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

CustomListAdapter:

public class CustomListAdapter extends RecyclerView.Adapter<CustomListAdapter.Holder> {
    public String[] taxi_list={"Audi","Tavera","Chevrolet"};
    public Context context;
    public CustomListAdapter(Context context){
        this.context=context;
    }
    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cstm_lst_lyt,parent,false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
    holder.taxi_text.setText(taxi_list[position]);
    }

    @Override
    public int getItemCount() {
        return taxi_list.length;
    }

    public class Holder extends RecyclerView.ViewHolder{
         TextView taxi_text;
        public Holder(View itemView) {
            super(itemView);
            taxi_text=(TextView) itemView.findViewById(R.id.taxi_txt);
            taxi_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("Clck","Yes");
                    SharedPreferences sharedPreferences=context.getSharedPreferences("Clck",Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("itm",taxi_text.getText().toString());
                    editor.commit();
                }
            });
        }

    }
}

XML:

cstm_lst_lyt:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#40000000"
    android:id="@+id/lyt">
<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/taxi_txt"
    android:textSize="20sp"
    android:text="Audi"
    android:paddingLeft="10dp"
    android:gravity="center|start"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您需要在CustomAdapter类中实现一个侦听器,在这种情况下,HomeFrag类将是侦听器。例如:

1.在CustomAdapter中,您将创建一个将成为监听器的接口:

public interface RowClickListener{
    void onRowClick(String whateverTextYouNeedToPassToHomeFrag);
}

2.在Custom Adapter构造函数中,您将传递一个侦听器对象,并将它放在该类型的字段中:

public CustomAdapter(param,param...,RowClickListener listener){
   this.listener = listener;
}
  1. 在onClick的Holder类中,您将调用侦听器的onRowClick方法:

    public void onClick(View view){
        String text;
        listener.onRowClick(text);
    }
    
  2. 4.然后在创建适配器时,在HomeFrag类中,您可以在构造函数中传递匿名类:

    new CustomAdapter(arg,arg...,new CustomAdapter.RowClickListener(){
        @Override
        public void onRowClick(String textYouNeed){
            //do something with text
    
    }
    

    4.b或者你可以做HomeFrag实现CustomAdapter.RowClickListener然后只做:

    new CustomAdapter(arg,arg...,this);
    

答案 1 :(得分:0)

由于我无法在Fragment中获取单击的数据,因此我在CustomListAdapter的构造函数中传递了EditText对象,并在Holder类的构造函数中设置了EditText的文本。 HomePageFrag.java:

 recyclerView.setAdapter(new CustomListAdapter(getContext(),editText));

CustomListAdapter.java:

public CustomListAdapter(Context context,EditText editText){
this.editText=editText;
}
 public Holder(View itemView) {
            super(itemView);
 taxi_text=(TextView) itemView.findViewById(R.id.taxi_txt);
            taxi_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
editText.setText(taxi_text.getText().toString());
}