我的应用程序从服务器获取数据并将其显示在列表视图中。我正在对数据进行排序并希望显示它们,所以我想使用单选按钮来更改视图。对于每个按钮,都应将另一个内容加载到适配器显示的列表中。问题是没有视图出现,不幸的是我的屏幕保持空白。如何正确加载它们并更改 onClick 输入?(在这种情况下,我想用单选按钮显示 contactList 和 contactListTwo)
到目前为止,这是我的代码。我所说的部分在底部:
public class CallsFragment extends Fragment{
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
private static String url = "xx";
int a = 1;
String title;
private BaseAdapter mContactsAdapter;
private RadioGroup rg;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
private final List<HashMap<String, String>> mContactsListItems = new ArrayList<>();
ArrayList<HashMap<String, String>> contactList;
ArrayList<HashMap<String, String>> contactListTwo;
private static final int rb1id = 2;
private static final int rb2id = 1;
private static final int rb3id = 0;
public CallsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_calls, container, false);
contactList = new ArrayList<>();
contactListTwo = new ArrayList<>();
rg = (RadioGroup) view.findViewById(R.id.RGroup);
rb1 = (RadioButton) view.findViewById(R.id.radioButton5);
rb2 = (RadioButton) view.findViewById(R.id.radioButton4);
rb3 = (RadioButton) view.findViewById(R.id.radioButton6);
rb1.setId(rb1id);
rb2.setId(rb2id);
rb3.setId(rb3id);
((ListView) view.findViewById(R.id.list))
.setAdapter(mContactsAdapter);
return view;
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute () {
super.onPreExecute();
}
@Override
protected Void doInBackground (Void...arg0){
HttpHandler sh = new HttpHandler();
//request to url and getting response
String jsonStr = sh.makeServiceCall("https://creativecommons.tankerkoenig.de/json/list.php?lat=" + lat + "&lng=" + lon + "&rad=" + umkreis + "&sort=price&type="+ type +"&apikey=21f2ed18-88b1-5d26-34f3-28318713eaf4");
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("stations");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String price = c.getString("price");
//tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id",id);
contact.put("price", price);
//adding contact to contact list
contactList.add(contact);
}
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
return null;
}
@Override
protected void onPostExecute (Void result){
super.onPostExecute(result);
/**
* Updating parsed JSON data into ListView
* */
mContactsAdapter = new SimpleAdapter(
getActivity(), mContactListItems,
R.layout.list_item, new String[]{"price", "brand",
"dist", "street", "houseNumber", "postcode", "place"}, new int[]{R.id.stoff,
R.id.brand, R.id.dist, R.id.street, R.id.houseNumber, R.id.postCode, R.id.place});
mContactListItems.addAll(contactListTwo);
mContactsAdapter.notifyDataSetChanged();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mContactsListItems.clear();
switch (checkedId) {
case R.id.radioButton4:
mContactsListItems.addAll(contactList);
break;
case R.id.radioButton5:
mContactsListItems.addAll(contactListTwo);
break;
}
//mark as changed, update list
mContactsAdapter.notifyDataSetChanged();
}
});
}
}
您将一个列表对象传递给您的适配器 ( contactListTwo
),但在您检查的更改侦听器 ( ) 中更新不同的列表对象mContactsListItems
。这意味着您的更改对您的适配器完全不可见。
要修复,请更改传递给适配器构造函数的列表引用:
new SimpleAdapter(..., mContactsListItems, ...);
我注意到您从未将初始项目设置到此列表中,因此您可能还必须mContactsListItems.addAll(contactListTwo)
在第一次创建适配器之前调用 eg 。这仅取决于在用户更改单选按钮之前您是否希望列表为空。
还有第二个问题:您将适配器分配给ListView
. 在您的onCreate()
,我看到这一行:
((ListView) view.findViewById(R.id.list)) .setAdapter(mContactsAdapter);
在此代码运行时,我没有看到 的任何初始化mContactsAdapter
,因此除非您省略了该代码,否则这将与编写setAdapter(null)
.
稍后,在 中onPostExecute()
,您创建一个new
适配器并将其分配给mContactsAdapter
。但是,这实际上并不影响列表视图!如果重新分配其变量,则需要重新设置适配器。
因此,您要么必须先初始化适配器,然后再将其设置在列表视图中onCreate()
(即使您尚未加载数据,也可以这样做;适配器将为空),或者您必须setAdapter()
再次调用onPostExecute()
将这个新适配器分配给您的列表视图的方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句