TextView 更改适用于使用 ListView 和 ArrayAdapter 的多个 TextView [英] A TextView change applies to multiple TextViews using ListView and ArrayAdapter

查看:18
本文介绍了TextView 更改适用于使用 ListView 和 ArrayAdapter 的多个 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我开始从事一个小项目,主要目标是为我打造一种方法来跟踪我在 100 周内的行为.

I've started working on a small project not to long ago, the main goal is to forge a way for me to keep track of my actions during the course of 100 weeks.

我还是一个新手 android 开发者,我遇到了一个我无法解释的问题.

I'm still a rookie android developer and I've encountered an issue that I couldn't explain.

基本上,我使用 ArrayAdapter 填充了一个 ListView,其中包含一个包含 100 个字符串(Week1、Week2、Week3 ... Week100)的列表

Basically I've populated a ListView using the ArrayAdapter with a list containing 100 strings (Week1, Week2, Week3 ... Week100)

在每个 TextView 上设置一个 onclicklistener,这样当用户点击一个 textview 时,背景颜色会变为红色.

Setting up an onclicklistener on each of the TextViews so that when a user performs a click on a textview, the background color would change to red.

但是;每当我单击单个文本视图时 - 不止一个文本视图被着色.

However; whenever I click a single textview - more than a single textview is being colored.

注意事项:

  1. 我正在使用 ScrollView 滚动整个列表.(填充后,100 周列表会填满整个屏幕,滚动视图用于访问整个列表.)

  1. I'm using a ScrollView to scroll through the entire list. (Once populated, the 100 week list fills up the entire screen, the scroll view is used to access the entire list.)

我还保存了对当前绘制的 textview 的引用,这样我可以确保当用户单击不同的 textview 时,前一个会失去其红色背景.

I also saved a reference to the currently painted textview so I could make sure that when a user clicks a different textview, the previous one would lose its red background.

MainActivity 初始化:

MainActivity initialization:

public class MainActivity extends ActionBarActivity 
{
TextView selectedWeek; // Reference to the selected week.
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    populateWeekList(); // Populating the ListView
    initWeekClick(); // Initializing click listener
}

填充 ListView:

Populating the ListView:

public void populateWeekList()
{
    String[] weeks = new String [100]; // 100 weeks
    for (int i=0; i<100;i++)
    {
        weeks[i] = "Week"+(i+1);
    }
    ArrayAdapter<String> weekAdapter = new ArrayAdapter<String>(
            this,
            R.layout.weeksview,
            weeks
    );

    // R.id.weekTypeList is just a normal TextView.
    ListView weekList=(ListView) findViewById(R.id.weekTypeList); 
    weekList.setAdapter(weekAdapter);
}

初始化 onClickListener 并保存 selectedWeek 引用的代码:

Code for initializing onClickListener and saving the selectedWeek reference:

public void initWeekClick()
{
    ListView weekList=(ListView) findViewById(R.id.weekTypeList);
    weekList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) 
        {
            if (selectedWeek != null) 
            {
                selectedWeek.setBackgroundColor(0);
            }
            TextView clicked = (TextView) viewClicked;

            // Change clicked TextView color to red.
            clicked.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));

            // Save the selected week reference
            selectedWeek = clicked;
        }
    });
}

推荐答案

好的,你的背景正在洗牌,因为当你滚动 ListView 时 getView() 会被调用,它会考虑你当前的 TextView(作为当前 view) 并在其上设置背景,因为它检测到 onClick 侦听器的 setBackground() 方法..

Ok, your background is shuffling because when you scroll your ListView getView() is called and it consider your current position of TextView(as current view) and set background on it as it detect setBackground() method at onClick listener on it..

首先我推荐创建一个Adapterextends ArrayAdapter

解决方案 1:

在您的文本视图上的 onClick 侦听器中使用 setTag(),例如..

Use setTag() at onClick listener on your text view like..

text.setTag(position);

及以上使用 getTag() 并放置条件

and above it use getTag() and put condition

if(holder.text.getTag().equals(position)){
    holder.text.setBackgroundColor(Color.BLUE);
}else{
    holder.text.setBackgroundColor(Color.WHITE);
}

解决方案 2:

将此添加到 onCreate 方法

    ArrayList<String> _array = new ArrayList<String>();
    for(int i=0 ; i <1000; i ++){                       // 1000 value
        _array.add(i+"");                
    }
    list.setAdapter(new  MainAdapter(this, _array));        // pass you list here

ArrayAdapter 类:

ArrayAdapter class :

public class MainAdapter extends ArrayAdapter<String> {

    ArrayList<String> _st = new ArrayList<String>();
    ArrayList<Integer> check = new ArrayList<Integer>();
    Context _context;
    public MainAdapter(Context context,ArrayList<String> _st) {
        super(context,R.layout.main, _st);        // your inflate layout
        this._context = context;
        this._st = _st;

    }
    @Override
    public int getCount() {
        return _st.size();
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        //---//

        // check if current position is there in arraylist
        if(checking(position)){
            holder.text.setBackgroundColor(Color.BLUE);
        }else{
            holder.text.setBackgroundColor(Color.WHITE);
        }
        holder.text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // set background and put value in array list
                holder.text.setBackgroundColor(Color.BLUE);
                check.add(position);

            }
        });
        return convertView;
    }

    // this will check whether current position is there is array list or not and if it there it will break loop and return true
    public boolean checking(int position){
        boolean fine = false;
        for(int i=0; i<check.size();i++){
            if(position == check.get(i)){
                fine = true;
                break;
            }
        }
        return fine;
    }       
  }
  public class ViewHolder{
      TextView text;
  }
}

我不知道我在这段代码中有多少道德......但正如你所指定的那样,你有 100 价值.我已经在 1000 价值上对其进行了测试它起作用了

I don't how much I am ethical in this code...but as you have specified that you have 100 value.I have tested it on 1000 value and it worked

我不是专家,所以如果我在某个地方错了,请告诉我

希望有用!!!

这篇关于TextView 更改适用于使用 ListView 和 ArrayAdapter 的多个 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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