28
Июл
2016

Как очистить listview

Заполняю ListView таким образом, где Dialogs - это массив String.

Возник вопрос, как очистить этот ListView уже после того, как он был наполнен?

adapter = new ArrayAdapter<String>(dialogs.this, android.R.layout.simple_list_item_1, Dialogs);
listView.setAdapter(adapter);

Код полностью:

public class dialogs extends Activity  implements TextWatcher{

    String answer = "";
    String[] Dialogs;
    int login_length = 0;
    String login;
    String password;
    ListView listView;

    AutoCompleteTextView mAutoComplete;
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter1;

    String[] Find_Logins = new String[3];
    //final String[] mContacts = { "123", "13", "1111", "Борис", "Бегемот", "Мурка" };




    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        if(s.toString().length() > 3) {

            OkHttpClient client = new OkHttpClient();
            RequestBody formBody = new FormBody.Builder()
                    .add("find_login", s.toString())
                    .build();
            Request request = new Request.Builder()
                    .url("http://s92640jz.bget.ru/find_login.php")
                    .post(formBody)
                    .build();
            client.newCall(request)
                    .enqueue(new Callback() {
                        @Override
                        public void onFailure(final Call call, IOException e) {
                            // Error
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast toast = Toast.makeText(getApplicationContext(), "Ошибка при отправке запроса, проверьте подключение к сети", Toast.LENGTH_LONG);
                                    toast.show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(Call call, final Response response) throws IOException {
                            String res = response.body().string();
                            Log.d("TAG123", "All response is: " + res);

                            try{
                                JSONObject jsonObject = new JSONObject(res);
                                int count = jsonObject.length();
                                if (count == 3) {
                                    for (int i = 0; i < Find_Logins.length; i++) {

                                        Find_Logins[i] = jsonObject.getString("find_login" + Integer.toString(i));
                                        //Dialogs[i] = jsonObject.getString("find_login" + Integer.toString(i));
                                        //Log.d("TAG123", "Dialog: " + answer);
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < count; i++) {

                                        Find_Logins[i] = jsonObject.getString("find_login" + Integer.toString(i));
                                        //Dialogs[i] = jsonObject.getString("find_login" + Integer.toString(i));
                                        //Log.d("TAG123", "Dialog: " + answer);
                                    }
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.d("TAG123", "olollolololo");
                                        Dialogs = null;
                                        adapter.notifyDataSetChanged();
                                     
                                    }
                                });

                            }
                            catch (JSONException e){}
                        }
                    });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogs);

        listView = (ListView)findViewById(R.id.listView);

        mAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        mAutoComplete.addTextChangedListener(this);



//        mAutoComplete.setAdapter(adapter);
//        mAutoComplete.setAdapter(new ArrayAdapter(this,
//                android.R.layout.simple_dropdown_item_1line, Find_Logins));


        login = getIntent().getExtras().getString("login");
        password = getIntent().getExtras().getString("password");
        Log.d("TAG", "Login: " + login);
        Log.d("TAG", "Password: " + password);
        login_length = login.length();

        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("login", login)
                .add("pass", password)
                .build();
        Request request = new Request.Builder()
                .url("http://s92640jz.bget.ru/get_dialogs.php")
                .post(formBody)
                .build();
        client.newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(final Call call, IOException e) {
                        // Error
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast toast = Toast.makeText(getApplicationContext(), "Ошибка при отправке запроса, проверьте подключение к сети", Toast.LENGTH_LONG);
                                toast.show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, final Response response) throws IOException {
                        String res = response.body().string();
                        Log.d("TAG123", "All response is: " + res);
                        try{
                            JSONObject jsonObject = new JSONObject(res);
                            answer = jsonObject.getString("r_0");
                            //Log.d("TAG123", "answer: " + answer);

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast toast = Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG);
                                    toast.show();
                                }
                            });
                        } catch (JSONException e){}
                        try{
                            JSONObject jsonObject = new JSONObject(res);
                            int count = jsonObject.length();
                            Dialogs = new String[count];

                            for(int i=0; i < count; i++)
                            {
                                String tmp = jsonObject.getString("dialog" + Integer.toString(i)).substring(7);
                                String aCats[] = tmp.split("_");
                                if (!login.equals(aCats[0]))
                                {
                                    Dialogs[i] = aCats[0];

                                }

                                //Dialogs[i] = tmp;
                                //Dialogs[i] = jsonObject.getString("dialog" + Integer.toString(i)).substring(8 + login_length);
                                //Log.d("TAG123", "Dialog: " + answer);
                            }

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    adapter = new ArrayAdapter<String>(dialogs.this,
                                            android.R.layout.simple_list_item_1,
                                            Dialogs);

                                    listView.setAdapter(adapter);


                                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View itemClicked, int position,
                                                                long id) {

                                            Intent intent = new Intent(dialogs.this, messages.class);
                                            intent.putExtra("dialog_name", ((TextView) itemClicked).getText());
                                            intent.putExtra("login", login);
                                            intent.putExtra("password", password);
                                            startActivity(intent);

                                        }
                                    });
                                }
                            });
                        } catch (JSONException e){}

                    }
                });
    }


}

Добавил ошибку:

07-28 17:20:20.168 22413-22439/com.example.jack.myapplication E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe97ca400 07-28 17:20:21.857 22413-22413/com.example.jack.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jack.myapplication, PID: 22413 java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:638) at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75) at java.util.AbstractList.removeRange(AbstractList.java:658) at java.util.AbstractList.clear(AbstractList.java:466) at android.widget.ArrayAdapter.clear(ArrayAdapter.java:273) at com.example.jack.myapplication.dialogs$1$2.run(dialogs.java:140) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Источник: https://ru.stackoverflow.com/questions/549684/%D0%9A%D0%B0%D0%BA-%D0%BE%D1%87%D0%B8%D1%81%D1%82%D0%B8%D1%82%D1%8C-listview

Share

Тебе может это понравится...