设置onClickListener对于多行的ListView多TextViews [英] Set onClickListener For Multiple TextViews in Multiple Rows ListView

查看:145
本文介绍了设置onClickListener对于多行的ListView多TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个ListView从一个ArrayList>叫,看起来像这样的数据的数据

I have set up a ListView from an ArrayList> called data of data that looks like this

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的点击我移动到通过单击的TextView中的文本和是同一行被点击的TextView的在dayTextView的文本的新活动。

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.

现在我不想设置了一堆onClickListners的每个TextView的那么什么是做这行的行所有的TextView的有效方法。

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 in the rows.

感谢您提前帮助。

推荐答案

选项1:

在的XML每个TextView的onClick的设置为相同的方法

In the xml for each textView set the onClick to the same method

android:onClick= "itemtapped"

然后调用您的活动窃听项目,并把它拿到TextView的名称和位置号码,以便您知道哪一个被窃听。

Then call item tapped in your activity and have it get the textview name and position number so you know which one was tapped.

选项2:

请在活动的自定义功能可按这需要被窃听项目

Make a custom funciton in the activity that takes the item that was tapped

像这样

       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

And then pass in the position so you know what one it is and the textview in that row

编码快乐:)

这篇关于设置onClickListener对于多行的ListView多TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆