【问题标题】:Adding items to data source doesn't appear in ListView向数据源添加项目不会出现在 ListView 中
【发布时间】:2011-04-05 12:30:14
【问题描述】:

我正在向我的数据源添加项目,并希望它出现在我的 ListView 中。由于某种原因,什么都没有出现:

适配器:

private class STCompanyAdapter extends ArrayAdapter<STCompany> {

        private ArrayList<STCompany> items;

        public STCompanyAdapter(Context context, int textViewResourceId,
                ArrayList<STCompany> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.addstocksrow, null);
            }
            STCompany q = items.get(position);
            if (q != null) {
                TextView symbolText = (TextView) v.findViewById(R.id.toptext);
                TextView nameText = (TextView) v.findViewById(R.id.bottomtext);

                if (nameText != null) {
                    nameText.setText(q.getSymbol());
                }
                if (symbolText != null) {
                    symbolText.setText(q.getName());
                }
            }
            return v;
        }
    }

在 onCreate 中添加项目:

listView = (ListView) findViewById(R.id.symbolsListView);
            this.companiesAdapter = new STCompanyAdapter(this, R.layout.addstocksrow, companies);
            listView.setAdapter(this.companiesAdapter);

    STCompany myCompany = new STCompany();
                    myCompany.setName("La La");
                    myCompany.setSymbol("BP");

                    companiesAdapter.add(myCompany);
                    companiesAdapter.notifyDataSetChanged();

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout">
<EditText android:id="@+id/addStocksEditText" android:text="Search company or stock" android:layout_width="200dp" android:layout_height="50dp"></EditText><Button android:layout_height="wrap_content" android:text="Search" android:id="@+id/searchButton" android:layout_width="fill_parent"></Button><ListView android:id="@+id/symbolsListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>


</LinearLayout>

【问题讨论】:

    标签: android listview arraylist listadapter


    【解决方案1】:

    问题是我的布局。列表视图没有出现。代码没问题。

    【讨论】:

      【解决方案2】:

      尝试用 Companies.add(myCompany) 代替 CompaniesAdapter.add(myCompany)。

      【讨论】:

        猜你喜欢
        • 2020-11-18
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-02-18
        相关资源
        最近更新 更多