2015-05-12 41 views
1

我可以插入数据到数据库,但现在面临的问题,无法从数据库中检索数据,并显示在Android的ListView。这是我的编码部分检索数据。希望有人能帮助解决这个问题,谢谢。如何在android中的listview中显示mySQL中的数据?

package com.example.iuum; 


private ProgressDialog pDialog; 


private static final String READ_COMMENTS_URL = "http://10.19.229.212/webservice/comments.php"; 


private static final String TAG_SUCCESS = "success"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_POSTS = "posts"; 
private static final String TAG_POST_ID = "post_id"; 
private static final String TAG_USERNAME = "username"; 
private static final String TAG_MESSAGE = "message"; 

private JSONArray mComments = null; 

private ArrayList<HashMap<String, String>> mCommentList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_forum1a); 
} 

@Override 
protected void onResume() { 

    super.onResume(); 

    new LoadComments().execute(); 
} 

public void clickbtnWrite(View v) { 
    Intent i = new Intent(Forum1a.this, AddForum.class); 
    startActivity(i); 
} 

/** 
* Retrieves recent post data from the server. 
*/ 
public void updateJSONdata() { 


    mCommentList = new ArrayList<HashMap<String, String>>(); 


    JSONParser jParser = new JSONParser(); 

    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL); 


    try { 


     mComments = json.getJSONArray(TAG_POSTS); 


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

      // gets the content of each tag 
      String title = c.getString(TAG_TITLE); 
      String content = c.getString(TAG_MESSAGE); 
      String username = c.getString(TAG_USERNAME); 


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

      map.put(TAG_TITLE, title); 
      map.put(TAG_MESSAGE, content); 
      map.put(TAG_USERNAME, username); 


      mCommentList.add(map); 


     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* Inserts the parsed data into the listview. 
*/ 
private void updateList() { 

    ListAdapter adapter = new SimpleAdapter(this, mCommentList, 
      R.layout.singelpost, new String[] { TAG_TITLE, TAG_MESSAGE, 
        TAG_USERNAME }, new int[] { R.id.title, R.id.message, 
        R.id.username }); 


    setListAdapter(adapter); 


    ListView lv = getListView();  
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 



     } 
    }); 
} 



public class LoadComments extends AsyncTask<Void, Void, Boolean> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Forum1a.this); 
     pDialog.setMessage("Loading Comments..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected Boolean doInBackground(Void... arg0) { 
     updateJSONdata(); 
     return null; 

    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     pDialog.dismiss(); 
     updateList(); 
    } 
} 
} 
+1

你得到什么错误?它如何不起作用? –

+0

错误消息,如果有崩溃请发布日志猫跟踪。 – Keshav1234

+0

请显示您的singlepost.xml布局 – Abhishek

回答

0

添加ListView控件在activity_forum1a.xml

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

采取的ListView参考

private ListView mList; 

初始化它的onCreate

mList = (ListView) findViewById(R.id.list_view); 

采取ListAdapter类Refernce

private ListAdapter listAdapter; 

添加setListAdapter方法

public void setListAdapter(ListAdapter listAdapter) { 
    this.listAdapter = listAdapter; 
    mList.setAdapter(listAdapter); 
} 

变化updateList方法

/** 
    * Inserts the parsed data into the listview. 
    */ 
    private void updateList() { 

     ListAdapter adapter = new SimpleAdapter(this, mCommentList, 
       R.layout.test, new String[] { TAG_TITLE, TAG_MESSAGE, 
       TAG_USERNAME }, new int[] { R.id.title, R.id.message, 
       R.id.username }); 

     setListAdapter(adapter); 
} 

,并从法外执行项目点击操作,通过实施onItemClickListener

相关问题