gpt4 book ai didi

android - ProgressDialog、RunOnUiThread 和 ListAdapter 的问题

转载 作者:行者123 更新时间:2023-11-30 00:11:47 25 4
gpt4 key购买 nike

好吧,我已经编写了这个 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/

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