gpt4 book ai didi

java - searchview.onQueryTextListener 之后得到错误的位置 recyclerview

转载 作者:行者123 更新时间:2023-12-02 11:38:37 25 4
gpt4 key购买 nike

Searchview 的结果正常,但是当我单击 Recycleview 时,它给出了错误的位置。例如:当我搜索 Padang 时,它显示“Padang”的结果,但是当我单击 Recyclerview 时,它会给我前一个位置 Recyclerview 的数据。

当我单击列表中的第三个 Recycleview 时,UI 返回原始列表中的第三个 Recycleview,而不是过滤列表中的第三个 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();
}
}

发布 Activity

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;
}

最佳答案

只需在适配器内的行后面添加一行代码..

 @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

对于more details see this answer

我希望它对你有用......祝你好运......

关于java - searchview.onQueryTextListener 之后得到错误的位置 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com