【发布时间】:2018-06-14 20:15:39
【问题描述】:
我正在我的应用程序中配置资源以使用自定义适配器在列表视图中搜索。我在 addTextChangedListener 事件中使用 EditText 键入字符串并将文本传递给适配器,但它不起作用。
我使用“implements Filterable”配置适配器并使用“.setTextFilterEnabled(true)”启用Listview,但不起作用。
我看到我必须实现“public Filter getFilter()”,但我不知道该怎么做。
当我在 EditText 中键入一些单词时,例如“cel”或“12”,过滤器会起作用,但结果始终相同:Listview 中的前两项,无论 Listview 中的内容是什么(内容listview 是随机的)。
在我的 Fragment Activity 的 sn-p 下方:
public class VideosFragment extends Fragment {
private ListView listView;
private ArrayList<String> idVideo = new ArrayList<>();
private ArrayList<String> titleVideo = new ArrayList<>();
private ArrayList<String> descVideo = new ArrayList<>();
private ArrayList<String> urlVideo = new ArrayList<>();
private ArrayList<String> channelTitle = new ArrayList<>();
private ArrayList<String> canalTitle = new ArrayList<>();
private ArrayList<String> canalId = new ArrayList<>();
private CustomFiltraCanaisAdapter customFiltraCanaisAdapter;
private EditText editText;
CustomVideoAdapter customVideoAdapter;
public VideosFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_videos, container, false);
// Configure Listview
listView = (ListView)view.findViewById(R.id.listView_videos);
//Create search parameters
editText = (EditText) view.findViewById(R.id.searchList);
listView.setTextFilterEnabled(true);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = editText.getText().toString().toLowerCase(Locale.getDefault());
VideosFragment.this.customVideoAdapter.getFilter().filter(text);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
在我的 CustomAdapter 下方:
public class CustomVideoAdapter extends ArrayAdapter<String> implements Filterable {
private final Activity context;
private final ArrayList<String> videoTitle;
private final ArrayList<String> videoDesc;
private final ArrayList<String> videoId;
private final ArrayList<String> channelTitle;
private final ArrayList<String> imgurl;
public CustomVideoAdapter(Activity context, ArrayList<String> videoTitle,
ArrayList<String> videoId,
ArrayList<String> channelTitle,
ArrayList<String> imgurl,
ArrayList<String> videoDesc) {
super(context, R.layout.custom_lista_videos, videoTitle);
// TODO Auto-generated constructor stub
this.context = context;
this.videoTitle = videoTitle;
this.videoDesc = videoDesc;
this.videoId = videoId;
this.channelTitle = channelTitle;
this.imgurl = imgurl;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.custom_lista_videos, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtVideoTitle);
ImageView imgPhoto = (ImageView) rowView.findViewById(R.id.imgPhoto);
TextView txtChannelTitle = (TextView) rowView.findViewById(R.id.txtChannelTitle);
TextView txtVideoId = (TextView) rowView.findViewById(R.id.txtVideoId);
TextView txtVideoDesc = (TextView) rowView.findViewById(R.id.txtVideoDesc);
txtTitle.setText(videoTitle.get(position));
Picasso.with(context).load(imgurl.get(position)).into(imgPhoto);
txtChannelTitle.setText(channelTitle.get(position));
txtVideoId.setText(videoId.get(position));
txtVideoDesc.setText(videoDesc.get(position));
return rowView;
};
缺少什么?
经过一些帮助后,我尝试创建(和使用)一个模型类,但我仍然没有任何进展,因为我不知道如何必须更改“CustomAdapter”上的代码才能正确使用此代码。
我的模型类如下:
公共类 FilteredVideoAdapter {
private ArrayList<String> storedVideoTitle = null;
private ArrayList<String> storedVideoDesc = null;
private ArrayList<String> storedVideoId = null;
private ArrayList<String> storedChannelTitle = null;
private ArrayList<String> storedImgurl = null;
public FilteredVideoAdapter(){
}
public ArrayList<String> getStoredVideoTitle() {
return storedVideoTitle;
}
public void setStoredVideoTitle(ArrayList<String> storedVideoTitle) {
this.storedVideoTitle = storedVideoTitle;
}
public ArrayList<String> getStoredVideoDesc() {
return storedVideoDesc;
}
public void setStoredVideoDesc(ArrayList<String> storedVideoDesc) {
this.storedVideoDesc = storedVideoDesc;
}
public ArrayList<String> getStoredVideoId() {
return storedVideoId;
}
public void setStoredVideoId(ArrayList<String> storedVideoId) {
this.storedVideoId = storedVideoId;
}
public ArrayList<String> getStoredChannelTitle() {
return storedChannelTitle;
}
public void setStoredChannelTitle(ArrayList<String> storedChannelTitle) {
this.storedChannelTitle = storedChannelTitle;
}
public ArrayList<String> getStoredImgurl() {
return storedImgurl;
}
public void setStoredImgurl(ArrayList<String> storedImgurl) {
this.storedImgurl = storedImgurl;
}
public FilteredVideoAdapter withFilteredVideoAdapter(
ArrayList<String> storedVideoTitle,
ArrayList<String> storedVideoDesc,
ArrayList<String> storedVideoId,
ArrayList<String> storedChannelTitle,
ArrayList<String> storedImgurl){
this.storedVideoTitle = storedVideoTitle;
this.storedVideoDesc = storedVideoDesc;
this.storedVideoId = storedVideoId;
this.storedChannelTitle = storedChannelTitle;
this.storedImgurl = storedImgurl;
return this;
}
}
【问题讨论】:
-
您到底想在 listView 中搜索什么?是视频标题吗
-
@RahulChandrabhan,是的!但我需要在 VideoTitle 上进行过滤,但有关过滤视频的其他信息必须与之相关联。 lib4 发布的解决方案适用于 VideoTitle,但缺少 Listview 上的信息,如 VideoID、VideoDesc 等。
-
您必须为您的视频信息创建一个pojo,以便所有数据相互绑定并且易于使用,不要为每个信息创建单独的arraylist
标签: android listview android-fragments android-adapter android-filterable