To save and retrieve values from a server in a list

Asked 2 years ago, Updated 2 years ago, 86 views

I checked to get the value from the server, but the item I added from Asyncask is not caught in the search function.ㅠ onIf you add list.add("item") to Create, there will be no problem, but Asyncask does not know how to solve this problem.ㅠ<

It's the onCreate part.

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

        editSearch = (EditText) findViewById(R.id.address_group_edtadd);
        listView = (ListView) findViewById(R.id.address_group_addlist);

        list = new ArrayList<>();
        Tablename = new ArrayList<>();
        // Create a list.

        //Send server class
        InsertData task = new InsertData();
        task.execute("serverURL");

        // Pre-save the data to be used for the search.
        //settingList();

        // Copy all data from the list to the arraylist.// Make a copy of the list.
        arraylist = new ArrayList<String>();
        arraylist.addAll(list);

        // Create an adapter to be linked to the list.
        adapter = new AddressGroupAdapter(list, this);

        // Connect the adapter to the list view.
        listView.setAdapter(adapter);

        // When entering a search term in the input window, define the "addTextChangedListener" event listener.
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                // Called whenever a character is entered in the input window.
                // Invoke the search method.
                String text = editSearch.getText().toString();
                search(text);
            }
        });
    }

This is where you get the value from the server.

class InsertData extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {
            JSONObject Land = new JSONObject(result);
            JSONArray jsonArray = Land.getJSONArray("Response");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject subJsonObject = jsonArray.getJSONObject(i);
                String TableName = subJsonObject.getString("serverURL");
                Tablename.add(TableName);
                namelist = Tablename.get(i);
                list.add(TableName); //Add value received from server to list
            }
        } } catch (JSONException e) {
            e.printStackTrace();
            }
        }
    }



android java listview

2022-09-22 18:47

1 Answers

If I can guess from the sauce... Before loading data from AsyncTask and putting it in the list, it seems that the list is contained in the adapter and bound to the list view.

adapter = new AddressGroupAdapter(list, this);

listView.setAdapter(adapter);

I think we can put two lines in the onPostExecute.


2022-09-22 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.