2017-01-27 56 views
1

我想要TextView的值當我點擊RecyclerView內的項目的textview時。我不希望整個項目可點擊,我只需要在下面的代碼中的「countNumber」項目clickable單個文本視圖。當我點擊它我想獲得countNumber顯示的文本數據。在Recycler視圖內獲得TextView的項目點擊值?

回收站適配器

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> { 

    ArrayList<UserData> dataSet = new ArrayList<>(); 

    public CustomAdapter(ArrayList<UserData> data) { 
     this.dataSet = data; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, 
              int viewType) { 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_layout, parent, false); 


     MyViewHolder myViewHolder = new MyViewHolder(view); 
     return myViewHolder; 
    } 

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

     holder.heading.setText(dataSet.get(listPosition).getHeading()); 
     holder.description.setText(dataSet.get(listPosition).getDescription()); 
     holder.countNumber.setText(String.valueOf(dataSet.get(listPosition).getCount())); 
     holder.time.setText(dataSet.get(listPosition).getTime()); 
     holder.date.setText(dataSet.get(listPosition).getDate()); 
     holder.place.setText(dataSet.get(listPosition).getPlace()); 


    } 

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

    public void countIncrease() { 

    } 

    public List<UserData> getList() { 
     return this.dataSet; 
    } 

    public static class MyViewHolder extends RecyclerView.ViewHolder { 


     TextView heading; 
     TextView description; 
     TextView countNumber; 
     TextView time; 
     TextView date; 
     TextView place; 

     public MyViewHolder(final View itemView) { 
      super(itemView); 

      this.countNumber = (TextView) itemView.findViewById(R.id.countId); 
      this.heading = (TextView) itemView.findViewById(R.id.textViewName); 
      this.description = (TextView) itemView.findViewById(R.id.textViewVersion); 
      this.place = (TextView) itemView.findViewById(R.id.placeId); 
      this.time = (TextView) itemView.findViewById(R.id.timeId); 
      this.date = (TextView) itemView.findViewById(R.id.dateId); 
      this.countNumber.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
         //i need counterNumber value here 
        Log.v("loo", "Click"); 
       } 
      }); 
     } 
    } 
} 

MainActivity

public class MainActivity extends AppCompatActivity { 

    DBHelper dbHelper; 

    CustomAdapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    private static RecyclerView recyclerView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     dbHelper = DBHelper.getInstance(getApplicationContext()); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       addButton(); 
      } 
     }); 



     recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true); 

     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 

     adapter = new CustomAdapter(dbHelper.getAllIncidents()); 
     recyclerView.setAdapter(adapter); 

    } 




    public void addButton(){ 
     Intent addButtonIndent = new Intent(this,AddData.class); 
     startActivity(addButtonIndent); 
    } 


} 
+0

你能得到你的onClick內的文本():在你的適配器(代碼註釋)首先創建接口:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> { ArrayList<UserData> dataSet = new ArrayList<>(); public CustomAdapter(ArrayList<UserData> data) { this.dataSet = data; } // Define listener member variable private static OnRecyclerViewItemClickListener mListener; // Define the listener interface public interface OnRecyclerViewItemClickListener { void onItemClicked(String text); } // Define the method that allows the parent activity or fragment to define the listener. public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener listener) { this.mListener = listener; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ... } @Override public void onBindViewHolder(final MyViewHolder holder, final int listPosition) { ... } ... public static class MyViewHolder extends RecyclerView.ViewHolder { ... public MyViewHolder(final View itemView) { super(itemView); ... this.countNumber.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // send the text to the listener, i.e Activity. mListener.onItemClicked(((TextView)v).getText()); } }); } } ... } 

然後實現在您的活動接口接收文本((TextView的)v)的.getText(); –

回答

3

如果你想從進去適配器的,如@拉梅什-R說,你可以得到它與((TextView)v).getText();

this.countNumber.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ((TextView)v).getText(); 
      } 
     }); 

但是,如果你想獲得價值從活動,您需要爲它創建和實現一個接口。使用

adapter = new CustomAdapter(dbHelper.getAllIncidents()); 

// Interface implementation. 
adapter.setOnRecyclerViewItemClickListener(new YourAdapter.OnRecyclerViewItemClickListener() { 
    @Override 
    public void onItemClicked(String text) { 
    Log.d(TAG, "Text is = " + text); 
    } 
}); 

recyclerView.setAdapter(adapter); 
+0

你做了我的一天,非常感謝! –

0

試試這個下面的代碼

this.countNumber.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String value=((TextView)v).getText(); 
       //i need counterNumber value here 
      Log.v("loo", value); 
     } 
    }); 
1

試試這個代碼

countNumberfinal

final this.countNumber = (TextView) itemView.findViewById(R.id.countId); 

而且,從TextView獲得的價值是countNumber

this.countNumber.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       //i need counterNumber value here 
      String value = this.countNumber.getText().toString().trim(); 
      Log.v("loo", "countNumber=="+value); 
     } 
    }); 
相關問題