- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在像这样设置一个 ListView 适配器:
public class SeeAllQuestionsActivity extends Activity
{
//ArrayAdapter<Question> adapter;
SimpleAdapter mSchedule = null;
ListView list = new ListView (this);
TextView loading_questions = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.all_question_page);
TextView loading_questions = (TextView) findViewById(R.id.loading_questions);
list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
mSchedule = new SimpleAdapter(this, mylist, R.layout.questions_list,
new String[] {"train", "from", "to"},
new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
list.setAdapter(mSchedule);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
...
然后进行远程异步调用以从我的数据库中获取列表,并尝试在 onPostExecute 方法中执行此操作:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
try
{
JSONArray obj = new JSONArray(result);
if ( obj != null )
{
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
map.put("train", "Business Name");
map.put("from", ">");
map.put("to", ">");
mylist.add(map);
map = new HashMap<String, String>();
map.put("train", "103(x)");
map.put("from", "6:35 AM");
map.put("to", "7:45 AM");
mylist.add(map);
}
}
}
catch ( Exception e )
{
}
list.setAdapter(mSchedule);
但是我在这一行得到了一个空指针异常:
ListView list = new ListView (this);
但我认为一般来说,我对如何在 postExecute 方法中完成这项工作还有很大的了解。非常感谢任何有关如何正确执行此操作的帮助。
最佳答案
在您的 OnCreate 中,您定义了一个新的 SimpleAdapter 并将其附加到您的 ListView。这是对的。数据源(在您的例子中为 mylist
)此时为空,因此您将在 AsyncTask
中填充它。
在您的onPostExecute 中,您正在创建一个新的ArrayList
。根据您收到的结果,您填写它。完成此操作后,您将再次设置适配器。这将不执行任何操作,因为适配器没有数据。您想要做的是将新填充的列表提供给适配器,以便它可以用您的数据填充 ListView
。
解决方案一
onPostExecute {
// create a list to store your data
new ArrayList
// fill the new list with the data you received (result)
fillArrayList from JSON
// create an adapter and give the new list with it
mAdapter = new SimpleAdapter(.., ArrayList, ...)
listView.setAdapter(mAdapter);
}
这是您可以做到的一种方式,适合您当前的实现。
方案二
我会选择这个解决方案
onPostExecute {
// don't create a new arraylist but use the mylist-object you created in the OnCreate
fill mylist object with new data
// mylist is the datasource of your adapter and it is now changed.
// let the ListView know his adapter received new information
mSchedule.notifyDataSetChanged
}
更新
查看 this tutorial .我使用相同的布局和相同的来源来填充我的列表,但我对其进行了更改,因此它与您的情况相似。祝你好运:)
主 Activity
public class MainActivity extends Activity {
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.listview);
String[] from = new String[] { "rowid", "col_1", "col_2", "col_3" };
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
// Create an adapter which will tell my ListView what to show..
// fillMaps is still empty so at the moment it my ListView will show
// nothing.
adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
lv.setAdapter(adapter);
// Retreive data from somewhere
new UpdateListWithAsyncTask().execute();
}
private class UpdateListWithAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
// do stuff
return null;
}
protected void onPostExecute(Void result) {
// Fill your datasource that your adapter has. In my case fillMaps
for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// my datasource is now changed, I want my adapter to know this and
// update my ListView
adapter.notifyDataSetChanged();
}
}
}
关于Android - 在异步调用后更新 ListAdapter 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11630942/
我正在实现 AdapterView产生一个AbsListView -like 类,我可以使用 CursorAdapter在布局中。我正在实现这个,因为我想使用方便的自动数据更新行为 CursorAda
Try to focus on general "dos and donts" regarding lists, e.g. is adding and removing 30 items of a 2
我有一个listadapter,其中有很多listitems,每个项目都包含一个textview 和一个imageview。我想从后端下载图片( 以 Base64 编码 ),所以 Picasso 和通
我对 Android 还很陌生,目前在使用自定义列表适配器时遇到一些问题。 基本上,我想制作一个问题列表,每个问题都有不同的类型(单选、复选框、文本、数字)。当 ListView 生成时,它显示得很好
我需要在另一个线程中加载自定义 ListAdapter 吗?我之前已经这样做过,但我也在同一个线程中下载了数据。 使用或不使用线程加载 ListAdapter 有何优缺点?加载线程是否需要大量资源?
我刚刚开始开发 Android,但遇到了问题。我有一个 DrawerNavigationListAdapter 和一个 DrawerNavigationListView,并且工作正常,现在我尝试向其中
我有一个 ListAdapter,用于在 Listview 中显示列表。现在我添加了一个长按菜单操作来删除任何选定的项目。 public boolean onContextItemSelected(M
加载列表适配器的最佳方式是什么。简单的问题,但我已经知道你们中的大多数人会说什么。你会说使用 LoaderManager/CursorManager。每个人都会告诉我它使用起来多么容易,而且它不能在
我已经有一个使用 SQLite 数据库和列表适配器的应用程序。我正在尝试更新我的应用程序以使用全文搜索功能,但正在努力寻找问题的答案。基本上,当我使用必要的 _id 列创建虚拟表时,数据库会将其转换为
我使用这个例子中的 ListAdapter: http://code.google.com/p/au-optimizing-layouts-201/source/browse/au_optimizin
在 ListAdapter 中,如何将数据添加到现有列表?submitList()只会用新列表替换现有列表, 也许一种更新数据的方法是这样的 adapter.addNewItem(list) 最佳答案
我在 RecyclerView 中遇到了分页无限滚动的问题,我正在使用 .addAll() 添加所有新项目 movieList.addAll(it.movieList) adapter.submi
我正在使用 Android Room和 Rx Flowable更新 RecyclerView将对象从我的数据库中。为了获得更好的动画和性能,我更新了项目以使用新的 ListAdapter在我的项目中。
我正在尝试将我的 Adapter 转换为 ListAdapter,以便将来包含 Paging 库。我的应用程序是一个基本的 2 fragment 笔记应用程序,第一个 fragment 具有回收器 V
我正在设置一个 RecyclerView,它使用 ListAdapter 来计算更改时的动画。 RecyclerView 通过 ViewModel 接收数据,ViewModel 通过 Firebase
我正在尝试使用 JSON 数据源中的项目列表填充 View 。我似乎无法让适配器在我的列表中显示 JSON 数据。这就是我到目前为止所做的。 MainActivity.java package com
我看了http://developer.android.com/reference/android/widget/ListAdapter.html并看到 ListAdapter 是一个公共(publi
我正在尝试获取 HashMap 的 ArrayList,以尝试获取可用的 ListAdapter。当我运行这个时,我得到一个 java.lang.RuntimeException: Your cont
好吧,所以我创建了一个 ListView 和我自己的 ListAdapter,但是每当我将结果添加到 ListAdapter 时,它都会崩溃,并显示 NullPointerException 当 fi
我有一个包含多个字段的类,我只想在 ListView 中显示其中一个字段 class FOO { String f1; String f2; String f3; }; FOO fo
我是一名优秀的程序员,十分优秀!