I am using BaseAdapter for filling the ListView
But it resets to the new item when it updates. How can i remain on its current position. i have searched and found few codes but it doesn't worked for me.
Here is my code:
public void appendToMessageHistory(ArrayList username, ArrayList message) {
if (username != null && message != null) {
if(lv.getAdapter()==null){
adapter = new ChatListAdapter(this, name, messages);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();}
else{
//int i=lv.getFirstVisiblePosition();
adapter.updateData(this, name, messages);
adapter.notifyDataSetChanged();
//lv.smoothScrollToPosition(i);
}
}
}
and my BaseAdapter :
public class ChatListAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> names;
private ArrayList<String> messages;
static class ViewHolder {
public TextView name, message;
public ImageView image;
}
public ChatListAdapter(Context context, ArrayList<String> names, ArrayList<String> messages) {
this.context = context;
this.names = names;
this.messages = messages;
}
public void updateData(Context context, ArrayList<String> names, ArrayList<String> messages){
this.context = context;
this.names = names;
this.messages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.layout,
null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) rowView.findViewById(R.id.name);
viewHolder.message=(TextView) rowView.findViewById(R.id.firstLine);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String name = names.get(position);
String message = messages.get(position);
holder.message.setTextColor(context.getResources().getColor(this.getTextColorID()));
return rowView;
}
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
// you have two different row types
return 2;
}
@Override
public int getItemViewType(int position) {
// You need to tell to adapter which row is needed at given position
return names.get(position).equals(own) ? 0 : 1;
}
}
I am also using stackFromBottom=true
I have used smoothScrollToPosition() but it scrolls back to the position after reset of listview i want to stick on the current position even when adapter updates.