2

我已经成功地基于此链接使用带有 LazyAdapter 的 ListView 构建了一个应用程序

但我的列表视图中有异常。每个列表视图有 1 个图像视图和 2 个文本视图。所以惰性适配器将向imageview显示“缩略图”,向Textview1显示“名称”,向Textview2显示“地址”......

ImageView 是正确的……它可以显示数据库中的图像缩略图……但这里的问题是,BOTH TEXTVIEW 没有显示正确的数据!而是显示数据库中的“名称”和“地址”......它们显示缩略图图像的链接。

所以,也许任何人都可以帮助我。谢谢b4。

这是我的 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"
    }
]
}

这是我的主要活动:

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

这是我的 LaxyAdapter 类:

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

}
4

1 回答 1

1

我解决了这个问题...

问题出在这段代码中

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

当我检查 logcat 时

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

该值为空,仅发送最后一个值为 KEY_THUMB ...

正确的代码是:

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

那会做得很好。谢谢。

于 2012-04-14T02:08:05.687 回答