mysql获取的数据不会显示在android中的listview上

时间:2016-03-01 09:41:38

标签: android mysql json listview

我想从mysql中获取名称并在android中的listview中显示。我已经使用了JSON解析。数据已成功获取但未在列表视图中显示。 listview的布局没有显示任何内容。 这是列表视图的xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.viren.cable.custlist">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview">

</ListView>

listview项目的xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listname"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>

现在是listactivity的java代码

public class custlist extends ActionBarActivity {
String json="";
private static final String TAG_RESULTS="result";
private static final String TAG_NAME="name";

JSONArray people=null;
ArrayList<HashMap<String,String>> personlist;
ListView list;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custlist);
list=(ListView) findViewById(R.id.listview);
     personlist=new ArrayList<HashMap<String, String>>();

     new getDataJSON().execute();

 }
protected void showList() {
    try {
        JSONObject jsonObj = new JSONObject(json);
        people = jsonObj.getJSONArray(TAG_RESULTS);

        for (int i = 0; i < people.length(); i++) {
            JSONObject c = people.getJSONObject(i);

            String name = c.getString(TAG_NAME);

         HashMap<String, String> persons = new HashMap<String, String>();


            persons.put(TAG_NAME, name);



            personlist.add(persons);
        }

        ListAdapter adapter = new SimpleAdapter(
                custlist.this, personlist, R.layout.activity_custlist,
                new String[]{TAG_NAME},
                new int[]{R.id.listname}
        );


        list.setAdapter(adapter);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data "+e.toString());
        e.printStackTrace();
    }

    }
    class getDataJSON extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new  
    BasicHttpParams());
            HttpPost httppost = new   
    HttpPost("http://www.vgeek.in/custname.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = "";
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                Log.e("log_tag", "connection success ");
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new    
               InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = "";
                while ((line = reader.readLine()) != null)
                {
                   // sb.append(line + "\n");
                sb.append(line);
                }
                inputStream.close();
                result = sb.toString();

            } catch (Exception e) {
                // Oops
                Log.e("log_tag", "Error converting result "+e.toString());
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result){
            json=result;
            showList();
        }
        }
        }

这是我的logcat,在这里我已经检查过我的数据是否已被提取但是它已被提取。

  03-01 17:44:33.186    2829-2829/com.example.viren.cable D/gralloc_goldfish﹕     
  Emulator without GPU emulation detected.
  03-01 17:44:36.174    2829-2829/com.example.viren.cable I/Choreographer﹕  
  Skipped 85 frames!  The application may be doing too much work on its main  
  thread.
  03-01 17:44:40.825    2829-2846/com.example.viren.cable E/log_tag﹕ 
  connection success
  03-01 17:44:41.188    2829-2829/com.example.viren.cable I/Choreographer﹕ 
  Skipped 97 frames!  The application may be doing too much work on its main 
  thread.
  03-01 17:44:41.191    2829-2829/com.example.viren.cable I/log_tag﹕ name vik
  03-01 17:44:41.191    2829-2829/com.example.viren.cable I/log_tag﹕ name nik
  03-01 17:44:41.191    2829-2829/com.example.viren.cable I/log_tag﹕ name    
   ravi

3 个答案:

答案 0 :(得分:1)

添加数据后应该告诉它,有更改并且应该刷新。您可以致电adapter.notifyDataSetChanged();

如果这没有帮助,这里有一个很好的answer可以帮助你。

答案 1 :(得分:0)

替换

 ListAdapter adapter = new SimpleAdapter( custlist.this, personlist,   
 R.layout.activity_custlist, new String[]{TAG_NAME}, new int[]  
 {R.id.listname}    
 ); 

 ListAdapter adapter = new SimpleAdapter( custlist.this, personlist,    
 R.layout.item_list, new String[]{TAG_NAME}, new int[]
 {R.id.listname} );

答案 2 :(得分:0)

替换这些行

ListAdapter adapter = new SimpleAdapter(
            custlist.this, personlist, R.layout.activity_custlist,
            new String[]{TAG_NAME},
            new int[]{R.id.listname}
    );
list.setAdapter(adapter);

通过以下

ListAdapter adapter = new SimpleAdapter(
            custlist.this, personlist, R.layout.item_list,
            new String[]{TAG_NAME},
            new int[]{R.id.listname}
    );
list.setAdapter(adapter);

它会正常工作。

<强>描述

  

您应该了解SimpleAdapter构造函数

     

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

     

此处Context:与此SimpleAdapter关联的视图正在运行的上下文

     

List:地图列表。列表中的每个条目对应于列表中的一行。地图包含每行的数据,并应包括“from”

中指定的所有条目      

int:视图布局的资源标识符,用于定义此列表项的视图。布局文件应至少包括“to”

中定义的那些命名视图      

String:将添加到与每个项目关联的地图的列名列表。

     

int:应在“from”参数中显示列的视图。这些都应该是TextViews。此列表中的前N个视图将给出from参数中前N列的值。