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

問題なく動作します。しかし、私は次のステップを学びたいです...

1 行目にandroid.R.layout.simple_list_item_2whereを、2 行目に+を使用できるようにコードを変更したいと思います。の代わりに何を実装する必要がありますか?また、どのアダプターまたは何を使用すればよいですか?nameinfonoteCustomer.toString()

パトリックのコメント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

2

カスタムlistViewの作成方法については、このチュートリアルをご覧ください。初心者なので、新しいプロジェクトの段階から始めるので、役立つと思います。メソッドの代わりに何を使用する必要があるかについては、データを取得するためのゲッターをクラスtoString()に作成するだけです。Customerこのようなもの:

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 に答える