2015-05-12 39 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

相關問題