gpt4 book ai didi

android - 如何制作动态可扩展 ListView ?

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:34 25 4
gpt4 key购买 nike

我正在制作一个动态的可扩展 ListView ,我想在其中显示标题(父级)中的标题和父级中的其他值,这意味着子级。我能够从服务器获取值(value)并希望在可扩展 ListView 中显示该值。我可以在 Header 中设置名称,但我无法在 child 中显示其他值,我应该怎么做,请帮助我。

我正在关注 this tutorial

这是Activity中的代码

public class Pxe extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
String myJSON,company_name,from_date,to_date,location,fire_id,guard_number;
JSONArray peoples = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_main);

// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);

// preparing list data
// prepareListData();
getProp();

}




public void getProp(){
class GetDataJSON extends AsyncTask<String, Void, String> {
public void onPreExecute() {
// Pbar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {

InputStream inputStream = null;
String result = null;
try {

URL url = new URL("http://xxxxxxxxx/app/guard/guard_history.php");
JSONObject postDataParams = new JSONObject();
Log.e("Value>>>>>", String.valueOf(postDataParams));
postDataParams.put("guard_id", "1");
Log.e("params", postDataParams.toString());

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));

writer.flush();
writer.close();
os.close();

int responseCode=conn.getResponseCode();

if (responseCode == HttpsURLConnection.HTTP_OK) {

BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder("");
String line="";
while ((line = in.readLine()) != null)
{
sb.append(line).append("\n");
}
result = sb.toString();
}

assert inputStream != null;
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line).append("\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "["+result+"]");
System.out.println(e);
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}

@Override
protected void onPostExecute(String result){

myJSON = result;
prepareListData();

}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}



public String getPostDataString(JSONObject params) throws Exception {

StringBuilder result = new StringBuilder();
boolean first = true;

Iterator<String> itr = params.keys();

while(itr.hasNext()){

String key= itr.next();
Object value = params.get(key);

if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));

}
return result.toString();
}


private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
List<String> top250 = new ArrayList<String>();

try {
peoples = new JSONArray(myJSON);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);

company_name = c.getString("firm_name");
from_date = c.getString("joining_date");
to_date = c.getString("to_date");
location=c.getString("duty_location");

listDataHeader.add(company_name);
// System.out.println(company_name+" - "+from_date+" - "+to_date+" - "+location);
top250.add("From Date: "+from_date);
top250.add("To Date: "+to_date);
top250.add("Location: "+location);
listDataChild.put(listDataHeader.get(i), top250);

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




// Adding child data
//listDataHeader.add("Top 250");
//listDataHeader.add("Now Showing");
// listDataHeader.add("Coming Soon..");

// Adding child data
/* 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");

List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");

List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");*/




listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);


}
}

和 ExpandableList 适配器

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 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;
}
}

图片enter image description here

最佳答案

检查您的代码后,更改您的 for 循环,例如: 对于(int i = 0;我

            company_name = c.getString("firm_name");
from_date = c.getString("joining_date");
to_date = c.getString("to_date");
location=c.getString("duty_location");

listDataHeader.add(company_name);
// System.out.println(company_name+" - "+from_date+" - "+to_date+" - "+location);
top250.add("From Date: "+from_date);
top250.add("To Date: "+to_date);
top250.add("Location: "+location);
listDataChild.put(listDataHeader.get(i), top250);
top250 = new ArrayList<String>();

}

关于android - 如何制作动态可扩展 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42625249/

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