有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Android Java中,公共阵列列表的大小在两个地方显示不同

我有一个公共的ArrayList,我想在其中存储用户ID,当我存储个人ID时,它似乎在getContacts函数中很好地显示ArrayList,但是当我以后让我的onclick函数用于列表时,位置整数似乎很好地工作,我已经用toasts测试了它,现在我需要的是类似ChatID的东西。get(position),但它一直将数组(chatids)返回为空

这是我的密码

import 安卓.content.Context;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import com.安卓.volley.RequestQueue;
import com.安卓.volley.Response;
import com.安卓.volley.VolleyError;
import com.安卓.volley.toolbox.StringRequest;
import com.安卓.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.安卓.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;
        }
    }
}

为什么聊天ID的内容在getContacts函数上得到更新,而在onClick侦听器上却没有!谢谢,非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

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

    局部变量在onResponse()方法中:

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