简体   繁体   中英

Image and buttons in ListView in android

I have a listView that can get data from MySql using AsyncTask, but my problem is when i add some buttons and images in the ListView. I added some buttons but i can use it and the image won't display.

here's my code...

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

            }
        });
    }

}

I student the lazyadapter from the net, but got a problem combining it to code. there's any tutorial or link that can solved my problem would be a great help. Thanks in advance.

hey try below approach-

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

You have to create your own adapter for the listview if you want to add extra components in every row layout.

It's not easy, but it's all here coded by me few time ago --> ListView Custom Adapter getView

I hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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