检查listview项是否包含字符串,并获取位置

时间:2017-06-09 01:40:50

标签: android listview adapter android-adapter

我有一个应用程序可以检查listview项是否包含特定的字符串,然后使用它的详细信息检索项目位置,但我在教程和堆栈中看到的所有内容都是按下按钮获取它们。

但是我试图在从服务器检索数据时获取位置,并检查从服务器检索的任何项是否包含特定字符串。我如何能够从包含特定字符串的项中检索数据?

这是我的MainActivity:

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

orderID = new ArrayList<>();
orderStatus = new ArrayList<>();

 adapter = new viewOrderAdapter(viewOrdersPage.this, orderID, orderUserNumber, orderDateTime, orderNameJSON, orderLatLong, orderStatus);
    listView.setAdapter(adapter);

    getData();

}

  public void getData() {
    final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
    String url = DATA_URL + textUsername.getText().toString().trim();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    showLIST(response);
                    loading.dismiss();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error instanceof NetworkError) {
                        loading.dismiss();

                        Snackbar snackbar = Snackbar.make(rootView, "Please connect to the Internet", Snackbar.LENGTH_INDEFINITE);
                        snackbar.setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getData();
                            }
                        });
                        snackbar.show();
                    }
                    else if (error instanceof TimeoutError) {
                        loading.dismiss();
                        Snackbar snackbar = Snackbar.make(rootView, "Check if you have Internet Access", Snackbar.LENGTH_INDEFINITE);
                        snackbar.setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getData();
                            }
                        });
                        snackbar.show();
                    }
                    else if (error instanceof NoConnectionError) {
                        loading.dismiss();
                        Snackbar snackbar = Snackbar.make(rootView, "Check if you have Internet Access", Snackbar.LENGTH_INDEFINITE);
                        snackbar.setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getData();
                            }
                        });
                        snackbar.show();
                    }
                }
            }
    );
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}
private void showLIST(JSONArray jsonArray) {
    JSONObject obj = null;

    for (int i = 0; i < jsonArray.length(); i++) {
        try {
            obj = jsonArray.getJSONObject(i);
            orderID.add(obj.getString(ORDERID));
            orderUserNumber.add(obj.getString(ORDERUSERNUMBER));
            orderDateTime.add(obj.getString(ORDERDATETIME));
            orderNameJSON.add(obj.getString(ORDERNAME));
            orderLatLong.add(obj.getString(ORDERLATLONG));
            orderStatus.add(obj.getString(ORDERSTATUS));


//THIS IS WHERE I INITIALLY THOUGHT THE CHECKING WITH OCCUR, THOUGH IT CHECKS IF A ITEM CONTAINS A SPECIFIC STRING.
//BUT IT RETRIEVES ONLY THE FIRST ORDERID EVEN IF IT DOES NOT CONTAIN THE STRING, AND YES IT IS WRONG BECAUSE IT DOES NOT SPECIFY THE SPECIFIC POSITION OF THE ITEM HAHA! SO THIS IS MY PROBLEM.

            if(orderStatus.contains("DECLINED"))
            {

                 }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    viewOrderAdapter listOrderAdapter = new viewOrderAdapter(this,orderID, orderUserNumber, orderDateTime, orderNameJSON, orderLatLong, orderStatus);
    listView.setAdapter(listOrderAdapter);
}

这是我的适配器视图:

public class viewOrderAdapter extends BaseAdapter {

private Context context;

private ArrayList<String> orderID;
private ArrayList<String> orderStatus;


public viewOrderAdapter(Context context, ArrayList<String> orderID, ArrayList<String> orderStatus){
    this.context = context;
    this.orderID = orderID;
    this.orderStatus = orderStatus;

}

@Override
public int getCount() {
    return orderID.size();
}

@Override
public Object getItem(int position) {
    return orderID.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final View listView;
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.view_orders_item, parent, false);

    TextView id = (TextView) listView.findViewById(R.id.orderID);
    TextView status = (TextView) listView.findViewById(R.id.orderStatus);


    id.setText(orderID.get(position));
    status.setText(orderStatus.get(position));

    return listView;
}

}

提前感谢您的帮助! :d

0 个答案:

没有答案