安卓:用于处理多个ArayStringLists自定义适配器 [英] Android: custom adapter for handling more than one ArayStringLists

查看:308
本文介绍了安卓:用于处理多个ArayStringLists自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个排的布局,有两个textViews,可以说textViewX和textViewY。
我知道如何填充一个TextView的使用适配器1的ArrayList。

我如何自定义适配器处理两个字符串数组列表填充包含两个textViews一个row_layout?

我试着这样做:

  //充分利用ROW_Layout年为文本视图,并添加来自字符串数组列表中的文本
            TextView的yearTextView =(的TextView)findViewById(R.id.editTextYear);
            尝试{
                yearTextView.setText((CharSequence的)yearStringList);
            }赶上(例外五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            yearTextView.setTextColor(0xff444444); //设置深灰色的颜色为文本            //充分利用ROW_Layout为国家文本视图,并添加来自字符串数组列表中的文本
            TextView的countryTextView =(的TextView)findViewById(R.id.editTextCountry);
            尝试{
                countryTextView.setText((CharSequence的)countryStringList);
            }赶上(例外五){
                // TODO自动生成catch块
                e.printStackTrace();
            }

应用程序崩溃,并且我查了日志,并有一个致命错误朝着这个指点:

  yearTextView.setText((CharSequence的)yearStringList);


解决方案

使用一个模型类

 类模型
{
字符串值1;
字符串值2
公共无效setValue1(字符串值1)
{
this.value1 =值;
}
公共无效setValue2(字符串值2)
{
this.value2 =值2;
}
公共字符串getValue1()
{
返回this.value1;
}
公共字符串getValue2()
{
返回this.value2;
}
}

现在在活动

 的ArrayList<模型与GT; AA =新的ArrayList<模型与GT;();

现在填充使用循环列表说。下面仅仅是一个例子。只要确保你填充列表。

 的for(int i = 0;我小于10;我++)
 {
 型号型号=新模式();
 model.setValue1(值1);
 model.setValue2(值2);
 aa.add(模型);
 }

通列表 AA 来CustomAdapter,并用它还有

在getView

 名模莫=(模型)aa.get(位置);
 tv1.setText(mo.getValue1());
 tv2.setText(mo.getValue2());

I have a Row layout, with two textViews, lets say textViewX and textViewY. I know how to populate one textView with one arrayList using the Adapter.

How do I make a custom Adapter to handle two Array String Lists to populate a row_layout containing two textViews ?

I tried doing this :

            // Getting the Text view from the ROW_Layout for Year and adding the text from array string lists 
            TextView yearTextView = (TextView) findViewById(R.id.editTextYear);
            try {
                yearTextView.setText((CharSequence) yearStringList);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            yearTextView.setTextColor(0xff444444);  // Setting Dark Grey color to text

            // Getting the Text view from the ROW_Layout for Country and adding the text from array string lists
            TextView countryTextView = (TextView) findViewById(R.id.editTextCountry);
            try {
                countryTextView.setText((CharSequence) countryStringList);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

The application crashes, and I checked the logs, and there is a "FATAL ERROR" pointing towards this:

yearTextView.setText((CharSequence) yearStringList);

解决方案

Use a Model Class

class Model
{
String value1;
String value2
public void setValue1(String value1)
{
this.value1=value1;
}
public void setValue2(String value2)
{
this.value2=value2;
}
public String getValue1()
{
return this.value1;
}
public String getValue2()
{
return this.value2;
}
}

Now in Activity

 ArrayList<Model> aa = new ArrayList<Model>();

Now populate the list say using for loop. Below is just an example. Just make sure you populate the list.

 for(int i=0;i<10;i++)
 {
 Model model = new Model();
 model.setValue1("value1");
 model.setValue2("value2");
 aa.add(model);
 } 

Pass list aa to CustomAdapter and use it there

In getView

 Model mo = (Model) aa.get(position);
 tv1.setText(mo.getValue1());
 tv2.setText(mo.getValue2());

这篇关于安卓:用于处理多个ArayStringLists自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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