2011-07-10 43 views
3

請指導以下代碼錯在哪裏,它沒有創建包括動態圖像在內的適當的列表活動及其從服務器 輸出的描述。這裏是圖像 enter image description here設置動態列表活動

,因爲它應該是這樣的

enter image description here

如可以看到只有一個圖像加載不帶任何說明和其餘沒有圖像甚至沒有空間,請指導什麼是錯誤的XML和代碼。 下面是代碼

主類

public class Test extends ListActivity { 


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

     // install handler for processing gui update messages 
     ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); 
     JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=123"); 


     try{ 

      JSONArray earthquakes = json.getJSONArray("services"); 

      for(int i=0;i<earthquakes.length();i++){       
       HashMap<String, Object> map = new HashMap<String, Object>(); 
       JSONObject e = earthquakes.getJSONObject(i); 

       map.put("id", e.getString("taskid")); 
       map.put("pic", "Service name : " + e.getString("employeepic")); 
       map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime") 
         +" To " + e.getString("endtime")); 


       String imageid = e.getString("employeepic"); 


        ImageView iv = (ImageView) findViewById(R.id.image); 
        Bitmap b = getUserPic(imageid); 
        iv.setImageBitmap(b); 


        map.put("img",b); 
       mylist.add(map);    
      }  
     }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

     SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,new String[] {"img", "servicename", "serviceinfo" }, 
         new int[] { R.id.image ,R.id.items, R.id.item_subtitle }); 

     setListAdapter(adapter); 

     final ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
       @SuppressWarnings("unchecked") 
       HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
       Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

      } 
     }); 

    } 

    public Bitmap getUserPic(String picID) { 
     String imageURL; 
     Bitmap bitmap = null; 
     Log.d("BITMAP", "Loading Picture"); 
     imageURL = "http://midsweden.gofreeserve.com/proj/admin/"+picID; 
     try { 
      bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent()); 
     } catch (Exception e) { 
      Log.d("BITMAP", "Loading Picture FAILED"); 
      e.printStackTrace(); 
     } 
     return bitmap; 
    } 


} 

這裏是jsonfunction類我覺得有需要發佈,但只是爲別人的理解如下

public class JSONfunctions { 

    public static JSONObject getJSONfromURL(String url){ 
     InputStream is = null; 
     String result = ""; 
     JSONObject jArray = null; 

     //http post 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 
       result=sb.toString(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     try{ 

      jArray = new JSONObject(result);    
     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

     return jArray; 
    } 
} 

我有兩個XML檔案,然後主要爲這個項目,這是 test.xml

<TextView 

    android:id="@+id/items" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:padding="2dp" 
    android:textSize="20dp" /> 
    <TextView 
    android:id="@+id/item_subtitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:textSize="18dp" /> 
</LinearLayout> 

listplace架

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">  
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:scaleType="centerCrop"/> 
    <ListView 
     android:id="@id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="false" /> 

    <TextView 
     android:id="@id/android:empty" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="No data"/> 

</LinearLayout> 

我知道有lazylist可用的,但我想設置這對我的理解。謝謝

+0

你可以發佈你想怎麼它工作時看草圖/圖。 – Kenny

+0

@ Kenny謝謝。我已經用問題 – umar

回答

4

對的,我已經有了另一個例子項目爲你您可以下載here.它的太多的代碼發佈,所以我壓縮了項目,所以你可以下載它。如果你不確定有什麼工作只是問!請享用。

BTW它看起來像這樣:

+0

非常感謝。我非常感謝你。你已經爲我解決了一個大問題。再次感謝 – umar

0

我幾天前有同樣的問題。 AFAIK你需要創建一個自定義ArrayAdapter。這篇文章幫了我不少:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

..this也發佈似乎是一個好辦法:

http://united-coders.com/phillip-steffensen/android-dealing-with-listactivities-customized-listadapters-and-custom-designed-0

+0

中的圖片更新了它,感謝您的關注,請你指導我如何爲我的代碼設置適配器類 – umar