Set Onclicklistener For Multiple Textview's In Multiple Rows Of A Listview
Solution 1:
You can look for any child of TextView instance in your convertView. This method uses Breadth First Search algorithm to search for TextView instance.
privatevoidsetOnClickListenerOnTextViews(View contentViev, OnClickListener listener) {
ArrayList<View> unvisited = new ArrayList<View>();
unvisited.add(contentView);
while (!unvisited.isEmpty()) {
View child = unvisited.remove(0);
if(child instanceof TextView) {
child.setOnClickListener(listener);
continue;
}
if (!(child instanceof ViewGroup)){
continue;
}
ViewGroup group = (ViewGroup) child;
final int childCount = group.getChildCount();
for (int i=0; i< childCount; i++){
unvisited.add(group.getChildAt(i));
}
}
}
}
Call this method in your getView() method:
public View getView(.....){
//...setOnClickListenerOnTextViews(convertView,mClickListener)
return convertView;
}
EDIT: For the sake of copy and paste solution, mClickListener
is listener instance somewhere in your code:
private View.OnClickListenermClickListener=newView.OnclickListerner(){
@OverridepublicvoidonClick(View v){
//do your thing here.
}
};
Solution 2:
The most efficient way is manually setting click listener for each TextView
. Why? because you already have found them (findViewById)
so again traversing the view tree is really bad idea(Breadth First Search)
. So all you can do is creating a function and then passing your TextViews
to it. you can do some refactoring to have a clean code though.
publicvoidaddClickListenerToTextView(TextView... textViewArray){
final TextView[] finalTextViewArray = textViewArray;
for(inti=0 ; i < finalTextViewArray.length-1 ; ++i){
finalintindex= i;
textViewArray[i].setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(View v){
Intentintent=newIntent(MainActivity.this, YourTargetActivity.class);
intent.putExtra("meal" + index ,finalTextViewArray[index].getText().toString());
intent.putExtra("dayText" , finalTextViewArray[finalTextViewArray.length-1].getText().toString());
startActivity(intent);
}
});
}
}
then you can call this function like this:
addClickListenerToTextView(mealOne_Three,mealTwo_Three,mealThree_Three,dayText_Three);
so before each break call the above function with all the textviews for that row. Note that you must always pass day textview as the last textview.
Solution 3:
You can do something like a generic OnClickListener
that holds all the information you need for the new Activity
:
publicstaticfinal String[] weekdayStringArray = DateFormatSymbols.getInstance().getWeekdays();
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
ArrayList<String> stringArrayList = getItem(position);
StringdayString= weekdayStringArray[dayNumber];
MyClickListenermyClicker=newMyClickListener(dayString);
switch (stringArrayList.size()) {
case4:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.four_meal_item_view, parent, false);
TextViewmealOne_Four= (TextView) convertView.findViewById(R.id.firstMealTextView);
TextViewmealTwo_Four= (TextView) convertView.findViewById(R.id.secondMealTextView);
TextViewmealThree_Four= (TextView) convertView.findViewById(R.id.thirdMealTextView);
TextViewmealFour_Four= (TextView) convertView.findViewById(R.id.fourthMealTextView);
TextViewdayText_Four= (TextView) convertView.findViewById(R.id.dayTextView);
dayText_Four.setText(dayString);
mealOne_Four.setText(stringArrayList.get(0).substring(0, 1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
mealTwo_Four.setText(stringArrayList.get(1).substring(0, 1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
mealThree_Four
.setText(stringArrayList.get(2).substring(0, 1).toUpperCase() + stringArrayList.get(2).substring(1).toLowerCase());
mealFour_Four.setText(stringArrayList.get(3).substring(0, 1).toUpperCase() + stringArrayList.get(3).substring(1).toLowerCase());
applyClickListeners(myClicker, mealOne_Four, mealTwo_Four, mealThree_Four, mealFour_Four, dayText_Four);
break;
case3:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.three_meal_item_view, parent, false);
TextViewmealOne_Three= (TextView) convertView.findViewById(R.id.firstMealTextView_Three);
TextViewmealTwo_Three= (TextView) convertView.findViewById(R.id.secondMealTextView_Three);
TextViewmealThree_Three= (TextView) convertView.findViewById(R.id.thirdMealTextView_Three);
TextViewdayText_Three= (TextView) convertView.findViewById(R.id.dayTextView_Three);
dayText_Three.setText(dayString);
mealOne_Three.setText(stringArrayList.get(0).substring(0, 1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
mealTwo_Three.setText(stringArrayList.get(1).substring(0, 1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
mealThree_Three.setText(stringArrayList.get(2).substring(0, 1).toUpperCase()
+ stringArrayList.get(2).substring(1).toLowerCase());
applyClickListeners(myClicker, mealOne_Three, mealTwo_Three, mealThree_Three, dayText_Three);
break;
case2:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.two_meal_item_view, parent, false);
TextViewmealOne_Two= (TextView) convertView.findViewById(R.id.firstMealTextView_Two);
TextViewmealTwo_Two= (TextView) convertView.findViewById(R.id.secondMealTextView_Two);
TextViewdayText_Two= (TextView) convertView.findViewById(R.id.dayTextView_Two);
dayText_Two.setText(dayString);
mealOne_Two.setText(stringArrayList.get(0).substring(0, 1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
mealTwo_Two.setText(stringArrayList.get(1).substring(0, 1).toUpperCase() + stringArrayList.get(1).substring(1).toLowerCase());
applyClickListeners(myClicker, mealOne_Two, mealTwo_Two, dayText_Two);
break;
case1:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_meal_item_view, parent, false);
TextViewmealOne_One= (TextView) convertView.findViewById(R.id.firstMealTextView_One);
TextViewdayText_One= (TextView) convertView.findViewById(R.id.dayTextView_One);
dayText_One.setText(dayString);
mealOne_One.setText(stringArrayList.get(0).substring(0, 1).toUpperCase() + stringArrayList.get(0).substring(1).toLowerCase());
applyClickListeners(myClicker, mealOne_One, dayText_One);
break;
case0:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.zero_meal_item_view, parent, false);
TextViewdayText_Zero= (TextView) convertView.findViewById(R.id.dayTextView_Zero);
dayText_Zero.setText(dayString + "(Closed)");
applyClickListeners(myClicker, dayText_Zero);
break;
}
return convertView;
}
privatevoidapplyClickListeners(MyClickListener clicker, View... toApply) {
for (View view : toApply) {
view.setOnClickListener(clicker);
}
}
classMyClickListenerimplementsView.OnClickListener {
private String dayText;
publicMyClickListener(String dayText) {
this.dayText = dayText;
}
@OverridepublicvoidonClick(View v) {
// do your thing
}
}
Solution 4:
Implement the Listener on your Activity, then pass the activity as the listener to every clickable item. This way you only have to create one listener, it doesn't leak memory and if you have to change the way items are displayed you don't have to change every single listener.
- Create a SoftReference in the adapter to store the activity safely
- Pass the activity to every textview as the listener
- implements View.OnClickListener in the activity
- Cast the view you're passed back to a TextView
- Create a new intent
- Get the text, attach it to the intent
- Start the new activity
Post a Comment for "Set Onclicklistener For Multiple Textview's In Multiple Rows Of A Listview"