如何在simple_list_item_2中构建和显示信息?

pep*_*epr 3 android android-layout

我从我的(测试)数据库中获取客户信息列表,我想显示它.客户是由所表示的Customer带班name,infonote成员.它的toString方法只返回name.我创建的仅DemoDatabaseMainActivity使用simple_list_item_1布局,因此只显示name客户 - 如下所示:

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);
    }
...
}
Run Code Online (Sandbox Code Playgroud)

它运作得很好; 但是,我想学习下一步......

我想修改代码,这样我可以用android.R.layout.simple_list_item_2其中name将在第一线,和info+ note第二行?应该实现什么,而不是Customer.toString()什么适配器或我应该使用什么?

根据Patric的评论/sf/answers/1124391971/ 更新 - 关于这一点,我希望修改后的解决方案如下:

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);
    }
...
}
Run Code Online (Sandbox Code Playgroud)

所以,我这样添加了我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但它没有用(显然是因为我的一些错误).注意simple_list_item_2super构造函数代码调用.运行时,代码显示错误日志消息,如:

E/ArrayAdapter(27961): You must supply a resource ID for a TextView
Run Code Online (Sandbox Code Playgroud)

我想问你问题出在哪里.试图找到原因,我修改了TwolineAdapter与原版相同的工作方式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;
    }
}
Run Code Online (Sandbox Code Playgroud)

为了确保覆盖getView有效,我已经显示了客户信息的不同部分.它工作得很好.换句话说,而不是一般toString方法,getInfo显示我的具体结果.

无论如何,我需要使用simple_list_item_2.我接下来该怎么办?(我的结论是我在构造函数中做错了.我是对的吗?问题出在哪里?)

Pat*_*ick 17

您可以覆盖以下getView方法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;
    }
  })
Run Code Online (Sandbox Code Playgroud)