2017-05-21 62 views
0

我試圖清除回收站中的搜索結果,當我開始從mysql數據庫搜索新單詞時。問題是,當我搜索新單詞時,它會將新搜索結果添加到我以前的搜索那些。 我有一個EDITTEXT和工作方式按鈕和recyclerview清除回收站視圖中的搜索結果

//Activity_search.java 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 

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

import java.util.ArrayList; 

public class Activity_Search extends AppCompatActivity { 
    public static String data = ""; 

    RecycleNewsAdapter recycleNewsAdapter; 

    LinearLayoutManager manager; 
    RecyclerView recyclenews; 

    ImageView imgsearch; 
    EditText txtsearch; 

    ArrayList<recycleinfo> recylerinfos; 
    ArrayList<String>recycleTitle ; 
    ArrayList<String> recycleimg; 
    ArrayList<String> recycleId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity__search); 
     // Toast.makeText(Activity_AfNews.this,afgnews, Toast.LENGTH_SHORT).show(); 

     recycleNewsAdapter = new RecycleNewsAdapter(recylerinfos); 
     recycleimg = new ArrayList<>(); 
     recycleTitle = new ArrayList<>(); 
     recycleId = new ArrayList<>(); 
     recylerinfos = new ArrayList<>(); 

     recyclenews = (RecyclerView) findViewById(R.id.searchRecycle); 
     manager = new LinearLayoutManager(this); 
     recyclenews.setHasFixedSize(true); 
     recyclenews.setLayoutManager(manager); 
     recyclenews.setNestedScrollingEnabled(false); 

     imgsearch = (ImageView) findViewById(R.id.imgsearch); 
     txtsearch = (EditText) findViewById(R.id.txtsearch); 

     imgsearch.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String text = txtsearch.getText().toString(); 
       if (!txtsearch.getText().toString().equals("")){ 
       new AsyncTaskSearch("http://192.198.1.100/search.php",text).execute(); 
       try { 
        JSONObject jsonObject=new JSONObject(data); 
        JSONArray jsonTitleaf=jsonObject.getJSONArray("title"); 
         JSONArray jsonPicaf=jsonObject.getJSONArray("pics"); 
         JSONArray jsonId=jsonObject.getJSONArray("id"); 

          for (int i=0;i<jsonTitleaf.length();i++){ 
         recycleimg.add(jsonPicaf.getString(i)); 
         recycleTitle.add(jsonTitleaf.getString(i)); 
         recycleId.add(jsonId.getString(i)); 
        } 
        for (int i=0;i<recycleTitle.size();i++){ 
         recycleinfo recycleinfo = new recycleinfo(); 
         recycleinfo.title = recycleTitle.get(i); 
         recycleinfo.img = recycleimg.get(i); 
         recycleinfo.Id = recycleId.get(i); 
         recylerinfos.add(recycleinfo); 
        }recycleNewsAdapter.notifyDataSetChanged(); 
        recyclenews.setAdapter(new RecycleNewsAdapter(recylerinfos)); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

      } 
     }); 
    } 
} 




//RecyclerNewsAdoptor.java 

public class RecycleNewsAdapter extends RecyclerView.Adapter<viewholder>{ 

    ArrayList<recycleinfo> recycleinfos = new ArrayList<>(); 
    public RecycleNewsAdapter(ArrayList<recycleinfo> recycleinfos){ 
     this.recycleinfos=recycleinfos; 
    } 
    @Override 
    public viewholder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclernews, parent, false); 
     return new viewholder(view); 
    } 
    @Override 
    public void onBindViewHolder(viewholder holder, int position) { 
     recycleinfo recycleinfo = recycleinfos.get(position); 
     holder.title.setText(recycleinfo.title); 
     holder.Id = recycleinfo.Id; 
     Picasso.with(G.context).load("http://afghanistanema.com/"+recycleinfo.img).into(holder.img); 
    // Picasso.with(G.context).load("http://afghanistanema.com/"+recycleinfo.img).into(holder.img); 
    } 
    @Override 
    public int getItemCount() { 
     return recycleinfos.size(); 
    } 
} 

class viewholder extends RecyclerView.ViewHolder{ 

    public ImageView img; 
    public TextView title; 
    public TextView date; 
    public String Id=""; 
    public LinearLayout linearLayout; 
    public viewholder(View itemView) { 
     super(itemView); 
     img = (ImageView) itemView.findViewById(R.id.img); 
     date = (TextView) itemView.findViewById(R.id.date); 
     title = (TextView) itemView.findViewById(R.id.title); 
     linearLayout = (LinearLayout) itemView.findViewById(R.id.linearNews); 
     linearLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(G.context, ActivityWait_full_Text.class); 
       intent.putExtra("id", Id); 
       intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK); 
       G.context.startActivity(intent); 
    // Toast.makeText(G.context,Id+"", Toast.LENGTH_SHORT).show(); 

      } 
     }); 
    } 
} 




//recycleinfo.java 


public class recycleinfo { 
    public String img; 
    public String title; 
    public String Id; 
    public String date; 


} 
+0

在你的onclick中執行「recylerinfos = new ArrayList <>();」。 –

+0

「它仍然不能解決我的問題」 - @ muditsen1234 – mahdi

回答

0

謝謝您的回答friends.i可以解決我的問題,在ONCLICK添加一些簡單的代碼到我的onclick.i只是清除所有我的ArrayList和它解決。

    recycleimg.clear(); 
       recycleId.clear(); 
       recycleTitle.clear(); 
       recylerinfos.clear(); 
0

的形象圖當用戶點擊搜索按鈕即可clear()ArrayList並刷新RecyclerAdapter。像這樣:

recycleinfo.clear(); 
recycleNewsAdapter.notifyDataSetChanged(); 

重要的是,你的你清除ArrayList而不是創建一個新的一個,因爲如果你創建一個新RecyclerAdapter參考ArrayList走了..

+0

我不undrestand在哪裏是我的問題。我做你所說的@Keivan Esbati – mahdi

0

添加recylerinfos.clear ();如下所示。

imgsearch.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      recylerinfos.clear();  // adding this line will clear the old search results. 
      String text = txtsearch.getText().toString(); 
      if (!txtsearch.getText().toString().equals("")){ 
      new AsyncTaskSearch("http://192.198.1.100/search.php",text).execute(); 
      try { 
       JSONObject jsonObject=new JSONObject(data); 
       JSONArray jsonTitleaf=jsonObject.getJSONArray("title"); 
        JSONArray jsonPicaf=jsonObject.getJSONArray("pics"); 
        JSONArray jsonId=jsonObject.getJSONArray("id"); 

         for (int i=0;i<jsonTitleaf.length();i++){ 
        recycleimg.add(jsonPicaf.getString(i)); 
        recycleTitle.add(jsonTitleaf.getString(i)); 
        recycleId.add(jsonId.getString(i)); 
       } 
       for (int i=0;i<recycleTitle.size();i++){ 
        recycleinfo recycleinfo = new recycleinfo(); 
        recycleinfo.title = recycleTitle.get(i); 
        recycleinfo.img = recycleimg.get(i); 
        recycleinfo.Id = recycleId.get(i); 
        recylerinfos.add(recycleinfo); 
       }recycleNewsAdapter.notifyDataSetChanged(); 
       recyclenews.setAdapter(new RecycleNewsAdapter(recylerinfos)); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     } 
    }); 
} 
+0

它不工作yet.i添加兩個recycleinfos.clear(); recycleNewsAdapter.notifyDataSetChanged();在我的onclick,但沒有任何變化,但你還沒有改變 – mahdi

+0

你可以嘗試添加recyclenews.setAdapter(null);在onclick事件之後的那些行之後。 –

+0

還沒有工作@RohitPadma – mahdi