将数组列表作为参数传递并显示为列表

时间:2019-01-31 21:07:57

标签: android android-arrayadapter

我正在尝试将ArrayList传递给一个函数,该函数然后将其以列表格式显示,但是看来我的参数对于Arrayadapter是错误的。

@Override
public void handleResult(final Result result) {

    final RequestQueue mQueue = Volley.newRequestQueue(this);
    String url = "https://fg3qzgb2va.execute-api.ap-southeast-1.amazonaws.com/sample";
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        for (int i = 0; i < response.length() ; i++) {

                            JSONObject jsonArray = response.getJSONObject(i);
                            String first = jsonArray.getString("Name");
                            String price = jsonArray.getString("Price");
                            int priceResult = Integer.parseInt(price);
                            String id = jsonArray.getString("id");



                            if(id.equals(result.getText())){
                                addItem.setName(first);
                                addItem.setPrice(priceResult);
                                MainActivity.resultTextView.setText("Name: " + addItem.getName() + "\n"+"Price: " + addItem.getPrice());

                                itemList = new ArrayList<>();
                                itemList.add(addItem.getName());
                                addItem.setName("");

                                addToList.addList(itemList);

                            }
                            //Storing into the list

                        }

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


public class AddToList extends AppCompatActivity {

    ArrayAdapter<String> adapter;

    public void addList(ArrayList<String> arrayList ){

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arrayList);
        adapter.notifyDataSetChanged();
        MainActivity.list.setAdapter(adapter);

    }
}

2 个答案:

答案 0 :(得分:0)

尝试一下

删除行:

MainActivity.list.setAdapter(adapter); 

添加:

ListView mList = MainActivity.list;
mList.setAdapter(adapter);

答案 1 :(得分:0)

尝试将public void addList(ArrayList<String> arrayList )更改为public void addList(List<String> arrayList )