【问题标题】:sqlite Viewlist errorsqlite 视图列表错误
【发布时间】:2013-07-27 23:30:42
【问题描述】:

我尝试将数据库中的一些元素放到列表视图中, 我的问题是,当我开始我的活动时,我得到了这个:

**com.example.restaurant.Restaurant@2be2d1d0

com.example.restaurant.Restaurant@2be3d3a8**

而不是数据库对象。

public class Liste extends Activity{

    private ListView listview;
    private Button boutonAjouter;

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

        listview = (ListView)findViewById(R.id.listView1);

        Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
        Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");

        RestaurantBDD restaurantBDD = new RestaurantBDD(this);
        restaurantBDD.openForWrite();
        restaurantBDD.insertRestaurant(mcdo);
        restaurantBDD.insertRestaurant(burger);

        ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
        restaurantBDD.close();

        ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
        listview.setAdapter(adapter);


        boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
        boutonAjouter.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Liste.this, Formulaire.class);
                startActivity(intent);

            }
        });


    }

}

【问题讨论】:

  • 你正试图显示objects(事实上,你是)。您需要的是一个自定义适配器,您可以在其中获取数据并以有意义的方式显示它。或者,您需要为适配器提供一个 Arraylist 字符串,您将从当前拥有的 Restaurant 对象中提取该字符串。

标签: android sqlite android-listview


【解决方案1】:

如果要显示包含餐厅名称的列表,假设您的 Restaurant 类包含字符串属性“name”,则应首先创建一个包含餐厅名称的数组,然后使用 ArrayAdapter &lt;String&gt;

String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
    values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2011-02-28
    相关资源
    最近更新 更多