我从我的(测试)数据库中得到客户信息列表,我想显示它。客户由具有Customernameinfo成员的note类表示。它的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);
    }
...
}

它工作得很好;但是,我想学习下一步…
我想修改代码,以便在第一行使用android.R.layout.simple_list_item_2,在第二行使用name+info?应该实现什么而不是note,应该使用什么适配器或什么?
根据patric的评论进行更新——关于这一点,我希望修改后的解决方案是这样的:
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);
    }
...
}

所以,我已经这样添加了我的Customer.toString()类:
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;
    }
}

但它不起作用(显然是因为我的一些错误)。注意TwolineAdapter构造函数代码调用中的simple_list_item_2。运行时,代码显示错误日志消息,如:
E/ArrayAdapter(27961): You must supply a resource ID for a TextView

我想问你问题出在哪里。为了找到原因,我对super进行了修改,使其工作方式与原来的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_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;
    }
}

为了确保覆盖的simple_list_item_1有效,我显示了客户信息的另一部分。而且效果很好。换句话说,不是一般的getView方法,而是显示特定toString的结果。
无论如何,我需要使用getInfo。接下来我该怎么办?(我的结论是我在构造函数中做了一些错误的事情。我说的对吗?问题在哪里?)

最佳答案

您可以重写getViewArrayAdapter方法:

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

09-10 22:19
查看更多