繁体   English   中英

我的 ListView 为空,我不明白为什么

[英]My ListView is null and i don't get why

iv 一直试图将我的 listView 填充到一个活动上,但即使在初始化之后我的 listView 仍然保持为空。 我检查了错误日志并通过调试器运行它,我得出结论我没有正确初始化它,尽管我不确定在哪里。

主要活动

  public class DisplayCountryActivity extends AppCompatActivity {

    ListView listView;


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

        // Construct the data source
        ArrayList<Country> countries = new ArrayList<>();
        // Create the adapter to convert the array to views

        //context=this;
         listView = (ListView) findViewById(R.id.list_view);

        Cursor cursor = getContentResolver().query(Contract.Country.contentUri, null, null, null, null);

        while(cursor.moveToNext()) {
            countries.add(new Country(cursor.getString(cursor.getColumnIndex("country")),cursor.getDouble(cursor.getColumnIndex("capital")),
                    cursor.getString(cursor.getColumnIndex("area")),cursor.getInt(cursor.getColumnIndex("population")),cursor.getString(cursor.getColumnIndex("official_lang"))));
        }


            CountryAdaptor adapter = new CountryAdaptor(this, countries);

            // Attach the adapter to a ListView
           listView.setAdapter(adapter);

    }


}

适配器

    public class CountryAdaptor extends ArrayAdapter<Country> {

    private static LayoutInflater inflater = null;
    public CountryAdaptor(Context context, ArrayList<Country> users) {
        super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Country country = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.country_item, parent, false);
        }
        // Lookup view for data population
        TextView countryTv = (TextView) convertView.findViewById(R.id.dp_country);
        TextView capitalTv = (TextView) convertView.findViewById(R.id.dp_capital);
        TextView areaTv = (TextView) convertView.findViewById(R.id.dp_area);
        TextView populationTv = (TextView) convertView.findViewById(R.id.dp_population);
        TextView officialLanguageTv = (TextView) convertView.findViewById(R.id.dp_official_language);

        // Populate the data into the template view using the data object
        countryTv.setText(country.getCountry());
        capitalTv.setText(country.getCapital());
        areaTv.setText(country.numberToString(country.getArea()));
        populationTv.setText(country.numberToString(country.getArea()));
        officialLanguageTv.setText(country.getOfficial_lang());

        // Return the completed view to render on screen
        return convertView;
    }
}

列表显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayCountryActivity">


    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:id="@+id/list_view">
    </ListView>

所以我解决了这个问题。 首先,只是为了澄清一下,当我尝试用我的 xml 文件初始化列表视图变量时,它为空。 我的错误是我将 listview xml 文件作为一个独立的文件,没有对其进行格式化。 所以基本上我的错误是没有将 ListView 标签放在我使用的内容 xml 文件中。 我所要做的就是将 ListView 标签添加到我的内容 xml 文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM