gpt4 book ai didi

java - 如何从 parseJson 获取 ListView 数据

转载 作者:行者123 更新时间:2023-11-30 10:56:38 25 4
gpt4 key购买 nike

你好请回答我的问题我在 eclipse 中有这段代码用于 Android 开发。我正在使用 mysql 和 php 作为数据库并使用 JSON 获取数据。但我不知道如何在 listview 中使用 JSONparse 数据。请编辑我的代码。

public class ViewAllPersons extends Activity {

String url = "http://192.168.1.206/androhp/view_all_persons.php";
ArrayList<String> result;
ListView list;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_all_person);
result = new ArrayList<String>();
LoadAllPersons lap = new LoadAllPersons();
lap.execute(url);
}

class LoadAllPersons extends AsyncTask<String, String, String> {

protected String doInBackground(String... args) {
InputStream jsonStream = getStreamFromURL(args[0], "GET");
String jsonString = streamToString(jsonStream);
parseJSON(jsonString);
return null;
}

void parseJSON(String JSONString) {
try {

JSONObject jo = new JSONObject(JSONString);

JSONArray allpersons = jo.getJSONArray("allpersons");
for (int i = 0; i < allpersons.length(); i++) {
JSONObject object = allpersons.getJSONObject(i);
String objString = "";
objString = object.getString("name") + " , "
+ object.getString("name2") + " : "
+ object.getInt("iconlink");
result.add(objString);
}

} catch (JSONException e) {

}
}

protected void onPostExecute(String file_url) {

list = (ListView) findViewById(R.id.list);
String[] web = {
"Google Plus",
"Twitter",
"Windows"
} ;

String[] imageUrl = {
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png"

};
CustomList adapter = new
CustomList(ViewAllPersons.this, web, imageUrl);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

}

我如何使用 parseJSON 数据而不是网络 ListView :

list = (ListView) findViewById(R.id.list); 
String[] web = {
"Google Plus",
"Twitter",
"Windows"
} ;

String[] imageUrl = {
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png",
"http://www.varzesh3.com/football3_Images/varzesh3-logo.png"

};

最佳答案

You have got both data as well as list, now you need to combine them.
first you have to convert your json result into list, where values are your json values.
final ArrayList<String> listdata = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
listdata .add(values[i]);
}

then you have to assign adapter to your list. You can use following code.
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.yourlistlayout, listdata );
list.setAdapter(adapter);

更多详情 http://www.vogella.com/tutorials/AndroidListView/article.html

关于java - 如何从 parseJson 获取 ListView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32933353/

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