Searchview的结果没问题,但是当我点击Recycleview时它会给出错误的位置。例如:当我搜索Padang时,它会显示" Padang"但当我点击Recyclerview时,它会给我以前位置Recyclerview的数据。
当我单击列表中的第三个Recycleview时,UI将返回原始List中的第三个Recycleview,而不是来自已过滤List的第三个Recycleview。我在这里缺少什么?
RecyclerViewAdapter
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.android.perpustakaanbpspadang.R;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
public Context context;
ArrayList<GetDataAdapter> getDataAdapter = new ArrayList<GetDataAdapter>();
ImageLoader imageLoader1;
DownloadManager downloadManager;
public RecyclerViewAdapter(ArrayList<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_buku, parent, false);
ViewHolder viewHolder = new ViewHolder(v, context, getDataAdapter);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
final GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.bps,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
Viewholder.Rilis.setText(getDataAdapter1.getRilis());
Viewholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(context, Viewholder.buttonViewOption);
popup.inflate(R.menu.buku_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
if (isConnectingToInternet()) {
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(getDataAdapter1.getDownload());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getDataAdapter1.getImageTitleName() + ".pdf");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}
else {
Toast.makeText(context, "Oops!! There is no internet connection. " +
"Please enable internet connection and try again.", Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
});
popup.show();
}
});
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView ImageTitleNameView;
public NetworkImageView networkImageView ;
public TextView buttonViewOption;
public TextView Rilis;
ArrayList<GetDataAdapter> getDataAdapter = new ArrayList<GetDataAdapter>();
Context context;
public ViewHolder(View itemView, Context context, ArrayList<GetDataAdapter> getDataAdapter) {
super(itemView);
this.getDataAdapter = getDataAdapter;
this.context = context;
itemView.setOnClickListener(this);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
Rilis = (TextView) itemView.findViewById(R.id.textView_rilis);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
GetDataAdapter perpus = this.getDataAdapter.get(position);
final String image = this.getDataAdapter.get(position).getImageServerUrl();
Intent intent = new Intent(this.context, DetailActivity.class);
intent.putExtra("id", perpus.getIdBuku());
intent.putExtra("judul", perpus.getImageTitleName());
intent.putExtra("no_publikasi", perpus.getNomorPublikasi());
intent.putExtra("no_katalog", perpus.getNomorKatalog());
intent.putExtra("isbn", perpus.getISBN());
intent.putExtra("deskripsi", perpus.getDeskripsi());
intent.putExtra("file", perpus.getDownload());
intent.putExtra("rilis", perpus.getRilis());
intent.putExtra("kategori_id", perpus.getKategori());
this.context.startActivity(intent);
}
}
//Check if internet is present or not
private boolean isConnectingToInternet() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
public void setFilter (ArrayList<GetDataAdapter> newList){
getDataAdapter = new ArrayList<GetDataAdapter>();
getDataAdapter.clear();
getDataAdapter.addAll(newList);
notifyDataSetChanged();
}
}
Publikasi活动
public class PublikasiActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, NavigationView.OnNavigationItemSelectedListener {
ArrayList<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
public String ImageServerUrl;
public String ImageTitleName;
RecyclerView.LayoutManager linearViewLayoutManager;
RecyclerViewAdapter recyclerViewadapter;
private ProgressWheel progressWheel;
public static String urlPub = url+"publikasi";
String GET_JSON_DATA_HTTP_URL = urlPub;
String JSON_ID = "id";
String JSON_IMAGE_TITLE_NAME = "judul";
String JSON_IMAGE_URL = "cover";
String JSON_NOMOR_PUBLIKASI = "no_publikasi";
String JSON_NOMOR_KATALOG = "no_katalog";
String JSON_ISBN = "isbn";
String JSON_DESKRIPSI = "deskripsi";
String JSON_DOWNLOAD = "file";
String JSON_RILIS = "rilis";
String JSON_KATEGORI = "kategori_id";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_berita);
getIntent();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Publikasi");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_berita);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_berita);
navigationView.setNavigationItemSelectedListener(this);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview_berita);
recyclerView.setHasFixedSize(true);
linearViewLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearViewLayoutManager);
progressWheel = (ProgressWheel) findViewById(R.id.progressWheelBerita);
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
progressWheel.setVisibility(View.VISIBLE);
JSON_DATA_WEB_CALL();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.option_menu, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<GetDataAdapter> newList = new ArrayList<>();
for (GetDataAdapter getBeritaModel : GetDataAdapter1){
String name = getBeritaModel.getImageTitleName().toLowerCase();
if (name.contains(newText))
newList.add(getBeritaModel);
}
recyclerViewadapter.setFilter(newList);
return true;
}
答案 0 :(得分:1)
只需在行后面的适配器中添加一行代码..
@Override
public void onBindViewHolder(final ViewHolder Viewholder, final int position)
喜欢吼叫
@Override
public void onBindViewHolder(final ViewHolder Viewholder, final int position)
//add this line of code
Viewholder.setIsRecyclable(false);
//......your rest of the code will same
for more details see this answer
我希望它对你有用..祝你好运..
答案 1 :(得分:0)
mySearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Maintain new arraylist for searchview and pass it to adapter.
ArrayList<Info> searchList = new ArrayList<Info>();
String search = mySearchView.getQuery().toString().toLowerCase().trim();
for (int i = 0; i < myInfoArrayList.size(); i++) {
String name = myInfoArrayList.get(i).getMyFullName();
if (name.toLowerCase().contains(search)) {
Info info = myInfoArrayList.get(i);
searchList.add(info);
}
}
if (searchList.size() > 0) {
myAdapter = new Adapter(this, searchLis);
myRecycler.setAdapter(myAdapter);
}
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
myAdapter = new Adapter(this, myArrayList);
myRecyclerView.setAdapter(myAdapter);
return true;
}
});