为ListView的多行中的多个TextView设置onClickListener [英] Set onClickListener for multiple TextView's in multiple rows of a ListView
问题描述
我已经从ArrayList<ArrayList<String>>
设置了一个名为ListView
的数据,该数据看起来像这样
data = {{"cat1","cat2","cat3","cat4"},
{"dog1","dog2","dog3"},
{"lion1"},
{"monkey1","monkey2"}};
我将其设置如下:
public static final String[] weekdayStringArray = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ArrayList<String> stringArrayList = data.get(position);
String dayString = weekdayStringArray[dayNumber];
switch(stringArrayList.size()){
case 4:
convertView = mLayoutInflater.inflate(R.layout.four_meal_item_view, parent, false);
TextView mealOne_Four = (TextView) convertView.findViewById(R.id.firstMealTextView);
TextView mealTwo_Four = (TextView) convertView.findViewById(R.id.secondMealTextView);
TextView mealThree_Four = (TextView) convertView.findViewById(R.id.thirdMealTextView);
TextView mealFour_Four = (TextView) convertView.findViewById(R.id.fourthMealTextView);
TextView dayText_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());
break;
case 3:
convertView = mLayoutInflater.inflate(R.layout.three_meal_item_view, parent, false);
TextView mealOne_Three = (TextView) convertView.findViewById(R.id.firstMealTextView_Three);
TextView mealTwo_Three = (TextView) convertView.findViewById(R.id.secondMealTextView_Three);
TextView mealThree_Three = (TextView) convertView.findViewById(R.id.thirdMealTextView_Three);
TextView dayText_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());
break;
case 2:
convertView = mLayoutInflater.inflate(R.layout.two_meal_item_view, parent, false);
TextView mealOne_Two = (TextView) convertView.findViewById(R.id.firstMealTextView_Two);
TextView mealTwo_Two = (TextView) convertView.findViewById(R.id.secondMealTextView_Two);
TextView dayText_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());
break;
case 1:
convertView = mLayoutInflater.inflate(R.layout.one_meal_item_view, parent, false);
TextView mealOne_One = (TextView) convertView.findViewById(R.id.firstMealTextView_One);
TextView dayText_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());
break;
case 0:
convertView = mLayoutInflater.inflate(R.layout.zero_meal_item_view, parent, false);
TextView dayText_Zero = (TextView) convertView.findViewById(R.id.dayTextView_Zero);
dayText_Zero.setText(dayString + "(Closed)");
break;
}
return convertView;
现在我想添加一个onClickListener
,以便可以连续单击一个TextView
.当单击此TextView
时,我将移至新的Activity
,并传递所单击的TextView
的文本和与所单击的TextView
在同一行的dayTextView的文本.>
现在,我不想为每个TextView
设置一堆onClickListners
,那么对我行中的所有TextView
执行此操作的有效方法是什么? ListView
?
选项1:
在每个textView的xml中,将onClick设置为相同的方法
android:onClick= "itemtapped"
然后在您的活动中点击呼叫项,并让其获得textview名称和位置编号,这样您就可以知道哪一个被点击了.
选项2:
在接受所窃取物品的活动中进行自定义功能
类似这样的东西
public void setOnClickListener(TextView... arrayOfView, int position){
TextView[] ]textViewArray = textViewArray;
for(int i = 0 ; i < textViewArray.length ; ++i){
int index = i;
textViewArray[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, YourTargetActivity.class);
startActivity(intent);
int textViewNumber = position;
}
});
}
然后传递位置,这样您就知道它是什么,并且知道该行中的textview
快乐编码:)
I have set up a ListView
from an ArrayList<ArrayList<String>>
called data of data that looks like this
data = {{"cat1","cat2","cat3","cat4"},
{"dog1","dog2","dog3"},
{"lion1"},
{"monkey1","monkey2"}};
And I have set it up like this:
public static final String[] weekdayStringArray = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ArrayList<String> stringArrayList = data.get(position);
String dayString = weekdayStringArray[dayNumber];
switch(stringArrayList.size()){
case 4:
convertView = mLayoutInflater.inflate(R.layout.four_meal_item_view, parent, false);
TextView mealOne_Four = (TextView) convertView.findViewById(R.id.firstMealTextView);
TextView mealTwo_Four = (TextView) convertView.findViewById(R.id.secondMealTextView);
TextView mealThree_Four = (TextView) convertView.findViewById(R.id.thirdMealTextView);
TextView mealFour_Four = (TextView) convertView.findViewById(R.id.fourthMealTextView);
TextView dayText_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());
break;
case 3:
convertView = mLayoutInflater.inflate(R.layout.three_meal_item_view, parent, false);
TextView mealOne_Three = (TextView) convertView.findViewById(R.id.firstMealTextView_Three);
TextView mealTwo_Three = (TextView) convertView.findViewById(R.id.secondMealTextView_Three);
TextView mealThree_Three = (TextView) convertView.findViewById(R.id.thirdMealTextView_Three);
TextView dayText_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());
break;
case 2:
convertView = mLayoutInflater.inflate(R.layout.two_meal_item_view, parent, false);
TextView mealOne_Two = (TextView) convertView.findViewById(R.id.firstMealTextView_Two);
TextView mealTwo_Two = (TextView) convertView.findViewById(R.id.secondMealTextView_Two);
TextView dayText_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());
break;
case 1:
convertView = mLayoutInflater.inflate(R.layout.one_meal_item_view, parent, false);
TextView mealOne_One = (TextView) convertView.findViewById(R.id.firstMealTextView_One);
TextView dayText_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());
break;
case 0:
convertView = mLayoutInflater.inflate(R.layout.zero_meal_item_view, parent, false);
TextView dayText_Zero = (TextView) convertView.findViewById(R.id.dayTextView_Zero);
dayText_Zero.setText(dayString + "(Closed)");
break;
}
return convertView;
And now I want to add an onClickListener
so that I can click a TextView
in a row. And when this TextView
is clicked I move to a new Activity
passing the text of the TextView
that was clicked and the text of the dayTextView that was in the same row as the TextView
that was clicked.
Now I don't want to set a bunch of onClickListners
for each TextView
so what is an efficient way of doing this for all the TextView
s in the rows of my ListView
?
Option 1:
In the xml for each textView set the onClick to the same method
android:onClick= "itemtapped"
Then call item tapped in your activity and have it get the textview name and position number so you know which one was tapped.
Option 2:
Make a custom funciton in the activity that takes the item that was tapped
Something like this
public void setOnClickListener(TextView... arrayOfView, int position){
TextView[] ]textViewArray = textViewArray;
for(int i = 0 ; i < textViewArray.length ; ++i){
int index = i;
textViewArray[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, YourTargetActivity.class);
startActivity(intent);
int textViewNumber = position;
}
});
}
And then pass in the position so you know what one it is and the textview in that row
Happy Coding :)
这篇关于为ListView的多行中的多个TextView设置onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!