在Android中的ListView中的图像和按钮

时间:2014-07-31 10:34:10

标签: android mysql listview

我有一个listView可以使用AsyncTask从MySql获取数据,但我的问题是当我在ListView中添加一些按钮和图像时。我添加了一些按钮,但我可以使用它,图像不会显示。

这是我的代码......

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    new Loadstudent().execute();

    ListView lv = getListView();



    lv.setOnItemClickListener(new OnItemClickListener() {

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

            Intent i = new Intent(getApplicationContext(), Maindetail.class);
            String sidlist = ((TextView) view.findViewById(R.id.tid)).getText().toString();
            String namelist = ((TextView) view.findViewById(R.id.sname)).getText().toString();
            i.putExtra(TAG_ID, sidlist);
            i.putExtra(TAG_NAME, namelist);
            startActivity(i);
        }
    });

}

class Loadstudent extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading students. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_student, "GET", params);

        Log.d("ALL student: ", json.toString());

        try {

            int success = json.getInt(TAG_SUCCESS);

            if(success == 1) {
                student = json.getJSONArray(TAG_STUDENT);

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

                    sid = c.getString(TAG_ID);
                    name = c.getString(TAG_NAME);
                    photo = c.getString(TAG_PHOTO);

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

                    map.put(TAG_ID, sid);
                    map.put(TAG_NAME, name);
                    map.put(TAG_PHOTO, photo);

                    studentList.add(map);
                }
            }else {

            }

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

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();

        runOnUiThread(new  Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(
                        MainActivity.this, studentList,
                        R.layout.list_main, new String[] {
                                TAG_PHOTO,TAG_ID, TAG_NAME},
                        new int [] { R.id.imageviewlist, R.id.tid, R.id.sname });
                setListAdapter(adapter);

            }
        });
    }

}

我从网上学习了lazyadapter,但是在将它与代码相结合时遇到了问题。 有任何可以解决我的问题的教程或链接将是一个很大的帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

嘿,尝试下面的方法 -

       lv.setOnItemClickListener(new OnItemClickListener() {

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

            Intent i = new Intent(getApplicationContext(), Maindetail.class);
            HashMap<String, String> data = studentList.get(position);
            String sidlist = data.get(TAG_ID);
            String namelist = data.get(TAG_NAME);
            i.putExtra(TAG_ID, sidlist);
            i.putExtra(TAG_NAME, namelist);
            startActivity(i);
        }
    });

答案 1 :(得分:0)

如果要在每个行布局中添加额外的组件,则必须为列表视图创建自己的适配器。

这并不容易,但不久前我在这里编码 - &gt; ListView Custom Adapter getView

我希望这可以帮到你!