2016-04-23 120 views
1

即使我以前使用幾乎相同的代碼,ListView元素也不會顯示。對不起,我不得不問這種類型的問題,但我認真只是不明白爲什麼它不工作。我看到的其他類似問題都沒有用。ListView元素不顯示

項目和ListView的高度應該沒問題。不知道發生了什麼事。多謝你們。

下面的代碼轉儲:

主要胡亞蓉:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_select_city); 
    findViewById(R.id.loadingPanel).setVisibility(View.GONE); // remove loading animation 

    ArrayList<String> citiesList = new ArrayList<>(); 
    citiesList.add("Waterloo"); 
    citiesList.add("Guelph"); 
    populateCities(citiesList); 
} 

public void populateCities(ArrayList<String> citiesList) { 

    // see what the list is 
    Toast.makeText(getApplicationContext(),citiesList.toString(),Toast.LENGTH_LONG).show(); 

    // populate the ListView 
    CitiesListAdapter citiesListAdapter = new CitiesListAdapter(this, citiesList); 
    ListView citiesListView = (ListView)findViewById(R.id.cities_list); 
    citiesListView.setAdapter(citiesListAdapter); 

    // listen for when a city in the list is clicked 
    citiesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // go to the bar list activity (send the city and province with it) 
      TextView lblCity = (TextView)view.findViewById(R.id.lblCity); 
      String city = lblCity.getText().toString(); 
      Toast.makeText(getApplicationContext(), city, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

這裏的列表適配器代碼(CitiesListAdapter):

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 
import java.util.ArrayList; 

public class CitiesListAdapter extends ArrayAdapter<String> { 

    private final Activity context; 
    private final ArrayList<String> cities; 

    public CitiesListAdapter(Activity context, ArrayList<String> cities) { 
     super(context, R.layout.cityitem); 
     this.context = context; 
     this.cities = cities; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.cityitem, null, true); 

     TextView lblCity = (TextView)rowView.findViewById(R.id.lblCity); 
     lblCity.setText(this.cities.get(position)); 

     return rowView; 
    } 
} 

那麼這裏主要佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.myapp.SelectCity"> 

    <RelativeLayout 
     android:id="@+id/loadingPanel" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" > 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:indeterminate="true" 
      android:id="@+id/progressBar" /> 
    </RelativeLayout> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnBack" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="5dp" 
     android:onClick="back" 
     android:src="@mipmap/ic_menu_back" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnRefresh" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="5dp" 
     android:onClick="refresh" 
     android:src="@mipmap/ic_action_refresh" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Select City" 
     android:id="@+id/lblTitle" 
     android:layout_below="@+id/btnBack" 
     android:layout_centerHorizontal="true" /> 


    <RelativeLayout 
     android:id="@+id/listViewLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/lblTitle" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginBottom="20dp" 
     android:layout_marginTop="20dp"> 

     <ListView 
      android:background="#F00" 
      android:id="@+id/cities_list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" /> 

    </RelativeLayout> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_below="@+id/listViewLayout" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

然後ListView的項目佈局(cityitem.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="City" 
     android:id="@+id/lblCity" /> 
</LinearLayout> 

編輯:看來我也越來越這種愚蠢的錯誤的Android似乎無法修復(只是在偷偷logcat中):

E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7ccc40 

不知道這是爲什麼沒有顯示。

截圖:http://prntscr.com/avpy9s

+0

可以附上你的截圖 –

+0

@PhanVănLinh編輯。 –

回答

2

super呼叫你的構造不包括ArrayList,也不是你重寫getCount()返回列表的大小,所以你Adapter將返回0的項目數和ListView韓元試圖檢索任何View。您可以在super呼叫中包含ArrayList,或覆蓋getCount()方法以返回cities.size()

但是,對於String s的簡單ArrayList,您不一定需要定製Adapter。您可以簡單地使用ArrayAdapter的構造函數,該構造函數需要一個佈局和一個TextView資源ID。

ArrayAdapter<String> citiesListAdapter = new ArrayAdapter<>(this, 
                  R.layout.cityitem, 
                  R.id.lblCity, 
                  cities); 
+1

Jeeez謝謝。複製時刪除太多參數。 –

0

ListView中沒有佈局。您需要在ListView內添加布局(線性或相對)。喜歡這個!

<ListView 
<RelativeLayout /> 
/> 

在佈局,聲明你想顯示什麼 - 的TextView或ImageView的或等