Android 带有Json数组的ExpandableListAdapter
我试图用Android 带有Json数组的ExpandableListAdapter,android,json,expandablelistadapter,Android,Json,Expandablelistadapter,我试图用ExpandableListAdapter编写一个应用程序,但我将用json整型我的数据库(MYSQL),因此我遇到了以下问题: Caused by: java.lang.NullPointerException at com.example.smart_survey.ExpandableListAdapter.getGroupCount (ExpandableListAdapter.java:71) 有人能帮我吗 类ExpandableListAdapter: public cla
ExpandableListAdapter
编写一个应用程序,但我将用json整型我的数据库(MYSQL),因此我遇到了以下问题:
Caused by: java.lang.NullPointerException at
com.example.smart_survey.ExpandableListAdapter.getGroupCount
(ExpandableListAdapter.java:71)
有人能帮我吗
类ExpandableListAdapter
:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
**private List<String> _listDataHeader= null;** // 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 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;
}
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
**私有列表_listDataHeader=null;**//标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
**这。_listDataHeader=listDataHeader**
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView
.findviewbyd(R.id.lblListItem);
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
类QR
public class QR extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
ArrayList<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all products list
private static String url_all_quests =
"http://10.0.2.2/android_connect/listquest.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_QUEST = "Question";
private static final String TAG_REP = "Reponse";
private static final String TAG_IDE = "idenq";
private static final String TAG_IDQ = "idquest";
private static final String TAG_LIBQ = "libquest";
private static final String TAG_LIBR = "librep";
// products JSONArray
String idenq;
String idquest;
String libquest,librep;
Button valider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_quest);
Intent i = getIntent();
idenq = i.getStringExtra(TAG_IDE);
idquest = i.getStringExtra(TAG_IDQ);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.list);
// preparing list data
new prepareListData().execute();
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;
}
});
}
/*
* Preparing the list data
*/
class prepareListData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("idenq", idenq));
params1.add(new BasicNameValuePair("idquest", idquest));
params1.add(new BasicNameValuePair("libquest", libquest));
params1.add(new BasicNameValuePair("librep", librep));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(
url_all_quests, "GET", params1);
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// products found
// Getting Array of Products
JSONArray Question;
try {
Question = json.getJSONArray(TAG_QUEST);
// looping through All Products
for (int i = 0; i < Question.length(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Storing each json item in variable
JSONObject c=Question.getJSONObject(i);
String libquest = c.getString(TAG_LIBQ);
map.put(TAG_LIBQ, libquest);
// Adding child data
listDataHeader.add(libquest);
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
listDataChild.put(listDataHeader.get(i), top250); // Header, Child data
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
公共类QR扩展活动{
可扩展列表适配器;
ExpandableListView解释视图;
ArrayList listDataHeader;
HashMapListDataChild;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
//获取所有产品列表的url
私有静态字符串url\u所有任务=
"http://10.0.2.2/android_connect/listquest.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_QUEST=“Question”;
私有静态最终字符串标记\u REP=“response”;
私有静态最终字符串标记_IDE=“idenq”;
私有静态最终字符串标记\u IDQ=“idquest”;
私有静态最终字符串标记_LIBQ=“libquest”;
私有静态最终字符串标记_LIBR=“librep”;
//产品JSONArray
字符串idenq;
字符串idquest;
字符串libquest,librep;
按钮稳定器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_quest);
Intent i=getIntent();
idenq=i.getStringExtra(标记IDE);
idquest=i.getStringExtra(TAG_IDQ);
//获取列表视图
expListView=(ExpandableListView)findViewById(R.id.list);
//准备列表数据
新建prepareListData().execute();
listAdapter=新的ExpandableListAdapter(此,listDataHeader,
listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
//Listview组单击侦听器
expListView.setOnGroupClickListener(新OnGroupClickListener(){
@凌驾
公共布尔值onGroupClick(ExpandableListView父视图,视图v,
int groupPosition,长id){
//Toast.makeText(getApplicationContext(),
//“组单击”+listDataHeader.get(groupPosition),
//吐司。长度(短)。show();
返回false;
}
});
//Listview组扩展侦听器
expListView.setOnGroupExpandListener(新的OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int groupPosition){
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition)+”
扩大“,
吐司。长度(短)。show();
}
});
//Listview组压缩侦听器
expListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@凌驾
公共void-onGroupCollapse(int-groupPosition){
Toast.makeText(getApplicationContext(),
class prepareListData extends AsyncTask<String, String, ArrayList<String>> {
protected void onPostExecute(ArrayList<String> result) {
listDataHeader = result;
}
@Override
protected String doInBackground(String... params) {
// ......
return localListDataHeader;
}
}