我正在研究一个Android项目,当我搜索某些东西时,我在活动中有一个搜索字段。当我刷新更新的列表但是列表视图显示以前的数据时,我通过包发送搜索关键字到片段。我使用了所有可能的解决方案,但结果仍然显示相同的结果。我陷入了我的代码中。但是当列表查看和编辑同一活动的文本时,它工作正常。当我将关键字从活动过滤到片段然后更新列表时,会出现问题。
这是我的适配器类。
package com.Celeritas.JSBank.Adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.telephony.VisualVoicemailService;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import com.Celeritas.JSBank.ListViewModels.OnlineModel;
import com.Celeritas.JSBank.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Created by anas on 10/27/2017.
*/
public class OnlineCourseAvailableAdapter extends ArrayAdapter<OnlineModel> {
List<OnlineModel> value;
List<OnlineModel> arraylist;
private ViewHolder holder;
public OnlineCourseAvailableAdapter(Context context, int resource,List<OnlineModel> value) {
super(context, resource, value);
this.value = value;
arraylist = new ArrayList<OnlineModel>();
arraylist.addAll(value);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.online_course_available_row, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_day.setText(value.get(position).getDay());
holder.tv_date.setText(value.get(position).getMonthyear());
holder.tv_title.setText(value.get(position).getTitle_tv());
holder.tv_completed.setText(value.get(position).getCompleted_onOtv());
holder.target.setText(value.get(position).getTargetAudience());
return convertView;
}
public List<OnlineModel> filter1(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
value.clear();
if (charText.length() == 0) {
value.addAll(arraylist);
} else {
for (OnlineModel onlineModel : arraylist) {
if (charText.length() != 0 && onlineModel.getTitle_tv().toLowerCase(Locale.getDefault()).contains(charText)) {
value.add(onlineModel);
}
}
}
notifyDataSetChanged();
return value;
}
public class ViewHolder{
TextView tv_day;
TextView tv_date;
TextView tv_title;
TextView tv_completed;
TextView target;
ImageView arrow;
public ViewHolder(View view) {
tv_day = view.findViewById(R.id.date_day);
tv_date = view.findViewById(R.id.date_monthyear);
tv_title = view.findViewById(R.id.title_tv);
tv_completed = view.findViewById(R.id.completed_on_tv);
target = view.findViewById(R.id.target);
arrow = view.findViewById(R.id.arrow);
}
}
}
这里我正在更新适配器
package com.Celeritas.JSBank.Fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.Celeritas.JSBank.Activities.CourseDiscription;
import com.Celeritas.JSBank.Adapter.OnlineCourseAvailableAdapter;
import com.Celeritas.JSBank.ListViewModels.OnlineModel;
import com.Celeritas.JSBank.Models.DesignationModel;
import com.Celeritas.JSBank.Models.OnlineCourseModel;
import com.Celeritas.JSBank.Models.TeachersModel;
import com.Celeritas.JSBank.R;
import com.Celeritas.JSBank.Storage.Data;
import java.util.ArrayList;
import java.util.List;
public class OnlineAvailableFragment extends Fragment {
private List<OnlineModel> list = new ArrayList<>();
private OnlineCourseAvailableAdapter adapter;
private ListView lv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_online_available, container, false);
lv = view.findViewById(R.id.listview_available);
LoadData(view);
/* if (Data.keyword.length() !=0){
adapter = new OnlineCourseAvailableAdapter(getActivity(),
R.layout.fragment_online_available, list);
adapter.filter("Kyc");
lv.setAdapter(adapter);
lv.invalidateViews();
}*/
return view;
}
private void LoadData(View view) {
final List<OnlineCourseModel> tem = new ArrayList<>();
for (int i= 0; i< Data.onlineCourseModelList.size();i++){
String startDate = Data.onlineCourseModelList.get(i).getStartdate();
String[] parts = startDate.split("-", 0);
String date = parts[0];
String month = parts[1];
String year = parts[2];
if (Data.onlineCourseModelList.get(i).getKey().equalsIgnoreCase("upcoming")){
String TargetAudienc = convertToCommaDelimited(Data.onlineCourseModelList.get(i).getDesignationModels());
if (TargetAudienc.length() == 0){
TargetAudienc = "ALL";
}
list.add(new OnlineModel(date, month+"-"+year, Data.onlineCourseModelList.get(i).getFullname(),
"End on: "+Data.onlineCourseModelList.get(i).getEnddate(),"Target Audience: "+ TargetAudienc));
tem.add(new OnlineCourseModel(Data.onlineCourseModelList.get(i).getFullname(),Data.onlineCourseModelList.get(i).getShortname(),
Data.onlineCourseModelList.get(i).getSummary(),Data.onlineCourseModelList.get(i).getStartdate(),Data.onlineCourseModelList.get(i).getEnddate(),
Data.onlineCourseModelList.get(i).getScormlink(),Data.onlineCourseModelList.get(i).getKey(),Data.onlineCourseModelList.get(i).getTrainingarea(),
Data.onlineCourseModelList.get(i).getCategoryid(),Data.onlineCourseModelList.get(i).getEnrolldate(),Data.onlineCourseModelList.get(i).getCompletiondate(),
Data.onlineCourseModelList.get(i).getCourseid(),Data.onlineCourseModelList.get(i).getGradeModelses(),Data.onlineCourseModelList.get(i).getDesignationModels()));
}
}
adapter = new OnlineCourseAvailableAdapter(getActivity(),
R.layout.fragment_online_available, list);
lv.setAdapter(adapter);
Bundle bundle = getArguments();
if(bundle!=null) {
String keyword = getArguments().getString("keyword");
if (keyword != null && !keyword.equalsIgnoreCase("")) {
adapter.filter1("Kyc");
lv.setAdapter(adapter);
lv.invalidateViews();
}
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), CourseDiscription.class);
Data.StartDate = tem.get(i).getStartdate();
Data.EndDate = tem.get(i).getEnddate();
Data.TargetAudeince = tem.get(i).getDesignationModels();
Data.Course = tem.get(i).getFullname();
Data.Category = tem.get(i).getCategoryid();
Data.CourseId = tem.get(i).getCourseid();
Data.Discription = Data.html2text(tem.get(i).getSummary());
Data.ScormLink = tem.get(i).getScormlink();
Data.Key = tem.get(i).getKey();
startActivity(intent);
}
});
}
public static OnlineAvailableFragment newInstance() {
return new OnlineAvailableFragment();
}
}