在onbuttonclick之后清除recyclelerview结果

时间:2017-05-23 07:14:17

标签: java android android-recyclerview recycler-adapter

我有这个问题超过一周。当我开始从mysql数据库中搜索新单词时,我试图在recyclerview中清除我的搜索结果。问题是,当我搜索新单词时,它会将新的搜索结果添加到我以前的搜索结果中。我有一个Edittext和一个Image视图作为按钮和recyclerview

ActivitySearch

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.Button;
import android.widget.EditText;

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 = "";
    LinearLayoutManager manager;
    RecyclerView recyclenews;
    EditText txtsearch;

    Button btnsearch;

    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);
        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);
        btnsearch = (Button) findViewById(R.id.btnsearch);

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

        btnsearch.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String text = txtsearch.getText().toString();
                if (!txtsearch.getText().toString().equals("")) {
                    new AsyncTaskSearch("http://192.168.1.100/afgApp/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);
                        }
                        recyclenews.setAdapter(new RecycleNewsAdapter(recylerinfos));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }   }     }       });    }}


//RecyclerNewsAdapter
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://192.168.1.100/"+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();

            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

尝试在每次搜索开始时清除 g++ -DCPU_ONLY=1 test.cpp -o test -I../include -I../include/openblas -L../libs -Wl,-rpath=../libs -lcaffe -lglog -lboost_system -lopencv_core -lopencv_highgui -lopencv_imgproc

喜欢 -

recylerinfos

答案 1 :(得分:0)

感谢您的回答朋友。我通过向onclick()添加一些简单代码解决了我的问题。我只需清除ArrayList中的所有Onclick即可解决。

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