gpt4 book ai didi

android - 在 Android ListView 中显示来自 Json String 的数据

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

我有一个如下所示的 JSON 字符串:

{"orders":[{"orderid":"561","order_date":"2016-05-04 09:23:18"},{"orderid":"560","order_date":"2016-05-04 08:56:33"}],"success":1}

我想在 Fragment Activity 的 ListView 中显示它。这是此 fragment 的代码:

public class PastOrders extends Fragment {

private String jsonResult;
private String url = "https://www.example.com/orders.php";
private ListView listView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.past_orders, container, false);

listView = (ListView) v.findViewById(R.id.listView1);

accessWebService();

return v;
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("user_id",params[0]);
data.put("status",params[1]);

RegisterUserClass ruc = new RegisterUserClass();

String result = ruc.sendPostRequest(url,data);

jsonResult = result;

return null;
}

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}

catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}

@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task

public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("orders");

for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("orderid");
String number = jsonChildNode.optString("order_date");
String outPut = name + "-" + number;
employeeList.add(createEmployee("orders", outPut));
}
} catch (JSONException e) {
Toast.makeText(getActivity().getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "orders" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}

我在以下几行中遇到错误:

SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "orders" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);

它说 SimpleAdapter 不能应用于我正在使用的这个类。

这是我遇到的错误:

enter image description here

最佳答案

尝试用 getActivity() 代替这个

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
android.R.layout.simple_list_item_1,
new String[] { "orders" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);

理解为什么这行不通也很重要。第一个参数需要是上下文。如果这是一个 Activity ,这可能会起作用,因为它指的是父对象,并且在 Activity 中它将为您提供上下文。但是在 fragment 内部你不能用它来获取上下文。您可以通过调用 getACtivity() 获取 fragment 内的上下文。在不同的情况下,您必须确保提供正确的上下文!!

关于android - 在 Android ListView 中显示来自 Json String 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37051621/

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