Question

i already success build up an app using ListView with LazyAdapter based on this link

but there's anomali on my listview. each listview have 1 imageview and 2 textview. so the lazy adapter will display "thumbnail" to imageview and "name" to Textview1 and "address" to Textview2...

the ImageView is coorect...it can display the image thumbnail from database... but the problem here is, the BOTH TEXTVIEW didnt display the correct data! instead displaying the "name" and the "address" from database... they display LINK for THUMBNAIL IMAGE.

so, maybe anyone can help me. thanks b4.

here's my json :

{
"listresto": [
    {
        "nama_resto": "CIE RASA LOOM",
        "alamat_resto": "JL. BUAH BATU No.154 Bandung",
        "thumb_img": "http://10.0.2.2/culigui/images/resto_thumb/cierasaloom.JPG"
    },
    {
        "nama_resto": "AYAM GORENG SUHARTI",
        "alamat_resto": "Jl. Lodaya No. 1",
        "thumb_img": "http://10.0.2.2/culigui/images/resto_thumb/ayamgorengsuharti.JPG"
    },
    {
        "nama_resto": "BAKSO ENGGAL MALANG",
        "alamat_resto": "JL. BURANGRANG 12 BANDUNG",
        "thumb_img": "http://10.0.2.2/culigui/images/resto_thumb/baksoenggal.JPG"
    },
    {
        "nama_resto": "ATMOSPHERE",
        "alamat_resto": "Jl.Lengkong Besar No.97",
        "thumb_img": "http://10.0.2.2/culigui/images/resto_thumb/atmosphere.JPG"
    },
    {
        "nama_resto": "WARUNG STEAK AND SHAKE",
        "alamat_resto": "Jl. Jend Gatot Subroto 28",
        "thumb_img": "http://10.0.2.2/culigui/images/resto_thumb/warungsteak.JPG"
    }
]
}

and here's my main activity :

public class MenuViewAll extends Activity {

// url to make request
private static String url = "http://10.0.2.2/culigui/getdataresto.php";

public static String KEY_ID,KEY_NAME,KEY_ADDRESS,KEY_THUMB; 


ListView Listview;
LazyAdapter adapter;

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

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

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting JSONArray of listresto
        JSONArray  listresto = json.getJSONArray("listresto");

        // looping through All listresto
        for(int i = 0; i < listresto.length(); i++){
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject list = listresto.getJSONObject(i);


            // insert String to Local Variable
            //map.put(KEY_ID, list.getString("id_resto"));
            map.put(KEY_NAME, list.getString("nama_resto"));
            map.put(KEY_ADDRESS, list.getString("alamat_resto"));
            map.put(KEY_THUMB, list.getString("thumb_img"));
            userList.add(map);

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


    /**
     * Updating parsed JSON data into ListView
     * */


    //this is new custom adapter
    Listview = (ListView) findViewById (R.id.list);
    adapter = new LazyAdapter(this, userList); 
    Listview.setAdapter(adapter);

and here's my LaxyAdapter class :

public class LazyAdapter extends BaseAdapter  {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 


public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listitemviewall, null);

    TextView namaresto = (TextView)vi.findViewById(R.id.name); // resto name
    TextView alamatresto = (TextView)vi.findViewById(R.id.address); // resto address
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.defaultthumb); // thumb image

    HashMap<String, String> resto = new HashMap<String, String>();
    resto = data.get(position);

    // Setting all values in listview
    namaresto.setText(resto.get(MenuViewAll.KEY_NAME));
    alamatresto.setText(resto.get(MenuViewAll.KEY_ADDRESS));
    imageLoader.DisplayImage(resto.get(MenuViewAll.KEY_THUMB), thumb_image);
    return vi;
}

}
Was it helpful?

Solution

i solved this problem...

the problem is in this code

        // insert String to Local Variable
        //map.put(KEY_ID, list.getString("id_resto"));
        map.put(KEY_NAME, list.getString("nama_resto"));
        map.put(KEY_ADDRESS, list.getString("alamat_resto"));
        map.put(KEY_THUMB, list.getString("thumb_img"));
        userList.add(map);

when i check on logcat with

//for checking value
//System.out.println("output: " +map);

the value is null and only sent the last value which is KEY_THUMB...

and the correct code is :

        // insert String to Local Variable
        //map.put("KEY_ID", list.getString("id_resto"));
        map.put("KEY_NAME", list.getString("nama_resto"));
        map.put("KEY_ADDRESS", list.getString("alamat_resto"));
        map.put("KEY_THUMB", list.getString("thumb_img"));
        userList.add(map);

that will do perfectly.thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top