2012-04-12 35 views
2

我已經成功建立在此基礎上link的TextView上LazyAdapter顯示錯誤的數據

與LazyAdapter一個使用應用程序的ListView但有anomali在我的列表視圖。 每個listview有1個imageview和2個textview。 所以懶適配器將顯示「縮略圖」,以imageview的和「名」來Textview1和「地址」到Textview2 ...

的ImageView的是coorect ......它可以顯示從數據庫中的圖像的縮略圖... 但這裏的問題是,兩個TEXTVIEW沒有顯示正確的數據! 而不是顯示數據庫中的「名稱」和「地址」......它們顯示THUMBNAIL IMAGE的LINK。

所以,也許任何人都可以幫助我。 謝謝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; 
} 

} 

回答

1

我解決了這個問題...

概率LEM是在此代碼

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

這將完成。感謝。

+0

應用程序崩潰...! – Prasad 2015-01-03 12:28:12