尝试使用EditText过滤Listview,但是每次我在EditText上输入文本时,我的listview都将变为空白,并且不返回任何结果。我已经尝试了许多方法,而这似乎是最简单的实施方法。任何帮助将不胜感激!
在我的适配器上
public class AdapterTrilhos extends BaseAdapter {
private final Activity activity;
private LayoutInflater inflater;
private List<Trilhos> triList;
private ArrayList<Trilhos> arraylist=null;
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;
this.arraylist = new ArrayList<Trilhos>();
this.arraylist.addAll(triList);
}
@Override
public int getCount() {
return this.triList.size();
}
@Override
public Object getItem(int position) {
return this.triList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.dadoslista, null);
TextView idtxt = (TextView) convertView.findViewById(R.id.txtid);
TextView tittxt = (TextView) convertView.findViewById(R.id.txttit);
// TextView usertxt = convertView.findViewById(R.id.txtuser);
TextView diftxt = (TextView) convertView.findViewById(R.id.txtdif);
RatingBar ratess= (RatingBar) convertView.findViewById(R.id.ratings);
Trilhos t = triList.get(position);
idtxt.setText(String.valueOf(t.getId()));
tittxt.setText(t.getTitulo());
// usertxt.setText(String.valueOf(t.getId_user()));
diftxt.setText(t.getDificuldade());
ratess.setRating(t.getMedia());
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
triList.clear();
if (charText.length() == 0) {
triList.addAll(arraylist);
} else {
for (Trilhos cs : arraylist) {
if (cs.getTitulo().toLowerCase(Locale.getDefault()).contains(charText)) {
triList.add(cs);
}
}
}
notifyDataSetChanged();
}
} 以及我的活动
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
// TODO Auto-generated method stub
int textlength = cs.length();
String text = search.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
我打算做的是,当我在editText上输入文字时,它只会返回带有输入文字的结果
答案 0 :(得分:2)
我认为您应该直接在活动中过滤列表,然后交换到新适配器:
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
int textlength = cs.length();
String text = search.getText().toString().toLowerCase(Locale.getDefault());
List<Trilhos> filteredList = yourFilterMethod(oldList, text);
mListView.setAdapter(new AdapterTrilhos(filteredList, this));
}
});
private List<Trilhos> yourFilterMethod(List<object> list, String text)
{
// some stuff that filter your list using text and returns the filtered one
return filteredList;
}
答案 1 :(得分:1)
尝试这个?
struct Example {
var partOne: Int
var partTwo: Int
var partThree: Int
}
var one = Example(partOne: 10, partTwo: 11, partThree: 12)
var two = Example(partOne: 10, partTwo: 11, partThree: 12)
var arrayOfExamples = [one, two]
for i in 0...arrayOfExamples[0].partThree {
print(i)
}
//once i = 12, then
for i in 0...arrayOfExamples[1].partThree {
print(i)
}