一个TextView变化适用于使用ListView和ArrayAdapter多个TextViews [英] A TextView change applies to multiple TextViews using ListView and ArrayAdapter

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

问题描述

我开始不不久前在一个小项目的工作,主要目标是打造一个办法,我到100周期间跟踪我的行为。

我还是个菜鸟的Andr​​oid开发人员,我遇到了我无法解释的问题。

基本上我用填充一个ArrayAdapter含100个字符串(Week1,Week2,译员更加... Week100)列表一个ListView

设置在每个TextViews的onclicklistener以便当用户执行上一个TextView点击,背景颜色将改变为红色。

不过,每当我点击一个单一的TextView - 比单一的TextView更被有色

注:


  1. 我使用的是滚动型在整个列表中滚动。 (填充一次,100个星期列表填满整个屏幕,滚动视图用于访问整个列表。)


  2. 我还保存到现在画的TextView的引用,所以我可以确保当用户点击一个不同的TextView中,previous人会失去它的红色背景。


MainActivity初始化:

 公共类MainActivity扩展ActionBarActivity
{
TextView的selectedWeek; //参考所选择的一周。
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    populateWeekList(); //填充的ListView
    initWeekClick(); //初始化监听点击
}

填充的ListView:

 公共无效populateWeekList()
{
    的String [] =周新的String [100]; //100周
    的for(int i = 0; I< 100;我++)
    {
        周由[i] =周+第(i + 1);
    }
    ArrayAdapter<串GT; weekAdapter =新ArrayAdapter<串GT;(
            这个,
            R.layout.weeksview,
            周
    );    // R.id.weekTypeList只是一个普通的TextView。
    ListView控件weekList =(ListView控件)findViewById(R.id.weekTypeList);
    weekList.setAdapter(weekAdapter);
}

code初始化onClickListener和保存selectedWeek参考:

 公共无效initWeekClick()
{
    ListView控件weekList =(ListView控件)findViewById(R.id.weekTypeList);
    weekList.setOnItemClickListener(新AdapterView.OnItemClickListener()
    {
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,查看viewClicked,INT位置,长的ID)
        {
            如果(selectedWeek!= NULL)
            {
                selectedWeek.setBackgroundColor(0);
            }
            点击的TextView =(TextView中)viewClicked;            //改变TextView的点击颜色为红色。
            clicked.setBackgroundColor(getResources()的getColor(android.R.color.holo_red_light));            //保存选定周的参考
            selectedWeek =点击;
        }
    });
}


解决方案

好吧,你的背景是洗牌,因为当你滚动你的ListView getView()被调用,它考虑您当前的TextView的位置(如当前的视图的),并在其上​​设置的背景,因为它检测到的setBackground()方法的onClick 就可以了听众。

首先,我建议创建一个适配器扩展ArrayAdapter<>

解决方案1:

使用 setTag()的onClick 像文本视图..监听器

  text.setTag(位置);

和它上面使用 getTag(),并把情况

 如果(holder.text.getTag()。等于(位置)){
    holder.text.setBackgroundColor(Color.BLUE);
}其他{
    holder.text.setBackgroundColor(Color.WHITE);
}

解决方案2:

添加到了的onCreate

 的ArrayList<串GT; _array =新的ArrayList<串GT;();
    的for(int i = 0; I< 1000;我++){// 1000值
        _array.add第(i +);
    }
    list.setAdapter(新MainAdapter(这一点,_array)); //通过您在此处列出

ArrayAdapter类:

 公共类MainAdapter扩展ArrayAdapter<串GT; {    ArrayList的<串GT; _st =新的ArrayList<串GT;();
    ArrayList的<整数GT;检查=新的ArrayList<整数GT;();
    上下文_context;
    公共MainAdapter(上下文的背景下,ArrayList的<串GT; _st){
        超(背景下,R.layout.main,_st); //你的充气布局
        this._context =背景;
        this._st = _st;    }
    @覆盖
    公众诠释的getCount(){
        返回_st.size();
    }
    @覆盖
    众长getItemId(INT位置){
        返回0;
    }
    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        // // ---        //检查,如果当前位置是在那里的ArrayList
        如果(检查(位置)){
            holder.text.setBackgroundColor(Color.BLUE);
        }其他{
            holder.text.setBackgroundColor(Color.WHITE);
        }
        holder.text.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                //设置背景,并把价值数组列表
                holder.text.setBackgroundColor(Color.BLUE);
                check.add(位置);            }
        });
        返回convertView;
    }    //这将检查是否当前的位置是有数组列表与否,如果有会打破循环,并返回true
    公共布尔检查(INT位置){
        布尔细= FALSE;
        的for(int i = 0; I< check.size();我++){
            如果(位置== check.get(I)){
                精= TRUE;
                打破;
            }
        }
        返回罚款;
    }
  }
  公共类ViewHolder {
      TextView的文本;
  }
}

我不知道有多少我在这code道德......但是当你指定了你的 100 value.I已经测试了它的 1000 的价值和它的工作

我不是专家,所以让我知道如果我错了地方

希望工程!

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.

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

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

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.

Notes:

  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.)

  2. 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 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
}

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);
}

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;
        }
    });
}

解决方案

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..

First I recommend to create a Adapter class extends ArrayAdapter<?>

Solution 1 :

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

text.setTag(position);

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);
}

Solution 2 :

Added this to onCreate method

    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 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;
  }
}

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

I am not expert so let me know if I am wrong somewhere

Hope it works !!!

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

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