Android ArrayAdapter 在滚动时动态添加更多数据
Android ArrayAdapter dynamic adding more data on scrolling
我想在滚动到达其 5 位置时在滚动时添加一些数据,但出现错误 ArrayOutOfBoundException...
//在MainActivity中设置适配器
Context context = this;
ArrayList<String> data = new ArrayList<String> ;
for(int i =0; i <20;i++){
data.add("a");
}
CustomAdapter customAdapter = new CustomAdapter(
context, data);
autoComplete.setAdapter(customAdapter);
// 自定义适配器
public class customAdapter extends ArrayAdapter<String> {
private Context context;
private LayoutInflater layoutInflater;
private LogsViewHolder logViewHolder;
ArrayList<String> data = null;
public customAdapter(Context context, ArrayList<String> data) {
super(context, 0, data);
this.context = context;
this.data = data;
layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemCount = data.size() - position;
if (getCount()>position && itemCount== 5) {
data.add("bbbbbbbbbbbbbb");
data.add("bbbbbbb ");
data.add("b bb");
data.add("bbb bbbb");
data.add("bb bbbbbb");
data.add("b bbb");
CustomAdapter customAdapter = (CustomSearchAdapter) autoComplete.getAdapter();
customAdapter.notifyDataSetChanged();
}
if (convertView == null) {
logViewHolder = new LogsViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row, null);
logViewHolder.data = (TextView) convertView
.findViewById(R.id.data);
convertView.setTag(logViewHolder);
} else
logViewHolder = (LogsViewHolder) convertView.getTag();
logViewHolder.data.setText(data.get(position));
return convertView;
}
public class LogsViewHolder {
public TextView data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public long getItemId(int position) {
return position;
}
}
请告诉我如何消除该错误以及为什么会出现该错误的解决方案...
一旦尝试如下更改以下行
if (getCount()>position && itemCount== 5) {
到
if (position==4) {
注意:如果滚动到 5 个位置,那么在 getView() 中位置是 4,因为索引从 0 开始。
如果你想在列表视图的末尾添加更多项目,你可以尝试如下
if (position==itemCount-1) {
希望对您有所帮助。
我认为你应该尝试将 OnScrollListener
添加到你的 ListView
而不是在你的 Adapter
中添加 getView
:
public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
// check position here.
}
如果您想创建 EndlessAdapter,请查看之前的 link:
@Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount()<75) {
@SuppressWarnings("unchecked")
// Append your adapter here.
}
}
我想在滚动到达其 5 位置时在滚动时添加一些数据,但出现错误 ArrayOutOfBoundException...
//在MainActivity中设置适配器
Context context = this;
ArrayList<String> data = new ArrayList<String> ;
for(int i =0; i <20;i++){
data.add("a");
}
CustomAdapter customAdapter = new CustomAdapter(
context, data);
autoComplete.setAdapter(customAdapter);
// 自定义适配器
public class customAdapter extends ArrayAdapter<String> {
private Context context;
private LayoutInflater layoutInflater;
private LogsViewHolder logViewHolder;
ArrayList<String> data = null;
public customAdapter(Context context, ArrayList<String> data) {
super(context, 0, data);
this.context = context;
this.data = data;
layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemCount = data.size() - position;
if (getCount()>position && itemCount== 5) {
data.add("bbbbbbbbbbbbbb");
data.add("bbbbbbb ");
data.add("b bb");
data.add("bbb bbbb");
data.add("bb bbbbbb");
data.add("b bbb");
CustomAdapter customAdapter = (CustomSearchAdapter) autoComplete.getAdapter();
customAdapter.notifyDataSetChanged();
}
if (convertView == null) {
logViewHolder = new LogsViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row, null);
logViewHolder.data = (TextView) convertView
.findViewById(R.id.data);
convertView.setTag(logViewHolder);
} else
logViewHolder = (LogsViewHolder) convertView.getTag();
logViewHolder.data.setText(data.get(position));
return convertView;
}
public class LogsViewHolder {
public TextView data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public long getItemId(int position) {
return position;
}
}
请告诉我如何消除该错误以及为什么会出现该错误的解决方案...
一旦尝试如下更改以下行
if (getCount()>position && itemCount== 5) {
到
if (position==4) {
注意:如果滚动到 5 个位置,那么在 getView() 中位置是 4,因为索引从 0 开始。
如果你想在列表视图的末尾添加更多项目,你可以尝试如下
if (position==itemCount-1) {
希望对您有所帮助。
我认为你应该尝试将 OnScrollListener
添加到你的 ListView
而不是在你的 Adapter
中添加 getView
:
public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
// check position here.
}
如果您想创建 EndlessAdapter,请查看之前的 link:
@Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount()<75) {
@SuppressWarnings("unchecked")
// Append your adapter here.
}
}