我試圖清除回收站中的搜索結果,當我開始從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;
}
在你的onclick中執行「recylerinfos = new ArrayList <>();」。 –
「它仍然不能解決我的問題」 - @ muditsen1234 – mahdi