如果ListView适配器中没有数据,我想显示刷新Button和TextView。我还希望能够向将重新加载列表的按钮添加单击侦听器。以下是我定义当前活动的方式:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foods);
arrList=new ArrayList<FoodsData>();
listView=(ListView)findViewById(R.id.list_foods);
this.listView.setEmptyView(findViewById(R.id.emptyView));
.....
.....
}
这是我的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
<TextView
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
</LinearLayout>
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
我在哪里设置刷新按钮的点击监听器?
答案 0 :(得分:9)
更改您的xml,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
<TextView
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
</FrameLayout>
<ListView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
</RelativeLayout>
将这些行放在java
中 View empty = findViewById(R.id.emptyView);
btn_retry=(Button)empty.findViewById(R.id.btn_retry);
listView.setEmptyView(empty);
现在,您可以使用相同的xml布局在同一个Activity中编写onClickListener()。
btn_retry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do the refresh thing or verify with Toast.
}
});
此处,如果列表视图为空,则会显示按钮刷新。否则它将显示填充列表。
答案 1 :(得分:0)
您需要通过扩展BaseAdapter
以下列方式为列表视图创建自定义适配器 public class ListViewAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
Context mContext;
ArrayList<String> slist_name;
ArrayList<String> list_deviceId;
/** constructor is initializing */
public ListViewAdapter(Context c, ArrayList<String> name,ArrayList<String> devId) {
mContext = c;
layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
slist_name = name;
list_deviceId=list_deviceId;
}
public int getCount() {
return slist_name.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = null;
convertView = null;
vi = convertView;
if (convertView == null) {
vi = layoutInflater.inflate(R.layout.listview_item, null);
TextView name_textview = (TextView) vi
.findViewById(R.id.textEmp_id);
name_textview.setText(slist_name.get(position));
Button btn = (Button) vi
.findViewById(R.id.btn_id);
btn.setOnClickListener(this);
//add relevent code in onclickListner
}
return vi;
}
}
在您的活动中,按以下方式将自定义适配器添加到listView:
listView = (ListView) findViewById(R.id.listview_employee_id);
adapter = new ListViewAdapter(this, listarray, listarrayDev);
listView.setAdapter(adapter);
答案 2 :(得分:0)
首先必须将Adapter设置为ListView,然后如果需要刷新listView内容使用: adapter.notifyDataSetChanged();
如果您需要删除所有项目使用:
adapter.clear();
adapter.notifyDataSetChanged();
例如:
String[] items = new String[] { "Item1", "Item2", "Item3", "Item4" };
final ListView listView = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, android.R.id.text1, items);
listView.setAdapter(adapter);
Button refreshButton = (Button)findViewById(R.id.button1);
refreshButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>)listview.getAdapter();
if(adapter!= null)
{
adapter.clear();
adapter.notifyDataSetChanged();
//here you can write your listView.setEmptyView(findViewById(R.id.emptyView)); code
}
});
答案 3 :(得分:0)
据我所知,不需要清除适配器并将notifyDataSetChanged()设置为适配器。您已使用List,ArrayList作为项或Array。首先清除它,然后重新初始化它,并在将数据传递给服务适配器后,清除使用的数组/列表。它会工作正常!