简体   繁体   English

如何在Android的ListView中显示来自MySQL的数据?

[英]How to display the data from mySQL in listview in android?

I can insert data into database but now face the problem that cannot retrieve data from the database and display in listview in android. 我可以将数据插入数据库,但现在面临无法从数据库检索数据并无法在android的listview中显示的问题。 Here is my coding part for retrieve data. 这是我用于检索数据的编码部分。 Hope someone can help to solve this, thank you. 希望有人可以解决这个问题,谢谢。

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();
    }
}
}

Add ListView in activity_forum1a.xml 在activity_forum1a.xml中添加ListView

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

Take ListView reference 取得ListView参考

    private ListView mList;

Initialize it in onCreate 在onCreate中初始化

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

Take ListAdapter class Refernce 以ListAdapter类为参照

private ListAdapter listAdapter;

Add setListAdapter method 添加setListAdapter方法

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

Change updateList method to 将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);
}

and perform item click operation outside from the method, by implementing onItemClickListener 并通过实现onItemClickListener在方法之外执行项目点击操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM