我已成功从Api获取Array List中的数据。但是当我试图获得可扩展子视图的值时,我没有得到适当的价值。请建议我错在哪里。 这是我的整个实施请给我建议我错了。
这是我的代码:
public class Ex_Lv extends AppCompatActivity {
private ArrayList<ModelGroup> modelgroup = null;
private ArrayList<ModelChild> modelchild = null;
private ArrayList<Category> alCategory;
private ArrayList<ArrayList<ModelChild>> ListChildXXXXXXXXX= null;
String result;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex__lv);
new AsyncGetExpandable().execute();
initData();
initUI();
}
private void initUI() {
toolbar= (Toolbar)findViewById(R.id.toolbarrEx_Lw);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.icon_back_white);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
}
private void initData() {
alCategory = new ArrayList<>();
modelgroup = new ArrayList<ModelGroup>();
modelchild = new ArrayList<ModelChild>();//ArrayList<ArrayList<ChildTerbaru>>>();
ListChildXXXXXXXXX = new ArrayList<ArrayList<ModelChild>>();
}
public class AsyncGetExpandable extends AsyncTask<Void, Void, AsyncTaskResult<Object>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
findViewById(R.id.rlProgress).setVisibility(View.VISIBLE);
}
@Override
protected AsyncTaskResult<Object> doInBackground(Void... params) {
BufferedReader in = null;
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams
.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);
String url = AppConstants.URL_GET_CATEGORIES;
if (AppConstants.DEBUG) Log.v(AppConstants.DEBUG_TAG, "CATEGORIES URL : " + url);
HttpGet request = new HttpGet(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("token", "Cq2uOA1Rvu5Pvtnt6XZifC9CAyNR4ISS3MsS6jDZ");
if (AppConstants.DEBUG)
Log.v(AppConstants.DEBUG_TAG, "BEARER : ");
HttpResponse response = null;
response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
result = sb.toString();
if (AppConstants.DEBUG)
Log.v(AppConstants.DEBUG_TAG, "CATEGORIES RESPONSE : " + result);
return new AsyncTaskResult<Object>(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return new AsyncTaskResult<Object>("OK");
}
@Override
protected void onPostExecute(AsyncTaskResult<Object> result) {
super.onPostExecute(result);
findViewById(R.id.rlProgress).setVisibility(View.GONE);
if (result.getError() != null) {
} else {
try {
String resulta = result.getResult().toString();
JSONObject jsonRoot = null;
try {
jsonRoot = new JSONObject(resulta);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = null;
try {
jsonArray = jsonRoot.getJSONArray("category");
} catch (JSONException e) {
e.printStackTrace();
}
ModelGroup LT = null;
ModelChild CT = null;
for (int i = 0; i < jsonArray.length(); i++) {
modelchild = new ArrayList<ModelChild>();
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
LT = new ModelGroup();
try {
LT.setCat_name(jsonObject.getString("category_name_en"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
LT.setCat_id(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
modelgroup.add(LT);
// for (int j = 0; j < jsonArray.length(); j++) {
CT = new ModelChild();
JSONObject jsonObject2=null;
JSONArray jsonArray1= jsonObject.getJSONArray("sub_category");
for(int j=0; j < jsonArray1.length();j++){
jsonObject2= jsonArray1.getJSONObject(j);
}
try {
CT.setSub_category_name_en(jsonObject2.getString("sub_category_name_en"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
CT.setCat_id(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
modelchild.add(CT);//get(i).get(i).add(CT);
ListChildXXXXXXXXX.add(modelchild);
ExpandableListView list = (ExpandableListView)findViewById(R.id.lvExp) ;
list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
final ExpandableAdpeter adapter = new ExpandableAdpeter(getApplicationContext(), modelgroup, ListChildXXXXXXXXX);
list.setAdapter(adapter);
/* list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//get the group header
ModelChild CT;
Toast.makeText(getApplicationContext(),modelchild.get(groupPosition).getSub_category_name_en(),Toast.LENGTH_SHORT).show();
return false;
}
});*/
// setOnGroupClickListener listener for group heading click
list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//get the group header
ModelGroup LT=modelgroup.get(groupPosition);
Toast.makeText(getBaseContext(), " Header is :: " + LT.getCat_name(),Toast.LENGTH_LONG).show();
return false;
}
});
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}`
**But i want to print value of Group and related Child **
list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//get the group header
ModelChild CT;
Toast.makeText(getApplicationContext(),modelchild.get(groupPosition).getSub_category_name_en(),Toast.LENGTH_SHORT).show();
return false;
}
});
// setOnGroupClickListener listener for group heading click
list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//get the group header
ModelGroup LT=modelgroup.get(groupPosition);
Toast.makeText(getBaseContext(), " Header is :: " + LT.getCat_name(),Toast.LENGTH_LONG).show();
return false;
}
});
}
这是我的适配器,我设置了getcat_name和get_id: 请给我建议
public class ExpandableAdpeter extends BaseExpandableListAdapter {
Context context;
ArrayList<ModelGroup> ListTerbaru;
ArrayList<ArrayList<ModelChild>> ListChildTerbaru;
int count;
public ExpandableAdpeter (Context context, ArrayList<ModelGroup>ListTerbaru, ArrayList<ArrayList<ModelChild>> ListChildTerbaru){
this.context=context;
this.ListTerbaru=ListTerbaru;
this.ListChildTerbaru=ListChildTerbaru;
// this.count=ListTerbaru.size();
// this.count=ListChildTerbaru.size();
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public ModelChild getChild(int groupPosition, int childPosition) {
return ListChildTerbaru.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ModelChild childTerbaru = getChild(groupPosition, childPosition);
ViewHolder holder= null;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_child, null);
holder=new ViewHolder();
holder.begdate1=(TextView)convertView.findViewById(R.id.tv_listchild);
holder.enddate1=(TextView)convertView.findViewById(R.id.tv_listchilds);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.begdate1.setText(childTerbaru.getCat_id());
holder.enddate1.setText(childTerbaru.getSub_category_name_en());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return ListChildTerbaru.get(groupPosition).size();
}
@Override
public ModelGroup getGroup(int groupPosition) {
return ListTerbaru.get(groupPosition);
}
@Override
public int getGroupCount() {
return ListTerbaru.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ModelGroup terbaruModel = (ModelGroup) getGroup(groupPosition);
ViewHolder holder= null;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
holder=new ViewHolder();
holder.nama=(TextView)convertView.findViewById(R.id.tv_listtitle);
holder.alamat=(TextView)convertView.findViewById(R.id.tv_listtitles);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.nama.setText(terbaruModel.getCat_name());
holder.alamat.setText(terbaruModel.getCat_id());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
static class ViewHolder{
TextView begdate1, enddate1,nama, alamat, imageid;
}
}
答案 0 :(得分:0)
/Put the below line of code inside your getChildView of adapter/
final String childText = (String) getChild(groupPosition, childPosition);
and Copy and paste the below override method inside adapter
@Override
public Object getChild(int groupPosition, int childPosititon)
{
return this.modelchild.get(this. modelgroup.get(groupPosition)).get(childPosititon);
}
答案 1 :(得分:0)
@Override
public ModelChild getChild(int groupPosition, int childPosition) {
return this.ListChildTerbaru.get(this.ListChildTerbaru.groupPosition).get(childPosition);
}
/Now in getChildView/
final String childText = (String) getChild(groupPosition, childPosition);
Log.i("Adapter", "getChildView childText: "+childText);
if (convertView == null){
----
-----
}