0

I just would to customize my ListView with an image on the left

package com.project.test;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.R.id;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ContactUs extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    ListView list;
     LazyAdapter adapter;
    ArrayList<HashMap<String, String>> contactsList;

    // url to get all contacts list
    private static String url_all_contacts = "http://10.0.2.2/test/get_all_staff.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CONTACT = "contacts";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";
    private static final String TAG_IMAGE = "image";

    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    // contacts JSONArray
    JSONArray contacts = null;
    ImageView img = (ImageView)findViewById(R.id.image);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_staff);

        // Hashmap for ListView
        contactsList = new ArrayList<HashMap<String, String>>();

        // Loading contacts in Background Thread
        new LoadAllcontacts().execute();
            //list = getListView();
        // Get listview
        //ListView lv = getListView();

        // on seleting single product
        // launching Edit Product Screen
        /*
            lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        GetAllStaffDetails.class);
                // sending pid to next activity
                in.putExtra(TAG_PID, pid);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });
        */
    }

    // Response from Edit Product Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllcontacts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ContactUs.this);
            pDialog.setMessage("Loading contacts. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All contacts from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_contacts, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("All contacts: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // contacts found
                    // Getting Array of contacts
                    contacts = json.getJSONArray(TAG_CONTACT);

                    // looping through All contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);
                        String image = c.getString(TAG_IMAGE);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        contactsList.add(map);
                    }
                    //list = (ListView)findViewById(id.list);
                    //list.setAdapter(contactsList);


                } else {
                    // no contacts found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            NewProductActivity.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all contacts
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                       //adapter=new LazyAdapter(this, contactsList);

                    ListAdapter adapter = new SimpleAdapter(ContactUs.this,
                            contactsList, R.layout.list_row, new String[] {
                                    TAG_PID, TAG_NAME }, new int[] { R.id.pid,
                                    R.id.name });

                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }
}

This is the code am using right now, but I had some difficulties in how to retrieve an image from data base using JSON then insert the image to the ListView. So any help please ??

Khaled
  • 1
  • 2

1 Answers1

0

Provide more details if you wonna get answer.

From which field you wonna get image? This is an url, filesystem ref or may be base64 data, or...

In your layout (R.layout.list_row) you should have an ImageView into which you can set any kind of Drawable object to be displayed.

And remove runOnUiThread call, because in onPostExecute you already on UI thread.

acc15
  • 1,205
  • 1
  • 11
  • 22
  • I wanna get the image from website database (working on local server right now ). I already set the the list_row.xml file and it's ready and thank you for your notice. – Khaled Apr 24 '13 at 17:55
  • Let me rephrase my question: What `String image = c.getString(TAG_IMAGE);` this variable contains? – acc15 Apr 24 '13 at 18:01
  • its supposed to be the holder for url of the image. I was trying to retrieve the url from database then display the image through it. – Khaled Apr 24 '13 at 18:06
  • here is a ready answer: http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – acc15 Apr 24 '13 at 18:09