3

我从我的(测试)数据库中获取客户信息列表,我想显示它。客户由具有、和成员的Customer类表示。它的方法只返回. 我创建了仅使用布局的,因此仅显示客户的 - 如下所示:nameinfonotetoStringnameDemoDatabaseMainActivitysimple_list_item_1name

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_2where和第二行的+ ?应该实现什么而不是,以及我应该使用什么适配器或其他什么?nameinfonoteCustomer.toString()

根据 Patric 的评论https://stackoverflow.com/a/16062742/1346705进行更新——对此,我希望修改后的解决方案是这样的:

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中的。super运行时,代码显示错误日志消息,如:

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

我想问你问题出在哪里。试图找到原因,我已经修改了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;
    }
}

为了确保覆盖getView有效,我显示了客户信息的不同部分。它工作得很好。换句话说,不是显示通用toString方法,而是显示我的特定getInfo结果。

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

4

2 回答 2

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;
    }
  })
于 2013-04-17T14:27:46.247 回答
2

查看教程,了解如何创建自定义 listView。作为初学者,它从新的项目阶段开始,我认为它会对你有所帮助。至于你应该使用什么而不是你的toString()方法,只需在你的Customer类中创建一些 getter 来检索数据。像这样的东西:

public String getName(){
return this.name;
}

并在您的代码中使用它,如下所示:

 Customer x = new Customer();
//return the customer name into a string, obviously you will return it in a list that you will pass it to the listview
String customerName = x.getName();
于 2013-04-17T14:28:20.300 回答