即使我之前使用过几乎相同的代码,也不会显示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
不确定这是不是为什么没有显示。
答案 0 :(得分:2)
您的构造函数中的super
调用不包含ArrayList
,也不会覆盖getCount()
以返回列表大小,因此您的Adapter
正在返回0
用于项目计数,ListView
不会尝试检索任何View
。您可以在ArrayList
来电中加入super
,或覆盖getCount()
方法以返回cities.size()
。
但是,对于简单的Adapter
ArrayList
,您不一定需要自定义String
。您可以简单地使用ArrayAdapter
的构造函数来获取布局和TextView
资源ID。
ArrayAdapter<String> citiesListAdapter = new ArrayAdapter<>(this,
R.layout.cityitem,
R.id.lblCity,
cities);
答案 1 :(得分:0)
ListView中没有布局。您需要在ListView中添加布局(线性或相对)。喜欢这个!
{{1}}
在布局中,您声明要显示的内容 - TextView或ImageView等。