Java 将列表视图的数据从Asynctask传递到自定义列表适配器类
我需要关于为自定义列表视图传递数据的帮助,我似乎无法将数据从asynctask传递到其他java类。这是我的密码:Java 将列表视图的数据从Asynctask传递到自定义列表适配器类,java,android,json,listview,android-asynctask,Java,Android,Json,Listview,Android Asynctask,我需要关于为自定义列表视图传递数据的帮助,我似乎无法将数据从asynctask传递到其他java类。这是我的密码: import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowMan
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.dmo.d2d.R;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Inventory extends Activity {
//Variables!!!!!!!!!
// url to get all products list
private static String url_all_products = "my server url :D";
// Get name and email from global/application context
final String accnt_user_name = globalVariable.getUserName();
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "vinID";
static final String TAG_CAR_NAME = "carName";
static final String TAG_VIN_ID = "vinID";
ListView list;
CustomInventoryList adapter;
JSONObject json;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();
// products JSONArray
JSONArray products = null;
// contacts JSONArray
JSONArray contacts = null;
JSONArray account = null;
JSONArray cars = null;
JSONArray user_names = null;
JSONObject jsonObj;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.inventory_list);
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username_GET", accnt_user_name ));
// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
runOnUiThread(new Runnable() {
public void run() {
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String vinID = c.getString("vinID");
String name = c.getString("carName").replaceAll("_", " ");
// globalVariable.setUserComment(dealer);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CAR_NAME, name);
map.put(TAG_VIN_ID, vinID);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
list=(ListView)findViewById(R.id.list);
adapter=new CustomInventoryList(this, productsList); << I CAN'T PASS THIS DATA!!!
list.setAdapter(adapter);
}
}
}
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.AdapterView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
进口com.dmo.d2d.R ;;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公共类库存扩展活动{
//变量!!!!!!!!!
//获取所有产品列表的url
私有静态字符串url\u all\u products=“我的服务器url:D”;
//从全局/应用程序上下文获取名称和电子邮件
最终字符串accnt\u user\u name=globalVariable.getUserName();
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“vinID”;
静态最终字符串TAG\u CAR\u NAME=“carName”;
静态最终字符串标记\u VIN\u ID=“vinID”;
列表视图列表;
CustomInventoryList适配器;
JSONObject json;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList=新的ArrayList();
//产品JSONArray
JSONArray产品=null;
//联系JSONArray
JSONArray联系人=null;
JSONArray帐户=null;
JSONArray cars=null;
JSONArray user_names=null;
JSONObject jsonObj;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//删除标题栏
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
//删除通知栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
//在上述顺序之后设置内容视图(以避免崩溃)
这个.setContentView(R.layout.inventory\u列表);
新建LoadAllProducts().execute();
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//建筑参数
List params=new ArrayList();
参数add(新的BasicNameValuePair(“username_GET”,accnt_user_name));
//从URL获取JSON字符串
json=jParser.makeHttpRequest(url\u所有产品,“GET”,参数);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
runOnUiThread(新的Runnable(){
公开募捐{
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签、车辆名称、名称);
地图放置(标记VIN ID,VIN ID);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}否则{
//没有发现任何产品
//启动添加新产品活动
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
});
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
list=(ListView)findViewById(R.id.list);
适配器= NealCudioSooLogyLIST(这个,产品清单);首先,您应该考虑将代码中的可运行块移到 >代码> OnPistExc>/COD>。这就是为什么它被设计为后台任务的UI线程操作。
请注意,您的异步任务
类型已更改
您还可以考虑通过使代码< <代码>静态< /代码>独立于您的活动,使您的<代码>异步任务< /代码>。然后您只需传递其构造函数中的任务所需的参数,再加上对该活动的引用,以便它可以在<>代码> OnPristCux< /Cl>中返回结果。
这个想法:
private ListView mList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// ...
mlist = ...
new LoadAllProducts(this, username, params, ...); // PASS ALL THE PARAMTERS THE ASYNCTASK NEEDS
}
public void populateList(ArrayList<HashMap<String, String>> productsList) {
mList.setAdapter(new CustomInventoryList(this, productsList));
}
static class LoadAllProducts extends AsyncTask<String, String, JSONObject> {
private String username;
private List<NameValuePair> params;
private Activity mActivity;
public LoadAllProducts(...) {
username = ...;
params = ...;
mActivity = ...;
}
@Override
protected String doInBackground(String... args) {
...
// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);
return json;
}
protected void onPostExecute(JSONObject json) {
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// ... handle your JSON
}
} catch (JSONException e) {
e.printStackTrace();
}
mActivity.populateList(productsList);
}
}
private-ListView-mList;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// ...
mlist=。。。
new LoadAllProducts(this、username、params等);//传递异步任务所需的所有参数
}
public void populateList(ArrayList productsList){
setAdapter(新的CustomInventoryList(这个,productsList));
}
静态类LoadAllProducts扩展了AsyncTask{
私有字符串用户名;
私有列表参数;
私人活动mActivi
private ListView mList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// ...
mlist = ...
new LoadAllProducts(this, username, params, ...); // PASS ALL THE PARAMTERS THE ASYNCTASK NEEDS
}
public void populateList(ArrayList<HashMap<String, String>> productsList) {
mList.setAdapter(new CustomInventoryList(this, productsList));
}
static class LoadAllProducts extends AsyncTask<String, String, JSONObject> {
private String username;
private List<NameValuePair> params;
private Activity mActivity;
public LoadAllProducts(...) {
username = ...;
params = ...;
mActivity = ...;
}
@Override
protected String doInBackground(String... args) {
...
// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);
return json;
}
protected void onPostExecute(JSONObject json) {
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// ... handle your JSON
}
} catch (JSONException e) {
e.printStackTrace();
}
mActivity.populateList(productsList);
}
}