JSON解析基于选择填充它的值写入的ListView [英] Parse JSON and populate its value into ListView Based on Selection
问题描述
下面是我的JSON链接.... JSON链接
您可以看到有有数据的数量相同的节点。在此JSON, standard_title 是一样的,并不止一个,但有不同的数据..
我的要求是解析标准名称和只显示一个来自多个重复的标准称号。当我选择基于他们标准的头衔,我必须得到每一个节点的所有不同的数据。对于任何类型的帮助日Thnx千亿..
我的code我做了什么是这里
私有类JSONParse扩展的AsyncTask<字符串,字符串,JSONObject的> {
私人ProgressDialog pDialog;
@覆盖
在preExecute保护无效(){
super.on preExecute();
版本=(的TextView)findViewById(R.id.vers);
名=(的TextView)findViewById(R.id.name);
API =(的TextView)findViewById(R.id.api);
pDialog =新ProgressDialog(DialogWithListviewActivity.this);
pDialog.setMessage(获得的数据...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();}@覆盖
受保护的JSONObject doInBackground(字符串参数... args){ JSONParser jParser =新JSONParser(); // URL从JSON入门
JSONObject的JSON = jParser.getJSONFromUrl(URL);
返回JSON;
}
@覆盖
保护无效onPostExecute(JSON的JSONObject){
pDialog.dismiss();
尝试{
// URL从获取JSON数组
安卓= json.getJSONArray(TAG_OS);
@燮pressWarnings(rawtypes)
清单tempTitles =新的ArrayList<列表>();
清单tempSubTitles =新的ArrayList<列表>(); 的for(int i = 0; I< android.length();我++){
JSONObject的C = android.getJSONObject(I)
C = c.getJSONObject(LearningStandards);
如果(tempTitles.contains(c.getString(STANDARD_TITLE)))
{ 继续;
}
tempTitles.add(c.getString(STANDARD_TITLE));
} 的for(int i = 0; I< android.length();我++){
JSONObject的C = android.getJSONObject(I)
C = c.getJSONObject(LearningStandards);
如果(tempSubTitles.contains(c.getString(介绍)))
{
tempSubTitles.add(c.getString(介绍)); }
}
的for(int i = 0; I< tempTitles.size();我++){
HashMap的<字符串,字符串> tmpMap =新的HashMap<字符串,字符串>();
tmpMap.put(STANDARD_TITLE,tempTitles.get(ⅰ)的ToString());
oslist.add(tmpMap);
适配器=新SimpleAdapter(DialogWithListviewActivity.this,oslist,R.layout.list_v,
新的String [] {STANDARD_TITLE,REF_ID,DESCRIPTION},新的INT [] {R.id.vers,R.id.name,R.id.api}); }
我宁愿建议使用扩展
的ListView
尽管你想要的是一个ListView,你想实现一个code其中在选择的改变应该显示的数据组是根据该选择什么样的事实和扩展
列表视图
是你的答案。
虽然我说你应该做的,我依然会回答你的问题,这里就是你正在寻找的实施。在code,也可在<一href=\"https://github.com/pankajnimgade/Tutorial/blob/master/app/src/main/java/listview/test/activities/ListViewTestOneActivity.java\"相对=nofollow> github上
公共类ListViewTestOneActivity延伸活动{ 私人的ListView ListView的;
私人的ArrayList&LT; LearningStandards&GT; learningStandardses;
私人ProgressDialog progressDialog;
@覆盖 保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_list_view_test_one);
initializeUI();
} 私人无效initializeUI(){
progressDialog =新ProgressDialog(本);
progressDialog.setMessage(发布...);
progressDialog.setIndeterminate(真);
progressDialog.setCanceledOnTouchOutside(假); ListView控件=(ListView控件)findViewById(R.id.ListViewTestOneActivity_listView);
learningStandardses =新的ArrayList&LT;&GT;();
新DownloadJSON()执行();
} 私有类DownloadJSON扩展的AsyncTask&LT;太虚,太虚,太虚&GT; { @覆盖
在preExecute保护无效(){
super.on preExecute();
progressDialog.show();
} @覆盖
保护无效doInBackground(虚空...... PARAMS){ 尝试{ 网址URL =新的URL(http://54.152.108.131/iphone111/getLearningStandards);
HttpURLConnection的HttpURLConnection类=(HttpURLConnection类)url.openConnection();
httpURLConnection.connect(); 字符串结果= IOUtils.toString(httpURLConnection.getInputStream());
的System.out.println(+结果); 的JSONObject的JSONObject =新的JSONObject(结果);
JSONArray jsonArray = jsonObject.getJSONArray(LearningStandards); 的for(int i = 0; I&LT; jsonArray.length();我++){
JSONObject的singleObject = jsonArray.getJSONObject(I)
JSONObject的learningStandardsJsonObject = singleObject.getJSONObject(LearningStandards);
LearningStandards learningStandards =新LearningStandards();
learningStandards.setStandard_title(+ learningStandardsJsonObject.getString(standard_title));
learningStandards.setRef_id(+ learningStandardsJsonObject.getString(REF_ID));
learningStandards.setDescription(+ learningStandardsJsonObject.getString(说明));
learningStandardses.add(learningStandards);
}
}赶上(例外五){
e.printStackTrace();
}
返回null;
} @覆盖
保护无效onPostExecute(虚空避免){
super.onPostExecute(避免);
progressDialog.dismiss();
的System.out.println(learningStandardses:尺寸:+ learningStandardses.size());
ArrayAdapter&LT; LearningStandards&GT;适配器=
新ArrayAdapter&所述; LearningStandards&GT;(getApplicationContext(),R.layout.simple_list_item_1,learningStandardses);
listView.setAdapter(适配器);
}
} 私有类LearningStandards {
私人字符串standard_title;
私人字符串REF_ID;
私人字符串描述; 公共字符串getStandard_title(){
返回standard_title;
} 公共无效setStandard_title(字符串standard_title){
this.standard_title = standard_title;
} 公共字符串getRef_id(){
返回REF_ID;
} 公共无效setRef_id(字符串REF_ID){
this.ref_id = REF_ID;
} 公共字符串getDescription(){
返回描述;
} 公共无效setDescription(字符串描述){
this.description =描述;
} @覆盖
公共字符串的toString(){
返回standard_title;
}
}
}
activity_list_view_test_one.xml
块引用>&LT;的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直&GT;
&LT; ListView控件
机器人:ID =@ + ID / ListViewTestOneActivity_listView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT/&GT;
&LT; / LinearLayout中&GT;
输出
块引用>我知道这并不是要你想要的,但我所要告诉你的是,你需要有不同的标题列表的组所以这应该是一个
扩展列表视图
。
更新
块引用>私有类DownloadAnd prepareGroupJSON延伸的AsyncTask&LT;太虚,太虚,太虚&GT; { @覆盖
在preExecute保护无效(){
super.on preExecute();
progressDialog.show();
} @覆盖
保护无效doInBackground(虚空...... PARAMS){ 尝试{
myGroups =新的ArrayList&LT;&GT;();
网址URL =新的URL(http://54.152.108.131/iphone111/getLearningStandards);
HttpURLConnection的HttpURLConnection类=(HttpURLConnection类)url.openConnection();
httpURLConnection.connect(); 字符串结果= IOUtils.toString(httpURLConnection.getInputStream());
的System.out.println(+结果); 的JSONObject的JSONObject =新的JSONObject(结果);
JSONArray jsonArray = jsonObject.getJSONArray(LearningStandards);
HashMap的&LT;弦乐,MyGroup的&GT; stringMyGroupHashMap =新的HashMap&LT;&GT;(); 的for(int i = 0; I&LT; jsonArray.length();我++){
JSONObject的singleObject = jsonArray.getJSONObject(I)
JSONObject的learningStandardsJsonObject = singleObject.getJSONObject(LearningStandards);
LearningStandards learningStandards =新LearningStandards();
如果(!stringMyGroupHashMap.containsKey(learningStandardsJsonObject.getString(standard_title))){
MyGroup的为myGroup =新MyGroup的();
LearningStandards single_learningStandards =新LearningStandards();
single_learningStandards.setStandard_title(learningStandardsJsonObject.getString(standard_title));
single_learningStandards.setRef_id(learningStandardsJsonObject.getString(REF_ID));
single_learningStandards.setStandard_title(learningStandardsJsonObject.getString(说明));
。myGroup.getLearningStandards()加(single_learningStandards);
myGroup.setStandard_title(learningStandardsJsonObject.getString(standard_title));
stringMyGroupHashMap.put(learningStandardsJsonObject.getString(standard_title),为myGroup);
}其他{
MyGroup的为myGroup = stringMyGroupHashMap.get(learningStandardsJsonObject.getString(standard_title));
LearningStandards single_learningStandards =新LearningStandards();
single_learningStandards.setStandard_title(learningStandardsJsonObject.getString(standard_title));
single_learningStandards.setRef_id(learningStandardsJsonObject.getString(REF_ID));
single_learningStandards.setStandard_title(learningStandardsJsonObject.getString(说明));
。myGroup.getLearningStandards()加(single_learningStandards);
} } 为(MyGroup的为myGroup:stringMyGroupHashMap.values()){
myGroups.add(为myGroup);
}
的System.out.println(HashMap的大小:+ stringMyGroupHashMap.size());
}赶上(例外五){
e.printStackTrace();
}
返回null;
} @覆盖
保护无效onPostExecute(虚空避免){
super.onPostExecute(避免);
progressDialog.dismiss();
的System.out.println(learningStandardses:尺寸:+ learningStandardses.size());
ArrayAdapter&LT;&MyGroup的GT;适配器=
新ArrayAdapter&LT;&MyGroup的GT;(getApplicationContext(),R.layout.simple_list_item_1,myGroups);
listView.setAdapter(适配器);
}
}您将需要这个类
MyGroup.class
块引用>私有类MyGroup的{ 私人的ArrayList&LT; LearningStandards&GT; learningStandards;
私人字符串standard_title; 公共MyGroup的(){
this.learningStandards =新的ArrayList&LT;&GT;();
this.standard_title =;
} 公众的ArrayList&LT; LearningStandards&GT; getLearningStandards(){
返回learningStandards;
} 公共无效setLearningStandards(ArrayList的&LT; LearningStandards&GT; learningStandards){
this.learningStandards = learningStandards;
} 公共字符串getStandard_title(){
返回standard_title;
} 公共无效setStandard_title(字符串standard_title){
this.standard_title = standard_title;
} @覆盖
公共字符串的toString(){
返回standard_title;
}
}
输出
块引用>Here is my JSON link.... JSON LINK
And here is
You can see there is same node having number of data.. In this JSON, standard_title is same and more than one but having different data.. My Requirement is to parse standard title and showing only one from multiple duplicate standard title . when i select standard title based on them I have to get all the different data from every node. Billions of thnx for any type help..
my code what i had done is here
private class JSONParse extends AsyncTask<String, String, JSONObject> { private ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); ver = (TextView)findViewById(R.id.vers); name = (TextView)findViewById(R.id.name); api = (TextView)findViewById(R.id.api); pDialog = new ProgressDialog(DialogWithListviewActivity.this); pDialog.setMessage("Getting Data ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected JSONObject doInBackground(String... args) { JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = jParser.getJSONFromUrl(url); return json; } @Override protected void onPostExecute(JSONObject json) { pDialog.dismiss(); try { // Getting JSON Array from URL android = json.getJSONArray(TAG_OS); @SuppressWarnings("rawtypes") List tempTitles = new ArrayList<List>(); List tempSubTitles = new ArrayList<List>(); for(int i = 0; i < android.length(); i++){ JSONObject c = android.getJSONObject(i); c = c.getJSONObject("LearningStandards"); if(tempTitles.contains(c.getString(STANDARD_TITLE))) { continue; } tempTitles.add(c.getString(STANDARD_TITLE)); } for(int i = 0; i < android.length(); i++){ JSONObject c = android.getJSONObject(i); c = c.getJSONObject("LearningStandards"); if(tempSubTitles.contains(c.getString(DESCRIPTION))) { tempSubTitles.add(c.getString(DESCRIPTION)); } } for(int i = 0; i < tempTitles.size(); i++){ HashMap<String, String> tmpMap = new HashMap<String, String>(); tmpMap.put(STANDARD_TITLE, tempTitles.get(i).toString()); oslist.add(tmpMap); adapter = new SimpleAdapter(DialogWithListviewActivity.this, oslist, R.layout.list_v, new String[] { STANDARD_TITLE,REF_ID, DESCRIPTION }, new int[] { R.id.vers,R.id.name, R.id.api}); }
解决方案I would rather suggest to use a
Expandable
ListView
despite the fact what you want is a listView, you want to implement a code where the change in the selection should show data group which is depending on that selection andExpandable
Listview
is your answer.Although I have stated what you should do, i will still answer your question, here is the implementation what you are looking for. the code is also available on github
public class ListViewTestOneActivity extends Activity { private ListView listView; private ArrayList<LearningStandards> learningStandardses; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_test_one); initializeUI(); } private void initializeUI() { progressDialog = new ProgressDialog(this); progressDialog.setMessage("Posting..."); progressDialog.setIndeterminate(true); progressDialog.setCanceledOnTouchOutside(false); listView = (ListView) findViewById(R.id.ListViewTestOneActivity_listView); learningStandardses = new ArrayList<>(); new DownloadJSON().execute(); } private class DownloadJSON extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Void doInBackground(Void... params) { try { URL url = new URL("http://54.152.108.131/iphone111/getLearningStandards"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); String result = IOUtils.toString(httpURLConnection.getInputStream()); System.out.println("" + result); JSONObject jsonObject = new JSONObject(result); JSONArray jsonArray = jsonObject.getJSONArray("LearningStandards"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject singleObject = jsonArray.getJSONObject(i); JSONObject learningStandardsJsonObject = singleObject.getJSONObject("LearningStandards"); LearningStandards learningStandards = new LearningStandards(); learningStandards.setStandard_title("" + learningStandardsJsonObject.getString("standard_title")); learningStandards.setRef_id("" + learningStandardsJsonObject.getString("ref_id")); learningStandards.setDescription("" + learningStandardsJsonObject.getString("description")); learningStandardses.add(learningStandards); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); progressDialog.dismiss(); System.out.println("learningStandardses: size: " + learningStandardses.size()); ArrayAdapter<LearningStandards> adapter = new ArrayAdapter<LearningStandards>(getApplicationContext(), R.layout.simple_list_item_1, learningStandardses); listView.setAdapter(adapter); } } private class LearningStandards { private String standard_title; private String ref_id; private String description; public String getStandard_title() { return standard_title; } public void setStandard_title(String standard_title) { this.standard_title = standard_title; } public String getRef_id() { return ref_id; } public void setRef_id(String ref_id) { this.ref_id = ref_id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return standard_title; } } }
activity_list_view_test_one.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/ListViewTestOneActivity_listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Output
I know this is not want you want, but what i am trying to tell you is you require to have group of List depending on the title so this should be a
expandable Listview
.Update
private class DownloadAndPrepareGroupJSON extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Void doInBackground(Void... params) { try { myGroups = new ArrayList<>(); URL url = new URL("http://54.152.108.131/iphone111/getLearningStandards"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); String result = IOUtils.toString(httpURLConnection.getInputStream()); System.out.println("" + result); JSONObject jsonObject = new JSONObject(result); JSONArray jsonArray = jsonObject.getJSONArray("LearningStandards"); HashMap<String, MyGroup> stringMyGroupHashMap = new HashMap<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject singleObject = jsonArray.getJSONObject(i); JSONObject learningStandardsJsonObject = singleObject.getJSONObject("LearningStandards"); LearningStandards learningStandards = new LearningStandards(); if (!stringMyGroupHashMap.containsKey(learningStandardsJsonObject.getString("standard_title"))) { MyGroup myGroup = new MyGroup(); LearningStandards single_learningStandards = new LearningStandards(); single_learningStandards.setStandard_title(learningStandardsJsonObject.getString("standard_title")); single_learningStandards.setRef_id(learningStandardsJsonObject.getString("ref_id")); single_learningStandards.setStandard_title(learningStandardsJsonObject.getString("description")); myGroup.getLearningStandards().add(single_learningStandards); myGroup.setStandard_title(learningStandardsJsonObject.getString("standard_title")); stringMyGroupHashMap.put(learningStandardsJsonObject.getString("standard_title"), myGroup); } else { MyGroup myGroup = stringMyGroupHashMap.get(learningStandardsJsonObject.getString("standard_title")); LearningStandards single_learningStandards = new LearningStandards(); single_learningStandards.setStandard_title(learningStandardsJsonObject.getString("standard_title")); single_learningStandards.setRef_id(learningStandardsJsonObject.getString("ref_id")); single_learningStandards.setStandard_title(learningStandardsJsonObject.getString("description")); myGroup.getLearningStandards().add(single_learningStandards); } } for (MyGroup myGroup : stringMyGroupHashMap.values()) { myGroups.add(myGroup); } System.out.println("Size of hashMap: " + stringMyGroupHashMap.size()); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); progressDialog.dismiss(); System.out.println("learningStandardses: size: " + learningStandardses.size()); ArrayAdapter<MyGroup> adapter = new ArrayAdapter<MyGroup>(getApplicationContext(), R.layout.simple_list_item_1, myGroups); listView.setAdapter(adapter); } }
you would require this class
MyGroup.class
private class MyGroup { private ArrayList<LearningStandards> learningStandards; private String standard_title; public MyGroup() { this.learningStandards = new ArrayList<>(); this.standard_title = ""; } public ArrayList<LearningStandards> getLearningStandards() { return learningStandards; } public void setLearningStandards(ArrayList<LearningStandards> learningStandards) { this.learningStandards = learningStandards; } public String getStandard_title() { return standard_title; } public void setStandard_title(String standard_title) { this.standard_title = standard_title; } @Override public String toString() { return standard_title; } }
Output
这篇关于JSON解析基于选择填充它的值写入的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!