我有一个多行的ListView,每行有多个textViews,现在为每一个设置一个onClickListener会很不方便和编程错误。
如何为所有TextView设置onClickListener,因为每个TextView都会执行相同的意图(它会转到相同的下一个活动)。但每个人都将其文本作为该意图的额外内容传递。
那么如何在Click Clickineres上设置TextViews呢?
感谢您的帮助。
修改
好的抱歉,我可能没有完全解释这个我已经设置的ListView。
我有5种不同类型的细胞。一个有4个textViews,一个有3个textViews,依此类推,直到0。
在我的getView中,我设置了这样的
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;
没有点击textView我想去一个新的活动并传递被点击的TextView的文本和该行的dayTextView的文本。
我希望这能更好地解释它。
答案 0 :(得分:0)
您应该在onClickListener
TextView
方法的每个Adapter
中添加getView
。
编辑:
为列表中的每个onClickListener
设置TextView
。然后在onClick
方法中:
@Override
public void onClick(View view) {
Intent i = new Intent(context, MyActivity.class);
i.putExtra("text", ((TextView) view).getText.toString());
context.startActivity(this);
}
答案 1 :(得分:0)
在 getView 方法中创建视图时,您可以为适配器中的每个TextView创建 OnClickListener 。
还有第二种选择。使用一个全局 OnClickListener 和....
public void onClick(View view) {
if(view instanceof YourFunkyCustomTextViewWithData)
{
((YourFunkyCustomTextViewWithData) view).getFunkyData()
... and do what you want with that
}
}