2017-05-23 17 views
0

私はこの問題が1週間以上続いています。私はmysqlデータベースから新しい単語の検索を開始すると、recyclerviewで検索結果をクリアしようとしています。問題は、私が新しい単語を検索するときですが、以前の検索単語に新しい検索結果を追加することです。私は1つのEdittextImageビューをボタンとして、recyclerviewとしています。onbuttonclickの後に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(); 

      } 
     }); 

    } 
} 
+0

あなたのAsyncTaskSearchコード – AlexTa

+2

は、あなたのArrayListと 'adapter.notifyDataSetChanged()'をクリアしてください。 –

答えて

0

のように、各検索

の開始時に、あなたのrecylerinfosをクリアしてみ -

new AsyncTaskSearch("http://192.168.1.100/afgApp/search.php", text).execute(); 
     recyclerinfos.clear(); 
        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); 
0

あなたの答えの友人をありがとう。私はonclick()に簡単なコードを追加して私の問題を解決しました。私はすべてArrayListOnclickにクリアして解決しました。

recycleimg.clear(); 
       recycleId.clear(); 
       recycleTitle.clear(); 
       recylerinfos.clear(); 
関連する問題