看不到列表视图,屏幕上未呈现任何内容



>我有一个列表视图,应该用数组列表中的数据填充,

这是代码:

public class List_view extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> throwlist = new ArrayList<String>();
        setContentView(R.layout.activity_list_view);
        ListView Trainlist;
        Trainlist=(ListView)findViewById(R.id.station_list);
        SQLiteDatabase db = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        setContentView(R.layout.activity_list_view);
        try{
            Cursor data_fetch = db.rawQuery("Select Station_name From Station", null);
            String[] station_array = new String[data_fetch.getCount()];
            int i= 0;
            while (data_fetch.moveToNext()) {
                String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
                station_array[i] = name;
                Log.d("STATION_GET!","Retrieved station " + station_array[i]);
                //Toast.makeText(List_view.this, "Retrieved station " + station_array[i] , Toast.LENGTH_SHORT).show();
                throwlist.add(name);
                i++;
            }
            data_fetch.close();
        }catch(Exception e)
        {
            Toast.makeText(List_view.this, "An Error occured Retrieving your data" , Toast.LENGTH_LONG).show();
        }
        try {
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_list_item_1,throwlist);
            Trainlist.setAdapter(arrayAdapter);
        }catch (Exception e){
            Log.d("Error in listview ",e.toString());
            Toast.makeText(List_view.this, "Listview Error" , Toast.LENGTH_LONG).show();
        }
    }
}

当我运行应用程序时,活动中没有任何内容出现,这里是列表视图的 XMl 数据:

<ListView
    android:id="@+id/station_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    tools:layout_constraintTop_creator="1"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="8dp" />

是的,我确认有问题的数据 bse 确实有一些数据。这是从数据库中检索的数据的日志输出

    10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station New York
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Boston
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Las Vegas
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Miami
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Chicago
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station New England
10-29 06:55:57.721 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Detroit
10-29 06:55:57.722 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Michigan
10-29 06:55:57.722 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station Austin
10-29 06:55:57.722 31050-31050/com.example.arun.db D/STATION_GET!: Retrieved station New Orleans 

任何帮助将不胜感激!

  • 删除第二个 setContentView(R.layout.activity_list_view);。你为什么打电话给多个时间?
  • 在编写代码之前,请思考。您应该阅读official Tutorials .

不要

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ArrayList<String> throwlist = new ArrayList<String>();
            setContentView(R.layout.activity_list_view);
            ListView Trainlist;

ArrayList<String> throwlist = new ArrayList<String>();
    ListView Trainlist;  
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

相关内容

最新更新