我有一个List视图,应该使用数组列表中的Data填充
下面是代码:
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" />
是的,我确认有问题的数据确实有一些数据。 这是从数据库中检索的数据的logcat输出
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
真的很感激任何帮助!
答案 0 :(得分:1)
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);