我已根据ListView
调用的数据数据设置了ArrayList<ArrayList<String>>
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
文本以及与{{1}位于同一行的dayTextView文本点击了。
现在我不想为每个TextView
设置一堆onClickListners
,那么对所有TextView
执行此操作的高效方式是什么?在我TextView
的行中?
答案 0 :(得分:1)
选项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
快乐编码:)
答案 1 :(得分:0)
您可以在convertView中查找TextView实例的任何子项。此方法使用广度优先搜索算法搜索TextView实例。
private void setOnClickListenerOnTextViews(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));
}
}
}
}
在getView()方法中调用此方法:
public View getView(.....){
//...
setOnClickListenerOnTextViews(convertView,mClickListener)
return convertView;
}
编辑:为了复制和粘贴解决方案,mClickListener
是代码中某处的侦听器实例:
private View.OnClickListener mClickListener = new View.OnclickListerner(){
@Override
public void onClick(View v){
//do your thing here.
}
};
答案 2 :(得分:0)
最有效的方法是为每个TextView
手动设置点击监听器。为什么?因为你已经找到它们(findViewById)
所以再次遍历视图树真是个坏主意(Breadth First Search)
。所以你所能做的就是创建一个函数,然后将TextViews
传递给它。你可以做一些重构来获得干净的代码。
public void addClickListenerToTextView(TextView... textViewArray){
final TextView[] finalTextViewArray = textViewArray;
for(int i = 0 ; i < finalTextViewArray.length-1 ; ++i){
final int index = i;
textViewArray[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, YourTargetActivity.class);
intent.putExtra("meal" + index ,finalTextViewArray[index].getText().toString());
intent.putExtra("dayText" , finalTextViewArray[finalTextViewArray.length-1].getText().toString());
startActivity(intent);
}
});
}
}
然后你可以像这样调用这个函数:
addClickListenerToTextView(mealOne_Three,mealTwo_Three,mealThree_Three,dayText_Three);
因此,在每次休息之前,请使用该行的所有textview调用上述函数。请注意,您必须始终将day textview作为最后一个textview。
答案 3 :(得分:0)
您可以执行类似通用OnClickListener
的内容,其中包含新Activity
所需的所有信息:
public static final String[] weekdayStringArray = DateFormatSymbols.getInstance().getWeekdays();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ArrayList<String> stringArrayList = getItem(position);
String dayString = weekdayStringArray[dayNumber];
MyClickListener myClicker = new MyClickListener(dayString);
switch (stringArrayList.size()) {
case 4:
convertView = LayoutInflater.from(parent.getContext()).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());
applyClickListeners(myClicker, mealOne_Four, mealTwo_Four, mealThree_Four, mealFour_Four, dayText_Four);
break;
case 3:
convertView = LayoutInflater.from(parent.getContext()).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());
applyClickListeners(myClicker, mealOne_Three, mealTwo_Three, mealThree_Three, dayText_Three);
break;
case 2:
convertView = LayoutInflater.from(parent.getContext()).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());
applyClickListeners(myClicker, mealOne_Two, mealTwo_Two, dayText_Two);
break;
case 1:
convertView = LayoutInflater.from(parent.getContext()).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());
applyClickListeners(myClicker, mealOne_One, dayText_One);
break;
case 0:
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.zero_meal_item_view, parent, false);
TextView dayText_Zero = (TextView) convertView.findViewById(R.id.dayTextView_Zero);
dayText_Zero.setText(dayString + "(Closed)");
applyClickListeners(myClicker, dayText_Zero);
break;
}
return convertView;
}
private void applyClickListeners(MyClickListener clicker, View... toApply) {
for (View view : toApply) {
view.setOnClickListener(clicker);
}
}
class MyClickListener implements View.OnClickListener {
private String dayText;
public MyClickListener(String dayText) {
this.dayText = dayText;
}
@Override
public void onClick(View v) {
// do your thing
}
}
答案 4 :(得分:0)
在Activity上实现Listener,然后将活动作为侦听器传递给每个可单击的项目。这样你只需要创建一个监听器,它不会泄漏内存,如果你必须改变项目的显示方式,你就不必改变每一个监听器。