gpt4 book ai didi

Java android listView 搜索

转载 作者:行者123 更新时间:2023-11-30 00:50:07 25 4
gpt4 key购买 nike

您好,我想在我的 ListView 中搜索,我写了一个适配器,在 Activity 中我有一个 listView 和 EditText。当我删除搜索时,一切都正常工作:这是我的适配器:

 public class NewDataAdapter extends ArrayAdapter<DataRow> {

private Context context;
private ArrayList<DataRow> rows;
private LayoutInflater inflater;
private Page page;
private boolean[] sList;
private String[] columnNames;
public static String objectId;
public static String lineId;
private List<DataRow> searchList = null;

public NewDataAdapter(Context context, ArrayList<DataRow> rows, Page page) {
super(context, 0, rows);
this.context = context;
this.rows = new ArrayList<>();
this.rows.addAll(rows);
this.page = page;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.sList = page.getSTypesList();
this.columnNames = page.getColumnsDataNames();
this.searchList = rows;
}

public class ViewHolder {
TextView name;
TextView date;
}

public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
// DataRow row = rows.get(position);
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
holder.name = (TextView) view.findViewById(R.id.id_name);
holder.date = (TextView) view.findViewById(R.id.id_date);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(rows.get(position).getValues().get(0));
holder.date.setText(rows.get(position).getValues().get(1));

if (position == MainActivity.selectedListItem) {
view.setBackgroundColor(context.getResources().getColor(R.color.listSelectedItem));
objectId = rows.get(position).getValues().get(5);
lineId = rows.get(position).getValues().get(0);
Log.e("wartosci ", objectId + ", " + lineId);
} else {
if (position % 2 == 0) {
view.setBackgroundColor(Color.LTGRAY);
} else {
view.setBackgroundColor(Color.WHITE);
}
}

return view;
}

@Override
public long getItemId(int position) {
return position;
}

public void addRow(DataRow row) {
this.rows.add(row);
}

@Override
public DataRow getItem(int itemIndex) {
return rows.get(itemIndex);
}

public int getSize() {
return rows.size();
}

public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
rows.clear();
if (charText.length() == 0) {
rows.addAll(searchList);
} else {
for (DataRow wp : searchList) {
if (wp.getValueFromCol("nazwa").toLowerCase(Locale.getDefault())
.contains(charText)) {
rows.add(wp);
}
}
}
notifyDataSetChanged();
}
}

在我的 Activity 中:

 inputSearch = (EditText) findViewById(R.id.inputSearch);

inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
myAdapter.filter(text);
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {

}
});

当我使用搜索时,我的 apk 崩溃了。在日志中我有:

                                                                            java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4

在线:

    holder.name.setText(rows.get(position).getValues().get(0));

我不知道我做错了什么

最佳答案

添加

@Override
public int getCount() {
return rows.size();
}

在你的 ArrayAdapter 中

关于Java android listView 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192695/

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