这就是我向 ListView 添加项目的方式:
public class ServersAdapter extends ArrayAdapter<String> {
public ServersAdapter(Context context, int resource, int textViewResourceId, String[] servers) {
super(context, resource, textViewResourceId, servers);
Log.d("noc", "ServersAdapterin: ");
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("noc", "getView: ");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.servers_list, parent, false);
Log.d("noc", "inflate: ");
//datasource = new ServersDataSource(getContext());
datasource.open();
//servir list
Log.d("noc", "ServersDataSource: ");
Server[] servers = datasource.getAllServers();
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
Log.d("noc", "setText: ");
if (servers.length > 0) {
tv.setText(servers[position].getTitle());
Log.d("noc", "getTitle: ");
Log.d("noc", "servers[position].getTitle()-==================: "+ servers[position].getTitle());
if (servers[position].getEnabled() < 1) {
iv.setImageResource(R.drawable.ic_server_status_red);
} else {
iv.setImageResource(R.drawable.ic_server_status_green);
}
}
return row;
}
}
这是 onCreate:
......
try {
Log.d("noc", "need to delete this: ");
datasource = new ServersDataSource(this);
datasource.open();
//servir list
Log.d("noc", "servir list: ");
//String[] servers = new String[10];
//servir list
Log.d("noc", "ServersDataSource: ");
String[] servers = new String[10];
Log.d("noc", "end need to delete this");
ArrayAdapter<Server> adapter = new ServersAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1, servers);
setListAdapter(adapter);
} catch (Exception e) {
Log.d("noc", "error (ArrayAdapter): " + e.toString());
}
我无法从 ListView 的所有内容中清除它(我正在尝试清空它)。我一直在尝试这样做:
ArrayAdapter<Server> adapter = (ArrayAdapter<Server>) getListAdapter();
Server server = null;
switch (view.getId()) {
case R.id.add:
// Save the new server to the database
// server_id, "title", "url", "ip", enabled, "services", fetch_interval
server = datasource.createServer(1, "title of the server", "url of the server", "ip of the server", 1, "services of the server", 1);
adapter.add(server);
break;
case R.id.delete:
Log.d("noc", "delete ");
if (getListAdapter().getCount() > 0) {
datasource.emptyServersTable();
Log.d("noc", "clear ");
adapter.clear();
Log.d("noc", "after clear ");
}
break;
}
Log.d("noc", "notifyDataSetChanged");
adapter.notifyDataSetChanged();
这是单击“删除”时的输出:
01-11 23:41:54.210: D/noc(14898): delete
01-11 23:41:54.210: D/noc(14898): emptyServersTable:
01-11 23:41:54.230: D/noc(14898): clear
01-11 23:41:54.230: D/AndroidRuntime(14898): Shutting down VM
01-11 23:41:54.230: W/dalvikvm(14898): threadid=1: thread exiting with uncaught exception (group=0x4140e2a0)
01-11 23:41:54.230: E/AndroidRuntime(14898): FATAL EXCEPTION: main
01-11 23:41:54.230: E/AndroidRuntime(14898): java.lang.IllegalStateException: Could not execute method of the activity
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$1.onClick(View.java:3699)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View.performClick(View.java:4223)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$PerformClick.run(View.java:17275)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Handler.handleCallback(Handler.java:615)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Looper.loop(Looper.java:137)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-11 23:41:54.230: E/AndroidRuntime(14898): at dalvik.system.NativeStart.main(Native Method)
01-11 23:41:54.230: E/AndroidRuntime(14898): Caused by: java.lang.reflect.InvocationTargetException
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$1.onClick(View.java:3694)
01-11 23:41:54.230: E/AndroidRuntime(14898): ... 11 more
01-11 23:41:54.230: E/AndroidRuntime(14898): Caused by: java.lang.UnsupportedOperationException
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.remove(AbstractList.java:638)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.removeRange(AbstractList.java:658)
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.clear(AbstractList.java:466)
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258)
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.wr.noc.ServersStatus.onClick(ServersStatus.java:91)
01-11 23:41:54.230: E/AndroidRuntime(14898): ... 14 more
那么有什么问题。还有其他方法吗?