自定义列表视图无法显示排球请求

时间:2017-01-05 17:42:11

标签: java android listview android-volley

我创建了一个自定义列表视图,它将显示我的数据库中的区域列表,但有时数据不会显示。在活动中停留几秒钟并再次重新启动活动后,它将显示列表。这是我的代码,你可以检查一下吗?如果看到问题是什么。谢谢!

AreaListActivity.java

private ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setupActionBar();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}

@Override
protected void onResume() {
    super.onResume();
    setupList();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // handle arrow click here
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Only if you need to restore open/close state when
    // the orientation is changed
    if (adapter != null) {
        adapter.saveStates(outState);
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // Only if you need to restore open/close state when
    // the orientation is changed
    if (adapter != null) {
        adapter.restoreStates(savedInstanceState);
    }
}

private void setupList() {

    ListView listView = (ListView) findViewById(R.id.list_view);
    adapter = new ListAdapter(this, createList());
    listView.setAdapter(adapter);
}

private List<String> createList() {
    final List<String> list = new ArrayList<>();

    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject obj = jsonArray.getJSONObject(i);
                    String areaName = obj.getString("area_name");
                    list.add(areaName);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    GetAreaListRequest registerRequest = new GetAreaListRequest("True", responseListener);
    RequestQueue queue = Volley.newRequestQueue(AreaListActivity.this);
    queue.add(registerRequest);

    return list;
}

private void setupActionBar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

GetAreaListRequest.java

public class GetAreaListRequest extends StringRequest {

private static final String GET_AREA_LIST_URL = "http://192.168.1.7:8080/smsams/get_area_list.php";
private Map<String, String> params;

public GetAreaListRequest(String get_area_status, Response.Listener<String> listener) {
    super(Method.POST, GET_AREA_LIST_URL, listener, null);

    params = new HashMap<>();
    params.put("get_area_status", get_area_status);

}

@Override
public Map<String, String> getParams() {
    return params;
}

ListAdapter.java

class ListAdapter extends ArrayAdapter<String> {
private final LayoutInflater mInflater;
private final ViewBinderHelper binderHelper;
private Context context;

ListAdapter(Context context, List<String> objects) {
    super(context, R.layout.row_list, objects);
    mInflater = LayoutInflater.from(context);
    binderHelper = new ViewBinderHelper();
    this.context = context;
    // uncomment if you want to open only one row at a time
    // binderHelper.setOpenOnlyOne(true);
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_list, parent, false);

        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.text);
        holder.statView = convertView.findViewById(R.id.statistic_layout);
        holder.mapView = convertView.findViewById(R.id.map_layout);
        holder.infoView = convertView.findViewById(R.id.info_layout);
        holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_layout);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final String item = getItem(position);
    if (item != null) {
        binderHelper.bind(holder.swipeLayout, item);

        holder.textView.setText(item);
        holder.statView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, StatisticActivity.class);
                context.startActivity(i);
            }
        });
        holder.mapView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(context, MapsActivity.class);
                i.putExtra("soilArea", holder.mapView.toString());
                context.startActivity(i);
            }
        });
        holder.infoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(context, InfoViewActivity.class);
                context.startActivity(i);
            }
        });
    }

    return convertView;
}

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onSaveInstanceState(Bundle)}
 */
void saveStates(Bundle outState) {
    binderHelper.saveStates(outState);
}

/**
 * Only if you need to restore open/close state when the orientation is changed.
 * Call this method in {@link android.app.Activity#onRestoreInstanceState(Bundle)}
 */
void restoreStates(Bundle inState) {
    binderHelper.restoreStates(inState);
}

private class ViewHolder {
    TextView textView;
    View statView;
    View mapView;
    View infoView;
    SwipeRevealLayout swipeLayout;
}

2 个答案:

答案 0 :(得分:1)

试试这个:

 private  List<String> list = new ArrayList<>(); 
    private void setupList() {

        ListView listView = (ListView) findViewById(R.id.list_view);
        adapter = new ListAdapter(this, list);
        listView.setAdapter(adapter);
    }

    private void createList() {


        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject obj = jsonArray.getJSONObject(i);
                        String areaName = obj.getString("area_name");
                        list.add(areaName);
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        GetAreaListRequest registerRequest = new GetAreaListRequest("True", responseListener);
        RequestQueue queue = Volley.newRequestQueue(AreaListActivity.this);
        queue.add(registerRequest);


    }

答案 1 :(得分:0)

网络请求通常以异步方式发生。因此没有特定的时间来调用onResponse()。因此,在createList()方法中,您初始化了一个空的arraylist,并在请求生成响应之前(异步调用onResponse)返回不包含任何值的列表。

您可以更改类似的代码:

createList()

Response.Listener<String> responseListener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONArray jsonArray = new JSONArray(response);
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject obj = jsonArray.getJSONObject(i);
                String areaName = obj.getString("area_name");
                list.add(areaName);
                adapter = new ListAdapter(this, list);
                listView.setAdapter(adapter);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
};

所以这里只有在从网络中提取数据后才设置适配器。