gpt4 book ai didi

java - 空对象引用上的 ListView.setAdapter(android.widget.ListAdapter)'

转载 作者:行者123 更新时间:2023-12-01 22:58:05 25 4
gpt4 key购买 nike

我想要一个 ListView 。但我遇到了以下错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.photoapp/com.example.photoapp.ViewAdmins}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

这是我的代码。

 public class ViewAdmins extends AppCompatActivity {

SyncAdminsDatabaseHelper mSyncAdminsDatabaseHelper;
private ListView ymListView;

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

ymListView = (ListView) findViewById(R.id.listviewadmins);
mSyncAdminsDatabaseHelper = new SyncAdminsDatabaseHelper(this);

//call method populateListView();
populateListView();

}
private void populateListView(){

//get the data to append in the list view
Cursor data = mSyncAdminsDatabaseHelper.getData();

if(data.getCount() == 0){
toastMessage("The database is empty.");
}else{

//Create an Array list
ArrayList<String> listData = new ArrayList<>();

//Store the result in array using while loop
while (data.moveToNext()){
//get the value from the database in column 1
//than add to array list
listData.add(data.getString(0));

}
System.out.println(listData);
// //create the list adapter and set the adapter
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,listData);
ymListView.setAdapter(adapter);


}


}

我想将应用表中的数据显示到 ListView 。但由于某种原因,我的 ListView 无法正确呈现数据。

我的问题是如何从SQLite表中获取数据并显示在 ListView 中。

以及如何在 ListView 中显示多个字段。

Activity_view_admins.xml代码

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


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listviewadmins">

</ListView>
</LinearLayout>

最佳答案

这是我几年前所做的,但我不是从 SQL lite 读取数据,而是从我的网络服务返回数据。但它应该让您了解如何填充 ListView 。

   public class MainActivity extends AppCompatActivity {
private static final String url = "http://10.0.2.2/android_app/urltomywebservice";
private ProgressDialog dialog;
private List<Item> array = new ArrayList<Item>();
private ListView listView;
private Adapter adapter;

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

listView = (ListView) findViewById(R.id.list_item);
adapter=new Adapter(this,array);
listView.setAdapter(adapter);

dialog=new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();

//Creat volley request obj
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideDialog();
//parsing json
for(int i=0;i<response.length();i++){
try{
JSONObject obj=response.getJSONObject(i);
Item item=new Item();
item.setTitle(obj.getString("Title"));
item.setImage(obj.getString("Image"));
item.setRate(obj.getInt("rating"));
item.setPrice(obj.getInt("Price"));



//add to array
array.add(item);
}catch(JSONException ex){
ex.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
AppController.getmInstance().addToRequesQueue(jsonArrayRequest);
}
public void hideDialog(){
if(dialog !=null){
dialog.dismiss();
dialog=null;
}
}

}

这里我添加到 ListView 的数组是我的模型用于设置和获取所需数据的列表。如果适合您,您可以创建类似的东西。

private List<Item> array = new ArrayList<Item>();

关于java - 空对象引用上的 ListView.setAdapter(android.widget.ListAdapter)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58431386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com