我想在产品列表的android中制作可扩展的列表视图,按类别分组。我使用phpmyadmin作为我的数据库。如何根据他们的类别向孩子展示? 我正在使用JSONParser& Php代码在这里。
第1类 - 产品A. - 产品B
第2类 - 产品C. - 产品D
这是显示标题(类别)
的php代码<?php
include("koneksi.php");
$q = mysql_query('select * from kategori_barang');
$v = '{"info" : [';
while($r=mysql_fetch_array($q))
{
$ob = array("<br>","<b>","</b>");
if(strlen($v)<12)
{
$v .= '{"id_kat" : "'.$r['id_kat'].'", "kat_barang" : "'.$r['kat_barang'].'"}';
}
else
{
$v .= '{"id_kat" : "'.$r['id_kat'].'", "kat_barang" : "'.$r['kat_barang'].'"}';
}
}
$v .= ']}';
echo $v;
?>
这是我的数据库
`product_id` int(4) NOT NULL AUTO_INCREMENT,
`cat_id` int(4) NOT NULL DEFAULT '0',
`subcat_id` int(4) NOT NULL DEFAULT '0',
`product_name` varchar(50) NOT NULL DEFAULT '',
`product_price` int(8) NOT NULL DEFAULT '0',
`product_weight` int(4) NOT NULL DEFAULT '0',
`product_weight_unit` varchar(10) NOT NULL DEFAULT '',
`product_price_unit` varchar(10) NOT NULL DEFAULT '',
`product_image` varchar(250) DEFAULT NULL,
`product_in_stock` int(8) DEFAULT NULL,
这是我的适配器
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblHeader = (TextView) convertView
.findViewById(R.id.lblHeader);
lblHeader.setTypeface(null, Typeface.BOLD);
lblHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
这是我的主要活动
public class MainActivity extends Activity {
String link_url = "";
String [] str=null;
public static final String AR_ID_KAT = "id_kat";
public static final String AR_KAT_BARANG = "kat_barang";
JSONArray str_login = null;
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<HashMap<String, String>> daftar_buku = new ArrayList<HashMap<String, String>>();
ProgressDialog mProgressDialog;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> arraylist;
HashMap<String, List<String>> listDataChild;
String id_kat, kat_barang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Sharani Designs");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params)
{
/*arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
jsonobject = JSONParser
.getJSONfromURL("http://api.androidhive.info/contacts/");
*/
JSONParser jParser = new JSONParser();
link_url = "http://10.0.2.2/login/test_kat.php";
JSONObject json = jParser.AmbilJson(link_url);
try {
str_login = json.getJSONArray("info");
for (int i = 0; i < str_login.length(); i++) {
JSONObject ar = str_login.getJSONObject(i);
String id = ar.getString(AR_ID_KAT);
String isb = ar.getString(AR_KAT_BARANG);
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(AR_ID_KAT, id);
map.put(AR_KAT_BARANG, isb);
daftar_buku.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
mProgressDialog.dismiss();
}
}