每当我使用文本搜索时,布局都不会显示任何内容,但是使用id搜索

时间:2019-06-04 05:46:34

标签: android-volley

我的应用程序具有使用连接到在线数据库的API的带有搜索界面的列表视图,我的问题是搜索功能可以使用id,但是当我单击自动完成文本视图上的关键字建议时却无法使用

我试图更改我设法获取所有数据的API,但这使搜索功能无用

没有错误,但仅适用于ID而不是文本

SubCategoryActivity.class

package in.net.nihal.nobbi;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewManager;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.pixplicity.easyprefs.library.Prefs;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import in.net.nihal.nobbi.adapter.SubCatAdapter;
import in.net.nihal.nobbi.classes.BaseActivity;
import in.net.nihal.nobbi.impl.CartDao;
import in.net.nihal.nobbi.model.CartData;
import in.net.nihal.nobbi.model.SubCatModel;
import in.net.nihal.nobbi.utils.Constants;
import in.net.nihal.nobbi.utils.Util;
import in.net.nihal.nobbi.utils.Utility;

public class SubCategoryActivity extends BaseActivity {

    private ProgressDialog pd;
    private SubCatModel subCatModel;
    private SubCatAdapter subCatAdapter;
    private ListView listView;
    private String ID, TITLE;
    private CartDao cartDao;
    private String URL
            , ID_KEY;
    private TextView txtViewCount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_news);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        cartDao = new CartDao(this);
        try {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                ID = extras.getString("ID");
                TITLE = extras.getString("TITLE");
                Log.d("aa", ID);
                Log.d("aaa", TITLE);
            }
            this.setTitle(TITLE);
            if (Constants.VENDOR_STATUS.equals("1")) {
                URL = "search.php";
                ID_KEY = "service";
            } else {
                URL = "sub_category.php";
                ID_KEY = "id";
            }
            listView = (ListView) findViewById(R.id.listView);
            if (Utility.checkInternetConnection(getApplicationContext())) {
                subCatData();
            } else {
                Util.customToast(getApplicationContext(), getString(R.string.internet_unavailable));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (Constants.refreshFlag) {
            updateHotCount();
            subCatAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

    private void subCatData() {
        pd = ProgressDialog.show(SubCategoryActivity.this, "",
                getString(R.string.please_wait));
        pd.setCancelable(false);

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                Constants.BASE_URL + URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (pd != null)
                            pd.dismiss();

                        try {

                            JSONObject obj = new JSONObject(response.toString());
                            Log.d("Response-->", obj.toString());
                            if (obj.getString("status").equals("1")) {
                                subCatModel = new Gson().fromJson(obj.toString(), SubCatModel.class);
                                subCatAdapter = new SubCatAdapter(SubCategoryActivity.this, subCatModel, SubCategoryActivity.this);
                                listView.setAdapter(subCatAdapter);
                            } else {
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (pd != null)
                            pd.dismiss();
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(ID_KEY, ID);
                params.put("city", Prefs.getString(Constants.SharedPreferences_City, "").trim());
                return params;
            }
        };

        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        final View notificaitons = menu.findItem(R.id.action_cart).getActionView();

        txtViewCount = (TextView) notificaitons.findViewById(R.id.txtCount);
        updateHotCount();
        notificaitons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //    TODO
                Constants.refreshFlag = true;
                Intent cart = new Intent(getApplicationContext(), CartActivity.class);
                cart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(cart);
            }
        });
        return true;
    }

    public void updateHotCount() {
        Constants.refreshFlag = true;
        if (cartDao.getAll() != null) {
            final int count = cartDao.getAll().size();
            if (count < 0) return;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (count == 0)
                        txtViewCount.setVisibility(View.GONE);
                    else {
                        txtViewCount.setVisibility(View.VISIBLE);
                        txtViewCount.setText(Integer.toString(count));
                        // supportInvalidateOptionsMenu();
                    }
                }
            });
        }

    }

    public void addItemInCart(SubCatModel.NewsBean newsBean, final int quantity,
                              String press, final String buyPrice) {
        if (cartDao.isRecordExist(newsBean.getId(), newsBean.getSub_category(), newsBean.getChild_category()) > 0) {
            if (quantity == 0){
                deleteItemFromCart(newsBean);
            }
            else {
                cartDao.updateData(newsBean.getId(),
                        newsBean.getSub_category(), newsBean.getChild_category(),
                        quantity, buyPrice);
            }
        } else {
            if (quantity == 0){
                Toast.makeText(this, "Units should be at least 1", Toast.LENGTH_SHORT).show();
            }
            else {
                CartData cartData = new CartData(newsBean.getId(), newsBean.getMain_category(),
                        newsBean.getSub_category(), newsBean.getChild_category(), newsBean.getAmount_service(),
                        newsBean.getSubcategory_image(), "sub_cat", String.valueOf(quantity), newsBean.getAmount_service(),
                        newsBean.getInspection());
                cartDao.insert(cartData);
                Toast.makeText(this, "Successfully added in cart", Toast.LENGTH_SHORT).show();
            }

        }

        updateHotCount();
    }

    private void deleteItemFromCart(SubCatModel.NewsBean newsBean) {
        if (cartDao.delete(newsBean.getId(),
                newsBean.getSub_category(), newsBean.getChild_category()) == 1) {
            updateHotCount();
            Toast.makeText(getBaseContext(), "Successfully deleted from cart", Toast.LENGTH_SHORT).show();

        }
    }



}  `



0 个答案:

没有答案