ArrayList.size()每次都显示为零。实际上我试图使用存储在ArrayList上的数据来过滤数据
这是我的HomeFragment.Java
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String TAG_check="check";
public RecyclerView recyclerView;
SearchView realsv;
private AlbumsAdapter adapter;
private ArrayList<Album> albumList;
private ArrayList<Album> test;
private Album album;
public ImageView imga;
public CollapsingToolbarLayout collapsingToolbar;
public AppBarLayout appBarLayout;
ProgressDialog pd;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
realsv=(SearchView)rootView.findViewById(R.id.realsv);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rcv);
//test.addAll(albumList);
//test.clear();
realsv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i("YES","FIRST STEP"+albumList.size());
newText=newText.toLowerCase();
for(Album al: albumList)
{
String title = al.getName().toLowerCase();
ArrayList<Album> test = new ArrayList<>();
Log.i("CHEK",title);
if(title.contains(newText)){
Log.i("CHEK ok",title+"ok");
test.add(al);
}
}
adapter.setFilter(test);
// adapter.getFilter().filter(newText);
return true;
}
});
initViews();
return rootView;
}
private void initViews(){
pd = new ProgressDialog(getActivity());
pd.setMessage("Fetching Data...");
pd.setCancelable(false);
pd.show();
albumList = new ArrayList<>();
adapter = new AlbumsAdapter(getActivity(), albumList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
loadJSON();
}
private void loadJSON(){
try{
Client Client = new Client();
//this is where it all changes
Hindi apiService = Client.getClient().create(Hindi.class);
Call<AlbumsResponse> call = apiService.getAlbums();
call.enqueue(new Callback<AlbumsResponse>() {
@Override
public void onResponse(Call<AlbumsResponse> call, Response<AlbumsResponse> response) {
ArrayList<Album> items = response.body().getAlbums();
recyclerView.setAdapter(new AlbumsAdapter(getActivity(), items));
recyclerView.smoothScrollToPosition(0);
pd.hide();
}
@Override
public void onFailure(Call<AlbumsResponse> call, Throwable t) {
Log.d("Error", t.getMessage());
// Toast.makeText(MainActivity.this, "Error Fetching Data!", Toast.LENGTH_SHORT).show();
pd.hide();
}
});
}catch (Exception e){
Log.d("Error", e.getMessage());
// Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
这是我的适用于Recyclerview的适配器类
public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder>{
private Context mContext;
private ArrayList<Album> albumList;
//private List<Album> newList;
public AlbumsAdapter(Context mContext, ArrayList<Album> albuList) {
this.mContext = mContext;
this.albumList = albuList;
// this.newList=albuList;
}
@Override
public AlbumsAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.album_card, viewGroup, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final AlbumsAdapter.MyViewHolder viewHolder, int i) {
viewHolder.title.setText(albumList.get(i).getName());
viewHolder.count.setText(albumList.get(i).getLang());
//load album cover using picasso
Picasso.with(mContext)
.load(albumList.get(i).getThumbnail())
.placeholder(R.drawable.load)
.into(viewHolder.thumbnail);
}
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
public void setFilter(ArrayList<Album> nn){
albumList= new ArrayList<Album>();
for (Album al:nn){
albumList.add(al);
}
notifyDataSetChanged();
}
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
public MyMenuItemClickListener() {
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_add_favourite:
Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_play_next:
Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
}
@Override
public int getItemCount() {
return albumList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail, overflow;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
// overflow = (ImageView) view.findViewById(R.id.overflow);
//on item click
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION){
Album clickedDataItem = albumList.get(pos);
Intent intent = new Intent(mContext, Det.class);
intent.putExtra("name", albumList.get(pos).getName());
intent.putExtra("lang", albumList.get(pos).getLang());
intent.putExtra("thumbnail", albumList.get(pos).getThumbnail());
intent.putExtra("scrn",albumList.get(pos).getScrn());
intent.putExtra("ourl",albumList.get(pos).getOurl());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
// Toast.makeText(v.getContext(), "You clicked " + clickedDataItem.getName(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
`
这是Model Class Album.java
public class Album {
@SerializedName("name")
@Expose
private String name;
@SerializedName("lang")
@Expose
private String lang;
@SerializedName("thumbnail")
@Expose
private String thumbnail;
@SerializedName("scrn")
@Expose
private String scrn;
@SerializedName("ourl")
@Expose
private String ourl;
public Album() {
}
public Album(String name, String lang, String thumbnail, String scrn,
String ourl) {
this.name = name;
this.lang = lang;
this.thumbnail = thumbnail;
this.scrn=scrn;
this.ourl=ourl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getScrn() {
return scrn;
}
public void setScrn(String scrn) {
this.scrn = scrn;
}
public String getOurl() {
return ourl;
}
public void setOurl(String ourl) {
this.ourl = ourl;
}
}
这是AlbumResponse.java
public class AlbumsResponse {
@SerializedName("Album")
@Expose
private ArrayList<Album> albums;
public ArrayList<Album> getAlbums(){
return albums;
}
public void setAlbums(ArrayList<Album>albums){
this.albums = albums;
}
}
答案 0 :(得分:0)
在loadJson()方法中,在onResponse中,您正在创建一个新的ArrayList来获取所有专辑,而不是将其设置为全局变量albumList。 所以你应该这样做,
ArrayList<Album> items = response.body().getAlbums();
albumList.addAll(items);
答案 1 :(得分:0)
好的,在你的LoadJson方法中,你填充一个名为items的新数组,然后用它填充你的适配器。所以在你的适配器中你的albumList被填充,这很好。但是在你的OnQueryTextChanged方法中,你检查你在home片段中声明但从未初始化的albumList数组中的查询字符串。因此没有过滤结果。将您的项目分配到相册列表,它将起作用