- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我已经编写了这个 Android 程序,它有 3 个选项卡(餐食、饮料和甜点),每个选项卡都有一个 fragment 在自己的 Activity 中。我现在遇到的问题是从 mysql 数据库检索列表并显示此列表我已经运行并执行了一个自定义代码,该代码在单个列表中显示从 mysql 数据库检索到的项目列表,效果很好。现在将此代码引入我的程序会出现 3 个错误
1)
pDialog = new ProgressDialog(MealsFragment.this); which says
ProgressDialog(android.context,Context) in progressDialog cannot be applied to
com.example.tab.tablayout.MealsFragment
2) runOnUiThread(new Runnable() 表示“无法解析 runonuithread 方法”
3)都是关于列表适配器的。
初始代码使用OnCreate,但我的 fragment 使用oncreateView。我不知道这是否是问题所在。
这是 MealsFragment.java 的代码
package com.example.tabs.tablayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/**
* Created by GIGABYTE on 5/21/2014.
*/
public class MealsFragment extends ListFragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> mealsList;
// url to get all products list
private static String url_all_meals = "http://10.180.79.73/dbase_connect/get_all_meals.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_MEALS = "meals";
private static final String TAG_MID = "mid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray meals = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_meals, container, false);
// Hashmap for ListView
mealsList = new ArrayList<HashMap<String, String>>();
// Loading meals in Background Thread
new LoadAllMeals().execute();
// Get listview
ListView lv = getListView();
return rootView;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllMeals extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MealsFragment.this);
pDialog.setMessage("Loading Meals. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All Meals from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_meals, "GET", params);
// Check your log cat for JSON response
Log.d("All Meals: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Meals found
// Getting Array of Meals
meals = json.getJSONArray(TAG_MEALS);
// looping through All meals
for (int i = 0; i < meals.length(); i++) {
JSONObject c = meals.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_MID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_MID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
mealsList.add(map);
}
} /*else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
*/
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all meals
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MealsFragment.this, mealsList,
R.layout.meals_list_item, new String[] {TAG_MID,
TAG_NAME},
new int[] { R.id.mid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
最佳答案
1) ProgressDialog 构造函数的第一个参数必须是上下文。使用
new ProgressDialog(getActivity());
2) 您可以使用getActivity().runOnUiThread(...)
。然而,在这种情况下这不是必需的。 AsyncTask.onPostExecute()
方法已在 UI 线程中运行。只需将 Runnable 代码移至 onPostExecute()
方法的主体中即可。
3) 与1相同,第一个参数必须是上下文。使用new SimpleAdapter(getActivity(), ...)
。
关于android - ProgressDialog、RunOnUiThread 和 ListAdapter 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23984486/
我正在实现 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
我是一名优秀的程序员,十分优秀!