gpt4 book ai didi

Android 将 json 解析为可扩展 View

转载 作者:行者123 更新时间:2023-11-30 01:18:32 24 4
gpt4 key购买 nike

我想实现这样的东西:

i want to implement  like this

这是父 View 。当我点击它时,我必须显示它的 ExpandableListView: This is parent view when i click on this i have to display its expandable view

public class MainActivity extends AppCompatActivity {
ProgressDialog PD;

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;
ArrayList<Group> list = new ArrayList<Group>();
ArrayList<Child> ch_list= new ArrayList<Child>();``
String url=ApplicationConstant.THIRD_WEB_CALL_HISTORY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_history);
ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

PD = new ProgressDialog(this);
PD.setMessage("Loading.....");
PD.setCancelable(false);
new HistoryAsynktask().execute(url);

}
class HistoryAsynktask extends AsyncTask<String ,String ,String> {

@Override
protected String doInBackground(String... uri) {
HttpClient httpClient= new DefaultHttpClient();
HttpResponse httpResponse;
String histry_result = null;

try {
httpResponse=httpClient.execute(new com.loopj.android.http.HttpGet(uri[0]));
StatusLine statusLine=httpResponse.getStatusLine();
if (statusLine.getStatusCode()== HttpStatus.SC_OK){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(byteArrayOutputStream);
histry_result=byteArrayOutputStream.toString();
JSONObject jsonObject= new JSONObject(histry_result);
JSONObject jsonObject1= jsonObject.getJSONObject("GetLastTxnsCustResult");
String status= jsonObject1.getString("status");
JSONArray jsonArray = jsonObject1.getJSONArray("txns");

for (int i =0 ;i< jsonArray.length();i++)
{
Child child = new Child();
Group group= new Group();
JSONObject jsonObject2= jsonArray.getJSONObject(i);
String address=jsonObject2.getString("address");
String city =jsonObject2.getString("city");
String last_4 = jsonObject2.getString("last_4");
String merch_name =jsonObject2.getString("merch_name");
String phone = jsonObject2.getString("phone");
String state = jsonObject2.getString("state");
String tran_id =jsonObject2.getString("tran_id");
String txn_amt = jsonObject2.getString("txn_amt");
String txn_date = jsonObject2.getString("txn_date");
String zip= jsonObject2.getString("zip");
group.setMerch_name(merch_name);
group.setTxn_amt(txn_amt);
group.setTxn_date(txn_date);
child.setName(address);
ch_list.add(child);
list.add(group);

}


}


} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return histry_result;
}

@Override
protected void onPreExecute() {
super.onPreExecute();



}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
PD.dismiss();


ExpAdapter = new ExpandListAdapter(
History.this, list);
ExpandList.setAdapter(ExpAdapter);

}
}
}

我的适配器类是:

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;


public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.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) {

Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item, null);
}

TextView tv = (TextView) convertView.findViewById(R.id.country_name);

tv.setText(child.getName().toString());
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.trns);
TextView date=(TextView) convertView.findViewById(R.id.dateTime);
TextView trn_amt=(TextView) convertView.findViewById(R.id.txnmnt);
tv.setText(group.getMerch_name());
date.setText(group.getTxn_date());
trn_amt.setText(group.getTxn_amt());
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

}

最佳答案

在下面的链接中,您可以在 listDataHeader 中添加标题数据,在 listDataChild 中添加子数据,并进行以下一些更改。

HashMap< String, List< Modelclass>> parent;
List< Modelclass > comingSoon1;
for (int i =0 ;i< jsonArray.length();i++)
{
parent = new HashMap<String, List<Modelclass>>();
comingSoon1 = new ArrayList<Modelclass>();
Group group=new Group();
JSONObject jsonObject2= jsonArray.getJSONObject(i);
String address=jsonObject2.getString("address");
String city =jsonObject2.getString("city");
String last_4 = jsonObject2.getString("last_4");
String merch_name =jsonObject2.getString("merch_name");
String phone = jsonObject2.getString("phone");
String state = jsonObject2.getString("state");
String tran_id =jsonObject2.getString("tran_id");
String txn_amt = jsonObject2.getString("txn_amt");
String txn_date = jsonObject2.getString("txn_date");
String zip= jsonObject2.getString("zip");
group.setMerch_name(merch_name);
group.setTxn_amt(txn_amt);
group.setTxn_date(txn_date);
comingSoon1.add(group);
parent.put("Your Heading", comingSoon1);
}

how to add expandable listview inside material design navigation drawer in android?

关于Android 将 json 解析为可扩展 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com