我已经成功使用基于此link
的LazyAdapter的ListView构建应用程序但我的列表视图中有异常。 每个列表视图都有1个imageview和2个textview。 所以懒惰的适配器将显示“thumbnail”到imageview,“name”到Textview1,“address”到Textview2 ......
ImageView是coorect ...它可以显示数据库中的图像缩略图... 但这里的问题是,两个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;
}
}
答案 0 :(得分: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);
该值为null,仅发送最后一个值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);
这将是完美的。谢谢。