Android的ListView中的JSONArray
urlHi我有一段代码,可以从php页面接收数据,我想把数据放在listview中。我收到了这些数据,我在system.out.println中看到了这些数据,但是我在listview中有白色的行。代码:Android的ListView中的JSONArray,android,json,list,listview,Android,Json,List,Listview,urlHi我有一段代码,可以从php页面接收数据,我想把数据放在listview中。我收到了这些数据,我在system.out.println中看到了这些数据,但是我在listview中有白色的行。代码: public class Utenti extends ListActivity { ListView lv; ListView listView; static String temp; //ListAdapter adapter; @Override public
public class Utenti extends ListActivity {
ListView lv;
ListView listView;
static String temp;
//ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Thread t = new Thread(new Runnable (){
public void run() {
//Looper.prepare();
JSONObject json =conn("ketut.php");
try {
//Get the element that holds the earthquakes ( JSONArray )
JSONArray ute = json.getJSONArray("success");
final List<HashMap<String, String>> mylist =
new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0;i < ute.length();i++){
JSONObject e = ute.getJSONObject(i);
System.out.println(e.getString("user"));
map.put("name", e.getString("user"));
mylist.add(map);
}
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(Utenti.this, mylist , R.layout.utentii,new String[] { "name" },new int[] { 1});
Utenti.this.setListAdapter(adapter);
}
});
} catch (JSONException e) {
System.out.println("4");
e.printStackTrace();
}
}
});
t.start();
// t.start();
}
public static JSONObject conn(String pagina){
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
JSONObject jArray = null;
try
{
url = new URL("url"+pagina);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
response = sb.toString();
System.out.println(response);
try{jArray=new JSONObject (response);}
catch(JSONException e){}
isr.close();
reader.close();
}
catch(IOException e)
{
Log.i("NetworkTest","Network Error: " + e);
}
return jArray;
}}
公共类Utenti扩展了ListActivity{
ListView lv;
列表视图列表视图;
静态字符串温度;
//列表适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
lv=getListView();
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
}
});
线程t=新线程(新可运行(){
公开募捐{
//Looper.prepare();
JSONObject json=conn(“ketut.php”);
试一试{
//获取保存地震的元素(JSONArray)
JSONArray ute=json.getJSONArray(“成功”);
最终列表mylist=
新的ArrayList();
HashMap=newHashMap();
对于(int i=0;i
utii.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
否。runOnUiThread()是必需的,因为您要调用setListAdapter
再来一次
移动这两条线
ListAdapter adapter = new SimpleAdapter(Utenti.this, mylist , R.layout.utentii,new String[] { "name" },new int[] { 1});
setListAdapter(adapter);
就在onClick()
方法中的for(){}
循环之后
如果您确定您的mylist
列表中填充了json调用中的数据
基本上是这样的示例:
将xml文件内容替换为以下内容:
test.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
对于ListView的行
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" android:layout_gravity="center"/>
</LinearLayout>
然后,onCreate()命令:
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
ListView lv=getListView();
populateListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
populateListView();
}
});
}
私有void populateListView(){
线程t=新线程(新可运行(){
公开募捐{
JSONObject json=getUserObject(“http://myhost:8080/login/email");
Log.i(“json”,json.toString());
试一试{
//获取保存地震的元素(JSONArray)
JSONArray-friends=json.getJSONObject(“用户”).getJSONArray(“朋友”);
最后名单=
新的ArrayList();
//获取用户对象中的好友ID列表
for(int i=0;i @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ListView lv = getListView();
populateListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
populateListView();
}
});
}
private void populateListView(){
Thread t = new Thread(new Runnable (){
public void run() {
JSONObject json = getUserObject("http://myhost:8080/login/email");
Log.i("json",json.toString());
try {
//Get the element that holds the earthquakes ( JSONArray )
JSONArray friends = json.getJSONObject("user").getJSONArray("friends");
final List<Map<String, String>> mylist =
new ArrayList<Map<String, String>>();
//get list of friend ids in the user object
for(int i=0;i < friends.length();i++){
Map<String,String> map = new LinkedHashMap<String, String>();
map.put("friendid", friends.getString(i));
mylist.add(map);
}
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(MyTestListActivity.this, mylist , R.layout.row,new String[] { "friendid" },new int[] {R.id.textView});
setListAdapter(adapter);
}
});
} catch (JSONException e) {
System.out.println("4");
e.printStackTrace();
}
}
});
t.start();
}
public static JSONObject getUserObject(String page){
JSONObject jArray = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(page);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "test"));
nameValuePairs.add(new BasicNameValuePair("pwd", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
try{
jArray=new JSONObject (str);}
catch(JSONException e){Log.d("error",e);}
}
catch(Exception e)
{
Log.i("NetworkTest", "Network Error: " + e);
}
return jArray;
}