好的我已经编写了这个android程序,它有3个标签(MEALS,DRINKS和DESSERT),每个标签都有自己的活动片段。我现在遇到的问题是从mysql数据库中检索列表并显示这个列表在每个tab.I'de已经运行并执行了一个自定义代码,显示从单个列表中的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,但我的片段使用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);
}
});
}
}
}
答案 0 :(得分:0)
1)ProgressDialog构造函数的第一个参数必须是上下文。使用
new ProgressDialog(getActivity());
2)您可以使用getActivity().runOnUiThread(...)
。但是,在这种情况下,这不是必需的。 AsyncTask.onPostExecute()
方法已在UI线程中运行。只需将Runnable代码移动到onPostExecute()
方法的主体中即可。
3)与1相同,第一个参数必须是上下文。使用new SimpleAdapter(getActivity(), ...)
。