java - 仅在填充的 ListView 中显示模型

标签 java android listview

我将以下字段记录为输出:

I/System.out: getLongitude 4.745929
I/System.out: getStreet Laat 199
I/System.out: getLatitude 52.630753
I/System.out: getCity Alkmaar
I/System.out: getZipCode 1811 EG 
I/System.out: getName SUBWAY® Alkmaar
I/System.out: getFacebookID 180894875290925

但是我想输出到我的 ListView 中,您可以在下面看到它当前在我的 ListView 中的样子的屏幕截图:

屏幕截图链接:/image/DLxDb.png

在下面您可以看到我尝试过的内容:

AllStores.java:

            Iterator it = subprises.body().iterator();
            List<Store> subprisesList = new ArrayList<>();
            int i = 0;
            while(it.hasNext()) {
                i++;
                Store store = (Store) it.next();
                System.out.println("getLongitude "+store.getLongitude());
                System.out.println("getStreet "+store.getStreet());
                System.out.println("getLatitude "+store.getLatitude());
                System.out.println("getCity "+store.getCity());
                System.out.println("getZipCode "+store.getZipCode());
                System.out.println("getName "+store.getName());
                System.out.println("getFacebookID "+store.getFacebookID());
                System.out.println("next store");
                subprisesList.add(store);
            }
            StoreArrayAdapter stringArrayAdapter = new StoreArrayAdapter(
                    getApplicationContext(),
                    R.layout.listview_item_row,
                    subprisesList);
            lv.setAdapter(stringArrayAdapter);
<小时/>

编辑:我更改了我的 StoreArrayAdapter 类

StoreArrayAdapter class which lives inside AllStores.java:
    private class StoreArrayAdapter extends ArrayAdapter<Store> {
        public StoreArrayAdapter(Context context, int textViewResourceId, List<Store> stores) {
            super(context, textViewResourceId, stores);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)  {
            View storeView = convertView;
            if (convertView == null) {
                // inflate your list view here
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                storeView = inflater.inflate(R.layout.listview_item_row, parent, false);
            }
            Store store = getItem(position);
            // use findViewById() to get the TextViews
            TextView name = (TextView)storeView.findViewById(R.id.txtName);
            // call setText using the values from store
            name.setText(store.getName());

            return convertView;
        }
    }

这次我想向您展示我的观点和名为 Store 的模型。

activity_all_stores.xml:

代码链接:http://pastebin.com/6eiUyPKU

listview_item_row.xml:

代码链接:http://pastebin.com/CGSVf2Uq

商店.java:

代码链接:http://pastebin.com/BEsWfaEQ

最佳答案

您在图像中看到文本的原因是它来自默认的 Store.toString() 方法。您可以通过重写 Store 类中的 toString() 方法来开始看到更有趣的东西:

@Override
public String toString() {
    return getName();  // or define how you want
}

但最终您可能需要定义一个 ListView 布局,其中每个字段都有单独的 TextView。然后,您需要在 StoreArrayAdapter 子类中重写 getView():

    @Override
    public View getView(int position, View view, ViewGroup parent)  {

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listview_item_row, parent, false);
        }
        Store store = getItem(position);
        // use findViewById() to get the TextViews
        TextView name = (TextView) view.findViewById(R.id.txtName);
        // call setText using the values from store
        name.setText(store.getName());

        return view;
    }

关于java - 仅在填充的 ListView 中显示模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381167/

相关文章:

java - 将数据拟合到配电 (java)

java - 如何在Java中生成以随机多边形为中心的多边形?

android - 列表适配器仅将 1 个项目添加到 ListView

java - xsd java类生成

java - 找不到名称下的 bean

android - View 拖放更新时出现 stackoverflow 错误

android - 具有单独行布局的 ListView 无法正确生成 View

android - 如何使用 SimpleCursorAdapter 使 ListView 跳过 ""文本?

android - Android中如何通过点击listview的item来连接蓝牙设备?

java - 回收器 View : No adapter attached; skipping layout (no shw Slider and photos Picasso and Sliderbanner