如何构建和显示simple_list_item_2的信息? [英] How to construct and display the info in simple_list_item_2?

查看:336
本文介绍了如何构建和显示simple_list_item_2的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的客户信息列表从我的(测试)的数据库,我想以显示它。该客户重新使用 psented由客户类$ P $名称信息的成员。其的toString 方法只返回名称。我创建了 DemoDatabaseMainActivity 使用的 simple_list_item_1管理布局而已,因此只显示名称一个客户 - 是这样的:

I get the list of customer info from my (testing) database, and I want to display it. The customer is represented by the Customer class with name, info, and note members. Its toString method returns only the name. I have created the DemoDatabaseMainActivity that uses the simple_list_item_1 layout only, thus displaying only the name of a customer -- like this:

public class DemoDatabaseMainActivity extends ListActivity {

    private CustomerDataSource datasource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        datasource = new CustomerDataSource(this);
        datasource.open();

        List<Customer> values = datasource.getAllCustomers();

        ArrayAdapter<Customer> adapter = new ArrayAdapter<Customer>(this,
                                   android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }
...
}

它工作得很好;不过,我想了解下一个步骤...

It works just fine; however, I'd like to learn the next step...

我想修改code,这样我可以使用 android.R.layout.simple_list_item_2 ,其中名称将处于第一线,和信息 + 在第二条线?应该怎样执行,而不是 Customer.toString(),和或者我应该用什么接口什么?

I would like to modify the code so that I could use the android.R.layout.simple_list_item_2 where name would be at the first line, and the info + note at the second line? What should be implemented instead of the Customer.toString(), and what adapter or whatever should I use?

更新基于帕特里克的评论 http://stackoverflow.com/a/16062742/1346705 - 对于这一点,我也希望这样的修改后的解决方案:

Update based on Patric's comment http://stackoverflow.com/a/16062742/1346705 -- with respect to that, I have hoped for the modified solution like that:

public class DemoDatabaseMainActivity extends ListActivity {

    private CustomerDataSource datasource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        datasource = new CustomerDataSource(this);
        datasource.open();

        List<Customer> values = datasource.getAllCustomers();

        TwolineAdapter adapter = new TwolineAdapter(this, values);  // here the difference
        setListAdapter(adapter);
    }
...
}

所以,我已经加入我的 TwolineAdapter 类是这样的:

public class TwolineAdapter extends ArrayAdapter<Customer> {

    private List<Customer> objects;

    public TwolineAdapter(Context context, List<Customer> objects) {
        super(context, android.R.layout.simple_list_item_2, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text1 = (TextView) view.findViewById(android.R.id.text1);
        TextView text2 = (TextView) view.findViewById(android.R.id.text2);

        text1.setText(objects.get(position).getName());
        text2.setText(objects.get(position).getInfo() 
                      + " (" + objects.get(position).getNote() + ")");
        return view;
    }
}

但没有奏效(显然是因为我的一些错误)。注意 simple_list_item_2 构造函数code调用。运行时,code显示类似错误日志消息:

But it did not work (apparently because of some of my errors). Notice the simple_list_item_2 in the super constructor code call. When running, the code shows the error log message like:

E/ArrayAdapter(27961): You must supply a resource ID for a TextView

我想问你的问题所在。试图找出原因,我已经修改了 TwolineAdapter 以相同的方式工作原来与 simple_list_item_1管理

I would like to ask you where the problem is. Trying to find the reason, I have modified the TwolineAdapter to work the same way as the original with the simple_list_item_1

public class TwolineAdapter extends ArrayAdapter<Customer> {

    private List<Customer> objects;

    public TwolineAdapter(Context context, List<Customer> objects) {
        super(context, android.R.layout.simple_list_item_1, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view  = (TextView)super.getView(position, convertView, parent);
        view.setText(objects.get(position).getInfo());  // displaying a different info
        return view;
    }
}

要确保该重写 getView 的作品,我都显示了客户信息的不同部分。它工作得很好。换句话说,而不是一般情况下,的toString 的方法,我的具体的结果的getInfo 被显示出来。

To be sure the overriden getView works, I have displayed a different part of the customer info. And it worked fine. In other words, instead of the general toString method, the result of my specific getInfo was displayed.

不管怎样,我需要使用 simple_list_item_2 。下一步做什么,应该怎么办? (我的结论是我做错了什么,在构造函数中。我说得对?问题出在哪里?)

Anyway, I need to use the simple_list_item_2. What next should I do? (My conclusion is I am doing something wrong in the constructor. Am I right? Where is the problem?)

推荐答案

您可以覆盖 ArrayAdapter getView 方法C>:

You can override the getView method of the ArrayAdapter:

  new ArrayAdapter (context, android.R.layout.simple_list_item_2, android.R.id.text1, list)
  {
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      TextView text1 = (TextView) view.findViewById(android.R.id.text1);
      TextView text2 = (TextView) view.findViewById(android.R.id.text2);
      text1.setText("1");
      text2.setText("2");
      return view;
    }
  })

这篇关于如何构建和显示simple_list_item_2的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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