我已经制作了一个自定义适配器用于安装到android中的listView,我正在削减一些jsondata并希望进入列表视图,我已经尝试了但是它不是workig,我的代码如下,请帮助我
myactivity.java
package com.epe.yehki.ui;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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;
import android.widget.Toast;
import com.epe.yehki.adapter.CategoryAdapter;
import com.epe.yehki.adapter.ProductAdapter;
import com.epe.yehki.backend.ServiceHandler;
import com.epe.yehki.uc.Header;
import com.epe.yehki.util.Const;
import com.example.yehki.R;
public class ProSubCategoryActivity extends Activity {
int flag;
public Header header;
public TextView title;
Bitmap bitmap;;
private ProductAdapter productContent;
private CategoryAdapter categoryContent;
// PRODUCTS....
// arrayLists......
public static ArrayList<String> productArray;
public static ArrayList<String> categoryArray;
//
// contacts JSONArray
JSONArray subcategories = null;
JSONArray products = null;
public String catid;
public String id;
String name;
ListView lv;
JSONObject jsonObj;
// Hashmap for ListView
ArrayList<HashMap<String, String>> subcategoryList;
ArrayList<HashMap<String, String>> productList;
private ProgressDialog pDialog;
Intent in = null;
// new
public String proname;
public String prodesc;
public String proimg;
public String proMinOrderQty;
public String proMinPrice;
public String proMaxPrice;
public String proTerms;
public String proId;
// new
// URL to get contacts JSON
private static String url = "http://yehki.epagestore.in/app_api/categories.php";
private static String mainurl = "http://yehki.epagestore.in/";
public String suburl = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
this.header = (Header) findViewById(R.id.headersubcat);
title = (TextView) findViewById(R.id.title);
// getting intent data
categoryArray = new ArrayList<String>();
productArray = new ArrayList<String>();
in = getIntent();
lv = (ListView) findViewById(R.id.list);
categoryContent = new CategoryAdapter(this, categoryArray);
// Get JSON values from previous intent
try {
catid = in.getStringExtra(Const.TAG_CAT_ID);
name = in.getStringExtra(Const.TAG_CAT_NAME);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("::::::::::::::MY CATEGORY ID::::::::::::::IN SUB "
+ catid);
subcategoryList = new ArrayList<HashMap<String, String>>();
productList = new ArrayList<HashMap<String, String>>();
suburl = "http://yehki.epagestore.in/app_api/categories.php?"
+ Const.TAG_CAT_ID + "=" + catid;
System.out.println("::::::::::::::::MY SUBCATEGORY URL::::::::::::"
+ suburl);
title.setText(name);
// Displaying all values on the screen
new GetSubCategories().execute();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
if (flag == 0) {
String catname = ((TextView) view.findViewById(R.id.name))
.getText().toString();
in = new Intent(getApplicationContext(),
SubCategoryTwoActivity.class);
in.putExtra(Const.TAG_CAT_NAME, catname);
in.putExtra(Const.TAG_CAT_ID, catid);
startActivity(in);
} else {
in = new Intent(getApplicationContext(),
ProductDetailActivity.class);
proname = ((TextView) view.findViewById(R.id.product_label))
.getText().toString();
proId = ((TextView) view.findViewById(R.id.pro_id))
.getText().toString();
System.out
.println(":::::::::::::::;;THE INTENT FOR THE PRODUCUT DETIALS ACTIVITY================="
+ proname
+ " proDuct Id::::::::::::>>>>>>>>>"
+ proId);
in.putExtra(Const.TAG_PRODUCT_ID, proId);
in.putExtra(Const.TAG_PRODUCT_NAME, proname);
in.putExtra(Const.TAG_PRODUCT_IMG, proimg);
in.putExtra(Const.TAG_PRODUCT_MIN_ORDER_QTY, proMinOrderQty);
in.putExtra(Const.TAG_PRODUCT_MIN_PRICE, proMinPrice);
in.putExtra(Const.TAG_PRODUCT_MAX_PRICE, proMaxPrice);
in.putExtra(Const.TAG_PRODUCT_DESCRIPTION, prodesc);
in.putExtra(Const.TAG_PRODUCT_PAYMENT_TERMS, proTerms);
/*
* in.putExtra(TAG_CAT_NAME, p); in.putExtra(TAG_CAT_ID,
* catid);
*/
startActivity(in);
}
}
});
}
private class GetSubCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ProSubCategoryActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
System.out.println(":::::::::::::::::::SUB URL:::::::::::::::::"
+ suburl);
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(suburl, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
try {
if (jsonStr != null) {
jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
if (jsonObj.has(Const.TAG_CAT_LlIS)) {
System.out
.println("::::::::::::::::true::::::::::::::::"
+ jsonObj.has(Const.TAG_CAT_LlIS));
subcategories = jsonObj
.getJSONArray(Const.TAG_CAT_LlIS);
if (subcategories != null
&& subcategories.length() != 0) {
// looping through All Contacts
flag = 0;
System.out
.println(":::::::::::FLAG IN SUB:::::::::::"
+ subcategories.length());
for (int i = 0; i < subcategories.length(); i++) {
JSONObject c = subcategories.getJSONObject(i);
id = c.getString(Const.TAG_CAT_ID);
String name = c.getString(Const.TAG_CAT_NAME);
// tmp hashmap for single category
/*
* HashMap<String, String> subcategory = new
* HashMap<String, String>();
*
* // adding each child node to HashMap key =>
* // value subcategory.put(Const.TAG_CAT_ID,
* id); subcategory.put(Const.TAG_CAT_NAME,
* name);
*
* // adding contact to contact list
* subcategoryList.add(subcategory);
*/
// new adde 3=04=2014
// categoryArray.add(id);
categoryArray.add(name);
System.out
.println("::::::::::My category:::::::"
+ categoryArray.get(i)
.toString().trim());
}
}
} else if (jsonObj.has(Const.TAG_PRODUCT_LlST)) {
flag = 1;
System.out
.println("::::::::::::::::true::::::::::::::::"
+ jsonObj.has(Const.TAG_PRODUCT_LlST));
products = jsonObj.getJSONArray(Const.TAG_PRODUCT_LlST);
if (products != null && products.length() != 0) {
// looping through All Contacts
System.out
.println(":::::::::::FLAG IN SUB:::::::::::"
+ flag);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
id = c.getString(Const.TAG_PRODUCT_ID);
String proname = c
.getString(Const.TAG_PRODUCT_NAME);
String prodesc = c
.getString(Const.TAG_PRODUCT_DESCRIPTION);
String proimg = Const.API_HOST + "/"
+ c.getString(Const.TAG_PRODUCT_IMG);
System.out
.println(":::::::::::::::My Image Url:::::::::::::"
+ proimg);
String proMinOrderQty = c
.getString(Const.TAG_PRODUCT_MIN_ORDER_QTY);
String proMinPrice = c
.getString(Const.TAG_PRODUCT_MIN_PRICE);
String proMaxPrice = c
.getString(Const.TAG_PRODUCT_MAX_PRICE);
String proTerms = c
.getString(Const.TAG_PRODUCT_PAYMENT_TERMS);
System.out
.println(":::::::::::::My prododuct name+++++++:::::::::::::"
+ proname);
System.out
.println(":::::::::::::My prododuct image+++++++:::::::::::::"
+ proimg);
System.out
.println(":::::::::::::My prododuct min order qty+++++++:::::::::::::"
+ proMinOrderQty);
// tmp hashmap for single category
HashMap<String, String> product = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
product.put(Const.TAG_PRODUCT_ID, id);
product.put(Const.TAG_PRODUCT_NAME, proname);
product.put(Const.TAG_PRODUCT_IMG, proimg);
product.put(Const.TAG_PRODUCT_MIN_ORDER_QTY,
proMinOrderQty);
product.put(Const.TAG_PRODUCT_DESCRIPTION,
prodesc);
// adding contact to contact list
productList.add(product);
}
}
}
} else {
Log.e("ServiceHandler",
"Couldn't get any data from the url");
}
} catch (JSONException e) {
e.printStackTrace();
System.out
.println("::::::::::::::::::got an error::::::::::::");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
*
* */
if (flag == 0) {
lv.setAdapter(categoryContent);
} else {
Toast.makeText(ProSubCategoryActivity.this, "Please wait", 1)
.show();
/*
* ListAdapter adapter = new SimpleAdapter(
* ProSubCategoryActivity.this, productList,
* R.layout.activity_single_produt, new String[] {
* Const.TAG_PRODUCT_ID, Const.TAG_PRODUCT_NAME,
* Const.TAG_PRODUCT_IMG, Const.TAG_PRODUCT_MIN_ORDER_QTY,
* Const.TAG_PRODUCT_DESCRIPTION }, new int[] {
* R.id.product_label, R.id.iv_product_img, R.id.min_qty,
* R.id.pro_desc, R.id.pro_id }); setListAdapter(adapter);
*/
}
}
}
}
Adapter.java
package com.epe.yehki.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.yehki.R;
public class CategoryAdapter extends BaseAdapter {
private ArrayList<String> categoryArray;
private Context mContext;
public CategoryAdapter(Context paramContext,
ArrayList<String> paramArrayList) {
this.mContext = paramContext;
this.categoryArray = paramArrayList;
}
public int getCount() {
return this.categoryArray.size();
}
public void setAllItems(ArrayList<String> paramArrayList) {
this.categoryArray.addAll(paramArrayList);
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
if (paramView == null) {
paramView = localLayoutInflater.inflate(R.layout.list_item,
paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.categoryName = ((TextView) paramView
.findViewById(R.id.name));
paramView.setTag(localViewholder);
} else {
localViewholder = new Viewholder();
localViewholder = (Viewholder) paramView.getTag();
}
localViewholder.categoryName.setText(categoryArray.get(paramInt)
.indexOf(0));
return paramView;
}
static class Viewholder {
private TextView categoryName;
}
}
答案 0 :(得分:0)
我更改了您的代码,现在就试试
public class CategoryAdapter extends BaseAdapter {
private ArrayList<String> categoryArray;
private Context mContext;
private Viewholder localViewholder = null;
public CategoryAdapter(Context paramContext,
ArrayList<String> paramArrayList) {
this.mContext = paramContext;
this.categoryArray = paramArrayList;
}
public int getCount() {
return this.categoryArray.size();
}
public void setAllItems(ArrayList<String> paramArrayList) {
this.categoryArray.addAll(paramArrayList);
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
if (paramView == null) {
localViewholder = new Viewholder();
paramView = localLayoutInflater.inflate(R.layout.list_item,
paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.categoryName = ((TextView) paramView
.findViewById(R.id.name));
paramView.setTag(localViewholder);
} else {
localViewholder = (Viewholder) paramView.getTag();
}
localViewholder.categoryName.setText(categoryArray.get(paramInt)
.indexOf(0));
return paramView;
}
static class Viewholder {
private TextView categoryName;
}
}