2017-02-26 36 views
0

下面是我SearchActivity搜索查看總是給人最初的幾個JSON的值中,

public class SearchActivity extends AppCompatActivity { 
    // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds 
    public static final int CONNECTION_TIMEOUT = 10000; 
    public static final int READ_TIMEOUT = 15000; 
    private SimpleCursorAdapter myAdapter; 
    SearchView searchView = null; 
    private WarehouseSalesDetails[] strArrData; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.search_item); 
     Toolbar toolBarSearch = (Toolbar)findViewById(R.id.toolbarSearch); 
     setSupportActionBar(toolBarSearch); 

     final String[] from = new String[]{"title"}; 
     final int[] to = new int[]{android.R.id.text1}; 
     //setup SimpleCursorAdapter 
     myAdapter = new SimpleCursorAdapter(SearchActivity.this,android.R.layout.simple_spinner_dropdown_item,null,from,to, 
       CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 

     new RetrieveWarehouseSalesTask(this).execute(); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu){ 

     getMenuInflater().inflate(R.menu.menu_search,menu); 
     MenuItem searchItem = menu.findItem(R.id.action_search); 
     SearchManager searchManager = (SearchManager)SearchActivity.this.getSystemService(Context.SEARCH_SERVICE); 
     if(searchItem!=null){ 
      searchView = (SearchView) searchItem.getActionView(); 
     } 
     if(searchView!=null){ 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(SearchActivity.this.getComponentName())); 
      searchView.setIconified(false); 
      searchView.setSuggestionsAdapter(myAdapter); 
      //getting selected on item suggestion 
      searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener(){ 
       @Override 
       public boolean onSuggestionClick(int position){ 
        //Add clicked text to search box 
        CursorAdapter ca = searchView.getSuggestionsAdapter(); 
        Cursor cursor = ca.getCursor(); 
        cursor.moveToPosition(position); 
        searchView.setQuery(cursor.getString(cursor.getColumnIndex("title")),false); 
        Log.d("strArr id",strArrData[position].id); 
        String selectedID = strArrData[position].id; 
        Intent intent = new Intent(SearchActivity.this,RetrieveIndividualWarehouseSales.class); 
        intent.putExtra("pid",selectedID); 
        startActivity(intent); 

        return true; 

       } 

       @Override 
       public boolean onSuggestionSelect(int position){ 
        return true; 
       } 
      }); 
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ 
       @Override 
       public boolean onQueryTextSubmit(String s){ 
        return false; 
       } 
       @Override 
       public boolean onQueryTextChange(String s){ 
        //filter data 
        final MatrixCursor mc = new MatrixCursor(new String[]{ 
          BaseColumns._ID,"title" 
        }); 
        for(int i = 0; i<strArrData.length; i++){ 
         if(strArrData[i].title.toLowerCase().startsWith(s.toLowerCase())){ 
          mc.addRow(new Object[]{i,strArrData[i].id}); 
         } 

        } 

        myAdapter.changeCursor(mc); 
        return false; 
       } 
      }); 


     } 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onNewIntent(Intent intent){ 
     if(Intent.ACTION_SEARCH.equals(intent.getAction())){ 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      if(searchView!=null){ 
       searchView.clearFocus(); 
      } 
     } 
    } 

    class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,Void> { 
     private String TAG = ActiveWarehouseSalesFragment.RetrieveWarehouseSalesTask.class.getSimpleName(); 
     private String TAG_PID = "pid"; 
     public ProgressDialog pDialog; 
     private Context context; 
     //URL to get JSON details 
     private String url = "http://www.example.com/abc.php"; 
     ArrayList<HashMap<String,String>> sales_details; 
     List<WarehouseSalesDetails> data = new ArrayList<>(); 
     JSONObject jsonObj; 
     String jsonStr; 
     JSONArray sales; 

     //for recycler view 
     private RecyclerView warehouse_recycler; 
     private AdapterRecycler mAdapter; 


     public RetrieveWarehouseSalesTask(Context context){ 
      this.context = context; 
      sales_details = new ArrayList<>(); 

     } 



     @Override 
     protected void onPreExecute(){ 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(context); 
      pDialog.setMessage("Searching..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0){ 
      HttpHandler sh = new HttpHandler(); 
      //making a request to URL and getting response 
      jsonStr = sh.makeServiceCall(url); 
      Log.e(TAG, "Response from url (SearchActivity): " + jsonStr); 


      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result){ 
      //ArrayList<String> dataList = new ArrayList<String>(); 
      super.onPostExecute(result); 
      if(pDialog.isShowing()){ 
       pDialog.dismiss(); 
      } 
      if(jsonStr != null){ 
       try{ 
        jsonObj = new JSONObject(jsonStr); 
        //Getting JSON Array Node 
        sales = jsonObj.getJSONArray("Result"); 
        //looping through all results 
        for(int i = sales.length() - 1; i >= 0 ;i--){ 
         JSONObject s = sales.getJSONObject(i); 
         WarehouseSalesDetails wsd = new WarehouseSalesDetails(); 
         wsd.id = s.getString("id"); 
         wsd.company_name = s.getString("company_name"); 
         wsd.promotion_image= s.getString("promotion_image"); 
         wsd.title = s.getString("title"); 
         wsd.promotional_period = s.getString("promotional_period"); 
         wsd.viewCount = s.getString("view_count"); 
         data.add(wsd); 
        } 
        //strArrData = dataList.toArray(new String[dataList.size()]); 
        strArrData = data.toArray(new WarehouseSalesDetails[data.size()]); 
        Log.d("TAG",sales_details.toString()); 
       }catch(final JSONException e){ 
        Log.e(TAG, "JSON parsing error: " + e.getMessage()); 
       } 
      }else{ 
       Log.e(TAG,"Couldn't get json from server"); 
      } 
     } 

    } 
} 

當我寫的東西在搜索框它是正確過濾,但如果我點擊任何項目篩選後,始終打開上一個項目的值在同一位置。這些值始終是json值中的前幾個值。我應該如何解決這個問題?

回答

0

在這裏你去...

你的建議得到聽衆的立場是各自對各自的搜索查詢結果集。你不能使用相同的位置。

CursorAdapter ca = searchView.getSuggestionsAdapter(); 
Cursor cursor = ca.getCursor(); 
If (cursor.moveToPosition(position)) { 
String selectedID = cursor.getString(cursor.getColumnIndex("id"); 
searchView.setQuery(cursor.getString(cursor.getColumnIndex("title")),false); 
Log.d("strArr id",strArrData[position].id); 
Intent intent = new Intent(SearchActivity.this,RetrieveIndividualWarehouseSales.class); 
intent.putExtra("pid",selectedID); 
startActivity(intent); 
} 
+0

得到這個錯誤android.database.CursorIndexOutOfBoundsException:請求的列:-1#列:2 – kylas

+0

我的壞。這應該是標題不是ID。字符串selectedID = cursor.getString(cursor.getColumnIndex(「title」); – albeee

+0

selectedID是標題的值而不是id – kylas