问:public class ExistingCSheet extends AppCompatActivity {
private TextView listing;
String[] items;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_csheet);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//new JSONTask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoList.txt");
new JSONTask().execute("http://192.168.1.102/PSF/api/product\n");
// listing = (TextView) findViewById(R.id.ecsList);
lv = (ListView) findViewById(R.id.esList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ExistingCSheet.this, NewCSheet.class);
intent.putExtra("in",lv.getItemAtPosition(i).toString());
startActivity(intent);
}
});
}
public class JSONTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
// JSONObject parentObject = new JSONObject(finalJson);
// JSONArray parentArray = parentObject.getJSONArray("");
JSONArray parentArray = new JSONArray(finalJson);
StringBuffer finalBufferedData = new StringBuffer();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
String t1 = finalObject.getString("ib_itemcode1");
String t2 = finalObject.getString("transtatuscode");
String t3 = finalObject.getString("invtid");
String t4 = finalObject.getString("descr");
// int year = finalObject.getInt("transtatuscode");
finalBufferedData.append(t1 + " - " + t2 + " - " + t3 + " - " + t4 + "\n");
}
//JSONObject finalObject = parentArray.getJSONObject(0);
return finalBufferedData.toString();
//return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
listing.setText(result);
//not done yet
}
}
}
大家好,我现在想做的是在android中显示json,但是我只能将它们全部转储到textview中。
我需要将它们放置在视图列表中,以使每一行都包含示例“ ib_itemcode1”:2131“ transtatuscode”:123“ invtid”:1231“ descr”:123123。
我仍然在arrayAdapter上阅读,但是我认为更大的问题是如何将json数据分配到数组适配器中。
答:首先:创建一个像这样的模型类:
public class MyJSONBean {
/** Property ib_itemcode1 */
String ib_itemcode1;
/** Property transtatuscode */
String transtatuscode;
/** Property invtid */
String invtid;
/** Property descr */
String descr;
/**
* Gets the ib_itemcode1
*/
public String getIb_itemcode1() {
return this.ib_itemcode1;
}
/**
* Sets the ib_itemcode1
*/
public void setIb_itemcode1(String value) {
this.ib_itemcode1 = value;
}
/**
* Gets the transtatuscode
*/
public String getTranstatuscode() {
return this.transtatuscode;
}
/**
* Sets the transtatuscode
*/
public void setTranstatuscode(String value) {
this.transtatuscode = value;
}
/**
* Gets the invtid
*/
public String getInvtid() {
return this.invtid;
}
/**
* Sets the invtid
*/
public void setInvtid(String value) {
this.invtid = value;
}
/**
* Gets the descr
*/
public String getDescr() {
return this.descr;
}
/**
* Sets the descr
*/
public void setDescr(String value) {
this.descr = value;
}
}
第二:声明清单:
List<MyJSONBean> myList = new ArrayList<>();
第三:在doInBackground方法内部,将JSON中的值放入列表内
修改for循环,如下所示:
for (int i = 0; i < parentArray.length(); i++) {
MyJSONBean myBean = new MyJSONBean();
JSONObject finalObject = parentArray.getJSONObject(i);
myBean.setIb_itemcode1(finalObject.getString("ib_itemcode1"));
myBean.setTranstatuscode(finalObject.getString("transtatuscode"));
myBean.setInvtid(finalObject.getString("invtid"));
myBean.setDescr(finalObject.getString("descr"));
myList.add(myBean);
}
第四:在onPostExecute方法中,使用myList调用适配器类,然后在ListView或RecyclerView中设置适配器。