公开Arraylist的大小在Android Java中的两个位置显示不同

时间:2018-11-16 01:46:15

标签: java android arraylist

我有一个公共ArrayList,我想在其中存储用户ID,当我存储人ID时,似乎在getContacts函数中很好地显示了arraylist,但是当我稍后将onclick函数用于列表时,位置整数似乎可以正常工作,我已经用烤面包对其进行了测试,现在我所需要的只是像chatids.get(position)之类的东西,但是它始终将数组(chatids)返回为空。

这是我的代码。

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MyActivity";

    public ListView contacts;
    public ArrayList<String> chatids = new ArrayList<String>();


    // User Setup Defaults
    String server = "https://xxxxxxxxx";
    String suser = "xxxxxxxx";
    String spass = "xxxxxxxx";

    public void getContacts(final aConAdapter adapterss) {
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = server+"/xxxxxx/?username="+suser+"&password="+spass+"&getcontacts=yes";

        StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Log.v(TAG, "I recieved: " + response);
                        ArrayList<String> chatids = new ArrayList<String>();
                        try {
                            JSONArray parseContacts = new JSONArray(response);

                            for (int x = 0; x < parseContacts.length(); x++) {
                                JSONArray array = (JSONArray) parseContacts.get(x);
                                aConMan newUser = new aConMan(array.get(0).toString());
                                adapterss.add(newUser);
                                String newUser2 = array.get(1).toString();
                                Log.v(TAG, newUser2);
                                chatids.add(newUser2); // This is working, i compared it and it shows as size is 2 when logging
                                //adapter.notifyDataSetChanged();
                            }
                        }catch(JSONException e) {
                            Log.v(TAG, "Error: "+e);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.v(TAG, "Oops, an error occurred"+error);
                aConMan newUser = new aConMan("VolleyError: "+error);
                adapterss.add(newUser);
                aConMan newUser3 = new aConMan("Your internet may have issues.");
                adapterss.add(newUser3);
            }
        });

// Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ArrayList<aConMan> ourlist = new ArrayList<aConMan>();
        aConAdapter adapter = new aConAdapter(this, ourlist);
        final ListView contacts = (ListView)findViewById(R.id.contacts);

        getContacts(adapter);

        //CustomAdapter customAdapter = new CustomAdapter();
        //contacts.setAdapter(customAdapter);



        contacts.setAdapter(adapter);

        contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object o = contacts.getItemAtPosition(position);
                Toast.makeText(getBaseContext(),String.valueOf(chatids.size()),Toast.LENGTH_SHORT).show(); // This still shows 0, no error or anything wrong, i want to debug it by getting the size of the result, it should be 2 if the user has 2 contacts (which shows up fine on the UI display).
            }
        });
    }

    public class aConMan {
        public String defusername;

        public aConMan(String defusername) {
            this.defusername = defusername;
        }
    }

    public class aConAdapter extends ArrayAdapter<aConMan> {
        public aConAdapter(Context context, ArrayList<aConMan> aconman) {
            super(context, 0, aconman);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            aConMan aconmans = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
            }

            TextView tvUname = (TextView) convertView.findViewById(R.id.username);
            tvUname.setText(aconmans.defusername);

            return convertView;
        }
    }
}

为什么在getContacts函数中更新chatid的内容,但在onClick侦听器中却没有更新!谢谢,非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您已将chatids定义为全局变量和局部变量。删除局部变量。

本地变量在您的onResponse()方法中:

public void onResponse(String response) {
    //...
    ArrayList<String> chatids = new ArrayList<String>(); //remove this
    //...
}