如何在可扩展列表视图中绑定 Json 数据?
Posted
技术标签:
【中文标题】如何在可扩展列表视图中绑定 Json 数据?【英文标题】:How to Bind Json Data in Expandale List view? 【发布时间】:2017-10-04 09:00:52 【问题描述】:我想将 json 数据绑定到 android 中的可扩展列表视图。 显示以下错误
错误:
Error:(175, 65) error: incompatible types: MainActivity.ExecuteTask cannot be converted to Context
Json 数据:
["PART_ID":"01684.021","SHIFT_DATE":"20-SEP-17","PLAN_QTY":"2","ACTUAL_QTY":"2",
"PART_ID":"0346001.020","SHIFT_DATE":"20-SEP-17","PLAN_QTY":"4","ACTUAL_QTY":"4",
"PART_ID":"01068.021","SHIFT_DATE":"20-SEP-17","PLAN_QTY":"7","ACTUAL_QTY":"7"]
ExpandableListAdapter.java
package com.example.saravanakumars.expandable_listview;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by saravanakumars on 10/3/2017.
*/
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;
// ArrayList<TerbaruModel>ListTerbaru;
// ArrayList<ArrayList<ChildTerbaru>> ListChildTerbaru;
// int count;
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 childPosititon)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
@Override
public long getChildId(int groupPosition, int childPosition)
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.lblListItem);
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()
return this._listDataHeader.size();
@Override
public long getGroupId(int groupPosition)
return groupPosition;
@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 lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
@Override
public boolean hasStableIds()
return false;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
MainActivity.java
package com.example.saravanakumars.expandable_listview;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ExpandableListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
String s1 = "CLASIFICACION";
String s2 = "ONE PIECE";
String s3 = "159-OP SHERIDAN";
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private static String url = "http://113.193.30.155/MobileService/MobileService.asmx/GetPartbyProcessFamilyModel";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// expListView = (ExpandableListView) findViewById(R.id.lvExp);
//
// // preparing list data
// prepareListData();
//
// listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
//
// // setting list adapter
// expListView.setAdapter(listAdapter);
new ExecuteTask().execute(s1, s2, s3);
/*
* Preparing the list data
// */
// private void prepareListData()
// listDataHeader = new ArrayList<String>();
// listDataChild = new HashMap<String, List<String>>();
//
// // Adding child data
// listDataHeader.add("Defect");
// listDataHeader.add("Production");
// listDataHeader.add("Report");
//
// // Adding child data
// List<String> Defect = new ArrayList<String>();
// Defect.add("Defect-1");
// Defect.add("Defect-2");
// Defect.add("Defect-3");
// Defect.add("Defect-4");
// Defect.add("Defect-5");
//
//
// List<String> Production = new ArrayList<String>();
// Production.add("Production-1");
// Production.add("Production-2");
// Production.add("Production-3");
// Production.add("Production-4");
// Production.add("Production-5");
//
//
// List<String> Report = new ArrayList<String>();
// Report.add("Report-1");
// Report.add("Report-2");
// Report.add("Report-3");
// Report.add("Report-4");
// Report.add("Report-5");
//
// listDataChild.put(listDataHeader.get(0), Defect); // Header, Child data
// listDataChild.put(listDataHeader.get(1), Production);
// listDataChild.put(listDataHeader.get(2), Report);
//
class ExecuteTask extends AsyncTask<String, Integer, String>
@Override
protected String doInBackground(String... params)
String res = PostData(params);
return res;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
// Toast.makeText(getApplicationContext(),
// result,Toast.LENGTH_LONG)
// .show();
fetchResponse(result.replace("\n", "").trim());
pDialog.dismiss();
private void fetchResponse(String result)
if (!result.equals(""))
try
JSONArray jsonArray = new JSONArray(result);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("SHIFT_DATE");
listDataHeader.add("PART_ID");
listDataHeader.add("PLAN_QTY");
// Adding child data
List<String> SHIFT_DATE = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++)
JSONObject jsonObject = jsonArray.getJSONObject(i);
jsonObject = jsonArray.getJSONObject(i);
SHIFT_DATE.add(jsonObject.getString("SHIFT_DATE"));
List<String> PART_ID = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++)
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
jsonObject1 = jsonArray.getJSONObject(i);
PART_ID.add(jsonObject1.getString("PART_ID"));
List<String> PLAN_QTY = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++)
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
jsonObject2 = jsonArray.getJSONObject(i);
PLAN_QTY.add(jsonObject2.getString("PLAN_QTY"));
listDataChild.put(listDataHeader.get(0), SHIFT_DATE); // Header, Child data
listDataChild.put(listDataHeader.get(1), PART_ID);
listDataChild.put(listDataHeader.get(2), PLAN_QTY);
//
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
catch (JSONException e)
e.printStackTrace();
public String PostData(String[] valuse)
String s = "";
try
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://113.193.30.155/MobileService/MobileService.asmx/GetPartbyProcessFamilyModel");
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("strProcess", s1));
list.add(new BasicNameValuePair("strFamily", s2));
list.add(new BasicNameValuePair("strModel", s3));
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
s = readResponse(httpResponse);
// HttpEntity entity = httpResponse.getEntity();
//
// s = EntityUtils.toString(entity);
catch (Exception exception)
return s;
public String readResponse(HttpResponse res)
InputStream is = null;
String return_text = "";
try
is = res.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuffer sb = new StringBuffer();
while ((line = bufferedReader.readLine()) != null)
sb.append(line);
return_text = sb.toString();
catch (Exception e)
return return_text;
帮我解决这个问题。
【问题讨论】:
Java 基础知识:this
关键字是什么以及它是如何工作的...
How can "this" of the outer class be accessed from an inner class?的可能重复
【参考方案1】:
改变这个:
listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
到:
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
【讨论】:
日志猫:致命异常:主进程:com.example.saravanakumars.expandable_listview,PID:20712 java.lang.NullPointerException:尝试调用虚拟方法'void android.app.ProgressDialog.dismiss() ' 在空对象引用上以上是关于如何在可扩展列表视图中绑定 Json 数据?的主要内容,如果未能解决你的问题,请参考以下文章