【问题标题】:How to add two string arrays to a custom listview?如何将两个字符串数组添加到自定义列表视图?
【发布时间】:2012-02-14 11:34:03
【问题描述】:

我有一个自定义的ListView 和两个TextViews,我的班级中有两个字符串数组,如何将这两个字符串数组添加为我的ListView 的项目?每个代表一个文本视图。 谢谢。

【问题讨论】:

标签: android listview arrays


【解决方案1】:

我觉得这对你有用,用两个字符串数组调用简单的适配器

public class simleAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater linflater;
private TextView txt_1, txt_2;
private String[] str1;
private String[] str2;

public simleAdapter(Context context, String[] s1, String[] s2) {
    mContext = context;
    str1 = s1;
    str2 = s2;
    linflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return str1.length;
}

@Override
public Object getItem(int arg0) {
    return str1[arg0];
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        convertView = linflater.inflate(R.layout.feed_raw, null);

    }

    txt_1 = (TextView) convertView.findViewById(R.id.txtlist1);
    txt_2 = (TextView) convertView.findViewById(R.id.txtlist2);
    txt_1.setText(str1[position]);
    txt_2.setText(str2[position]);

    return convertView;

}

}

如果您有任何疑问,请告诉我

【讨论】:

  • 第二个字符串数组呢?我的意思是 getCount 和 getItem 方法没有用于 str2 对吗?
  • 两个数组的长度应该相同,否则必须使用对象的arraylist。创建一个具有两个字符串成员的类并将这些类对象的arraylist og传递给适配器
  • 谢谢你,但我找到了一个很棒的教程,与你的相似。为我工作。
  • ok 没问题但是两个字符串数组之间有任何关系那么你应该使用对象的arraylist 它更容易更快
  • @Dinesh 你能给出你找到的教程的链接吗?
【解决方案2】:

在此线程Custom adapter: get item number of clicked item in inflated listview 中查看我的答案。 这应该可以回答您正在尝试做的事情。

【讨论】:

  • 谢谢,但我几乎找不到我正在尝试做的事情与您在上述线程中的答案之间有任何相似之处。
  • 原始海报也有两个字符串数组。所以我把它变成了一个对象并以这种方式管理它。
  • 我希望它们都不同。
猜你喜欢
  • 2014-05-06
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多