2012-09-13 113 views
0

我创建了ListView活动来列出从服务器检索到的一些数据。使用简单适配器的Android ListView

这是ListAtmActivity

public class ListAtmActivity extends ListActivity{ 

private static String url ="http://10.0.2.2:8080/hello/AvailabilityResponse"; 

private static final String ATM_NO = "atmbrno"; 
private static final String ATM_PLACE = "atmbrname"; 

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


    String brName=getIntent().getExtras().getString("key"); 


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

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

    // getting JSON string from URL 
    JSONArray contacts = jParser.getJSONFromUrl(url,brName); 

    try{ 

     for(int i = 0; i < contacts.length(); i++){ 
      JSONObject json_data = contacts.getJSONObject(i); 

      // Storing each json item in variable 
      String atm_id = json_data.getString(ATM_NO); 
      String atm_name = json_data.getString(ATM_PLACE); 

      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(ATM_NO, atm_id); 
      map.put(ATM_PLACE, atm_name); 

      contactList.add(map);     
     }      
    } 

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

    } 
    /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_main, 
      new String[] { ATM_NO, ATM_PLACE }, new int[] { 
        R.id.name , R.id.email }); 

    setListAdapter(adapter); 

    //setContentView(R.layout.list_main); 

}} 

我的联系人数组就是这样的。

{"atmbrname":"ANURADAPURA [ATM 2]","atmbrno":"ATM084"}, 
{"atmbrname":"MANNAR BRANCH ","atmbrno":"ATM344"} 

这里我的两个xml文件也。

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ListView android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" />  
</LinearLayout> 

list_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <!-- Name Label --> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#43bd00" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:paddingTop="6dip" 
     android:paddingBottom="2dip" /> 
    <!-- Description label --> 
    <TextView 
     android:id="@+id/email" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#acacac" 
     android:paddingBottom="2dip"> 
    </TextView> 

</LinearLayout> 

但我不能让列表视图。没有错误出现。我认为我的XML文件存在问题。谁能帮我解决这个问题..

+0

它只是一个空白的屏幕? – fasheikh

+0

是的..它是空白的黑色屏幕..只是应用程序名称是作为标题。 – Dasaya

+0

尝试切换布局,即setContentView(R.layout.list_main)和充气list_item.xml – fasheikh

回答

0

我会尝试设置高度为“match_parent”。但不知道这是否能解决您的问题。

我只有ListActivity的问题,因此我总是使用正常的Activity并从Activityview的Contentview中检索ListView。