2017-03-28 73 views
-1

我開發了一個搜索功能,並且它已成功運行。負責搜索的類從onQueryTextSubmit中調用,並且沒有任何問題。將數據從數據庫顯示到cardview列表onclick按鈕

現在我想在搜索欄的同一個活動中添加其他的東西,當它被點擊時,數據庫中的所有數據都顯示在cardView中。當我添加代碼時,onQueryTextSubmit方法不再有效,並且按鈕不顯示數據。我不知道問題在哪裏。這是整個活動的代碼。 PS:由於某種原因,它說從未使用過showdata()方法。

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

     button = (Button) findViewById(R.id.button); 
     recyclerView = (RecyclerView) findViewById(R.id.recyclerViewer); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 


     listView = (ListView) findViewById(R.id.searchList); 
     searchView = (SearchView) findViewById(R.id.searchView); 
     noData = (ImageView) findViewById(R.id.nodata); 
     noNetwork = (ImageView) findViewById(R.id.nonetwork); 
     urlAdress = "http://skillsexchangecyprus.com/SEC/ss.php"; 


     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       progressBar = (ProgressBar) findViewById(R.id.progressBar); 
       progressBar.setVisibility(View.VISIBLE); 
       getData(); 
      } 
     }); 


     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String query) { 
       SenderReceiver sr = new SenderReceiver(FindSkill.this, urlAdress,listView, query,noData,noNetwork); 
       sr.execute(); 
       return false; 
      } 
      @Override 
      public boolean onQueryTextChange(String query) { 
       return false; 
      }});} 

      private void getData() { 
       class GetData extends AsyncTask<Void, Void, String> { 
        ProgressDialog progressDialog; 

        @Override 
        protected void onPreExecute() { 
         super.onPreExecute(); 
         progressDialog = ProgressDialog.show(FindSkill.this, "Fetching Data", "Please wait...", false, false); 
        } 

        @Override 
        protected void onPostExecute(String res) { 
         super.onPostExecute(res); 
         progressDialog.dismiss(); 
         parseJSON(res); 

        } 

        @Override 
        protected String doInBackground(Void... params) { 
         BufferedReader bufferedReader = null; 
         try { 
          URL url = new URL(Config.GET_URL); 
          HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
          StringBuilder sb = new StringBuilder(); 
          bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
          String json; 
          while ((json = bufferedReader.readLine()) != null) { 
           sb.append(json + "\n"); 
          } 
          return sb.toString().trim(); 
         } catch (Exception e) { 
          return null; 
         } 
        } 
       } 
       GetData gd = new GetData(); 
       gd.execute(); 
      } 
    public void showData(){ 
     adapter = new CardAdapter(Config.skills,Config.ids); 
     recyclerView.setAdapter(adapter); 
    } 


      private void parseJSON(String json) { 
       try { 
        JSONObject jsonObject = new JSONObject(json); 
        JSONArray array = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

        config = new Config(array.length()); 

        for (int i = 0; i < array.length(); i++) { 
         JSONObject j = array.getJSONObject(i); 
         Config.skills[i] = getSkill(j); 
         Config.ids[i] = getId(j); 
        } 

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

      private String getSkill(JSONObject j){ 
       String name = null; 
       try { 
        name = j.getString(Config.JSON_NAME); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       return name; 
      } 

      private String getId(JSONObject j){ 
       String id = null; 
       try { 
        id = j.getString(Config.JSON_ID); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       return id; 
      } 

這是配置類:

public class Config { 

     public static String[] skills; 
     public static String[] ids; 


     public static final String GET_URL = "http://skillsexchangecyprus.com/SEC/mainList.php"; 
     public static final String JSON_ID = "id"; 
     public static final String JSON_NAME = "skill"; 
     public static final String TAG_JSON_ARRAY="result"; 

     public Config(int i) { 
      skills = new String[i]; 
      ids = new String[i]; 
     } 

     } 

卡適配器類:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> { 

    List<ListItem> items; 

    public CardAdapter(String[] skills, String[] ids){ 
     super(); 
     items = new ArrayList<>(); 
     for(int i =0; i<items.size(); i++){ 
      ListItem item = new ListItem(); 
      item.setSkill(skills[i]); 
      item.setId(ids[i]); 
      items.add(item); 
     } 
    } 


    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.list_item, parent, false); 
     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     ListItem myAdapter = items.get(position); 
     holder.skillName.setText(myAdapter.getSkill()); 
     holder.skillId.setText(String.valueOf(myAdapter.getId())); 
    } 

    @Override 
    public int getItemCount() { 
     return items.size(); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder{ 
     public TextView skillId; 
     public TextView skillName; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      skillId = (TextView) itemView.findViewById(R.id.skillId); 
      skillName = (TextView) itemView.findViewById(R.id.skillName); 

     } 
    } 
} 
+0

您可以發佈完整的活動代碼嗎? – FAT

+0

這是活動的整個代碼。如果你的意思是其他類只是提到哪一個,所以我不會在問題中發表任何代碼。 – Jou

回答

0
  1. 你我以前不打電話給你showData()方法。您需要創建CardAdapter的實例並將此適配器設置爲您的recyclerView

更新OnCreate()方法如下:

.............. 
    .................... 
    urlAdress = "http://skillsexchangecyprus.com/SEC/ss.php"; 

    config = new Config(INITIAL_VALUE); 

    // Adapter 
    adapter = new CardAdapter(config.skills,config.ids); 
    recyclerView.setAdapter(adapter); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      progressBar = (ProgressBar) findViewById(R.id.progressBar); 
      progressBar.setVisibility(View.VISIBLE); 
      getData(); 
     } 
    }); 
    .................... 
    .............................. 
  • 使用adapter.notifyDataSetChanged()更新recyclerView與新的項目。
  • 更新parseJson()方法如下:

    private void parseJSON(String json) { 
    
        try { 
         JSONObject jsonObject = new JSONObject(json); 
         JSONArray array = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 
    
         config = new Config(array.length()); 
    
         for (int i = 0; i < array.length(); i++) { 
          JSONObject j = array.getJSONObject(i); 
          config.skills[i] = getSkill(j); 
          config.ids[i] = getId(j); 
    
          // Update 
          adapter.notifyDataSetChanged(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
    } 
    
  • 在你CardAdapter構造,使用skills.length代替items.size()

    public CardAdapter(String[] skills, String[] ids){ 
        super(); 
        items = new ArrayList<>(); 
    
        for(int i = 0; i < skills.length; i++){ 
         ListItem item = new ListItem(); 
         item.setSkill(skills[i]); 
         item.setId(ids[i]); 
         items.add(item); 
        } 
    } 
    
  • 希望這會起作用〜

    +0

    謝謝你的幫助。我必須在哪裏調用show方法? – Jou

    +0

    @Jou:不需要調用showData()。我已經在parseJson()方法中添加了該代碼部分。參見>> adapter = new CardAdapter(config.skills,config.ids); recyclerView.setAdapter(adapter);只需更新您的parseJson()方法,正如我在我的答案中所述。 – FAT

    +0

    仍然無法正常工作。此錯誤顯示:沒有附加適配器;跳過佈局 – Jou