3

这就是我向 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

那么有什么问题。还有其他方法吗?

4

5 回答 5

7

您应该尝试覆盖类clear()中的方法并添加对您的方法的ServersAdapter调用(如果有的话)。如果您的数据从数组加载到视图中,只需清空它。clear()ArrayList

查看您的代码,我认为相关的是服务器

于 2013-01-11T21:17:33.737 回答
6

我猜你向适配器传递了一个列表或一个数组。如果您保留此添加项目的实例,您可以执行以下操作:

adapter.clear();
listview.getAdapter().notifyDataSetChanged();

并非所有适配器都有clear()方法。ArrayAdapter 有,但ListAdapterSimpleAdapter没有

于 2013-01-11T21:06:35.603 回答
0

据我所知,无需清除适配器并将 notifyDataSetChanged() 设置为适配器。你已经使用 List,ArrayList 为项目或数组 1 st 清除它并重新初始化它,并将数据传递给服装适配器后清除使用的数组/列表。它会正常工作!

于 2014-12-04T02:27:34.140 回答
0

该构造函数(带有数组的构造函数)的实现Arrays.asList(objects)会创建一个不可变列表。尝试将带有字符串的显式 ArrayList 传递给此构造函数:

  public ArrayAdapter(Context context, int resource, 
                      int textViewResourceId, List<T> objects)
于 2013-01-11T21:30:13.947 回答
0

试试这个,它会从视图中删除所有项目

    final int adapterCount = adapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View item = adapter.getView(i, null, null);
        myLayout.removeView(item);
    }
于 2016-03-27T09:42:11.267 回答