Fill the ListView in this way, where Dialogs is an array of String .

There was a question how to clear this ListView already after it was filled?

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

Complete code:

 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){} } }); } } 

Added error:

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 $ SimpleLteIterator.rebuild 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 $ Metho dAndArgsCaller.run (ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

    2 answers 2

    Try this:

     this.adapter.clear(); this.adapter.notifyDataSetChanged(); 
    • adapter.clear (); the application crashes on this line - Nikola Krivosheya
    • Can I see the code completely? Perhaps somewhere the link to the adapter object is reset. And with what error crashes? - Ivan Dembicki
    • added to the first post - Nikola Krivosheya
    • Can I still see the error code? Perhaps adding this. will help this. before contacting the adapter. Update the answer. - Ivan Dembicki
    • added to the first post - Nikola Krivosheya

    There are 2 solutions:

    1. Clear the collection with the clear() method and notify the adapter of data changes:

       Dialogs.clear(); adapter.notifyDataSetChanged(); 
    2. Remove the adapter at all:

       listView.setAdapter(null); 
    • Dialogs.clear (); the array has no such method - Nikola Krivosheya
    • @NikolaKrivosheya I thought you were using the adapter's constructor, where the 3th parameter is a collection of type List. Essentially does not change, try to clear the array. String[] Dialogs = new String[0]; - TheNorthon pm