带有JSON的可扩展列表视图

时间:2013-11-16 06:06:20

标签: android expandablelistview

嘿我正在尝试从我的数据库中获取数据并在ExpandableListView上显示。 JSON时,应用程序会继续崩溃。但在此之前它运行正常。

我的适配器类

public class ExpandableListAdapterA extends BaseExpandableListAdapter 
{

private Activity context;
private Map<String, List<String>> laptopCollections;
private List<String> laptops;

public ExpandableListAdapterA(Activity context, List<String> laptops,
        Map<String, List<String>> laptopCollections) {
    this.context = context;
    this.laptopCollections = laptopCollections;
    this.laptops = laptops;
}

public Object getChild(int groupPosition, int childPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String laptop = (String) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.child_view, null);
    }

    TextView item = (TextView) convertView.findViewById(R.id.laptop);

    ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
    delete.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("Do you want to remove?");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            List<String> child =
                                laptopCollections.get(laptops.get(groupPosition));
                            child.remove(childPosition);
                            notifyDataSetChanged();
                        }
                    });
            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });

    item.setText(laptop);
    return convertView;
}

public int getChildrenCount(int groupPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    return laptops.get(groupPosition);
}

public int getGroupCount() {
    return laptops.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String laptopName = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.parent_view,
                null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.head);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(laptopName);
    return convertView;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
 }

我的其他课程

public class Product extends Activity {

List<String> groupList;
List<String> childList;
Map<String, List<String>> laptopCollection;
ExpandableListView expListView;
private static String url = "http://10.0.2.2/Monoprix/DB_Connect.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.products);

    new createGroupList().execute();



    expListView = (ExpandableListView) findViewById(R.id.products);
    final ExpandableListAdapter expListAdapter = new ExpandableListAdapterA(this, groupList, laptopCollection);
    expListView.setAdapter(expListAdapter);

    //setGroupIndicatorToRight();

    expListView.setOnChildClickListener(new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            final String selected = (String) expListAdapter.getChild(
                    groupPosition, childPosition);
            Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
                    .show();

            return true;
        }
    });
}



class createGroupList extends AsyncTask<String,String,String> {
    protected String doInBackground(String... args) {
    groupList = new ArrayList<String>();
    String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540",
    "HP Envy 4-1025TX" };

    //String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series","ThinkPad X Series", "Ideapad Z Series" };
    String[] dellModels = { "Inspiron", "Vostro", "XPS" };
    try{
        JSONParser jsonParser = new JSONParser();
        JSONObject json = jsonParser.getJSONFromUrl(url);
        Log.d("Create Response", json.toString());
        JSONArray title = json.getJSONArray("product");
        for (int i=0;i<title.length();i++)
        {
            JSONObject c = title.getJSONObject(i);
            String categ = c.getString("product_category");
            groupList.add(categ);
        }
        childList = new ArrayList<String>();
        laptopCollection = new LinkedHashMap<String, List<String>>();
        for (String laptop : groupList) {
            if (laptop.equals("HP")) {
                for ( String model : hpModels)
                    childList.add(model);
            } else if (laptop.equals("Dell"))
                for (String model : dellModels )
                    childList.add(model);

            laptopCollection.put(laptop, childList);
        }

 }catch (JSONException e)
{
    e.printStackTrace();
}
    return null;
    }
}


public int getDipsFromPixel(float pixels) {
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}
 }

XML文件 包含expandablelist的xml页面 products.xml

<ExpandableListView
    android:id="@+id/products"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ExpandableListView>

parent_view.xml

<TextView
    android:id="@+id/head"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="25dp"/>

child_view.xml

<TextView
    android:id="@+id/laptop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:paddingLeft="25dp" />

<ImageView
    android:id="@+id/delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/ic_delete" />

0 个答案:

没有答案