代码使用baseadapter使用项目填充listview。为了显示空列表消息,我使用setEmptyView处理空列表。但它因IndexOutOfBoundsException而失败。我做错了什么?
活动
ArrayList<String> fNameTemp = (ArrayList<String>) intent.getSerializableExtra("fName_Array");
ArrayList<String> lNameTemp = (ArrayList<String>) intent.getSerializableExtra("lName_Array");
MyListAdapter myListAdapter = new MyListAdapter SecondActivity.this,fNameTemp ,lNameTemp );
namesList = (ListView) findViewById(R.id.list_names);
namesList.setEmptyView(findViewById(R.id.emptyElement));
namesList.setAdapter(myListAdapter);
在xml中
<ListView
android:id="@+id/list_names"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="EMPTY!"
android:visibility="gone" />
适配器
private LayoutInflater mInflater;
private ArrayList<String> fName = new ArrayList<String>();
private ArrayList<String> lName = new ArrayList<String>();
public MyListAdapter (Context c, ArrayList<String> LS_fName , ArrayList<String> LS_lName {
mInflater = LayoutInflater.from(c);
this.fName = LS_fName;
this.lName = LS_lName;
}
public int getCount() {
return fName.size();
}
public Object getItem(int position) {
return fName.get(position);
}
public long getItemId(int position) {
return position;
}
static class ViewHolder {
TextView value_fName;
TextView value_lName;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.item_names, null);
holder = new ViewHolder();
holder.value_fName = (TextView) convertView.findViewById(R.id.value_fName);
holder.value_lName = (TextView) convertView.findViewById(R.id.value_lName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.value_fName.setText(fName.get(position));
holder.value_lName.setText(lName.get(position));
return convertView;
}
答案 0 :(得分:0)
您必须使用夸大的view
,仅引用不起作用。
namesList.setEmptyView(findViewById(R.id.emptyElement));
实施例
// Create a progress bar to display while the list loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
namesList.setEmptyView(progressBar);