I am using the RefreshableListView (Pull to refresh) from here .But I am using a Custom ListAdapter and not the ArrayAdapter. It working all fine but the only issue is that it is adding new items to the bottom of the list while I want new items to be displayed on top. . I have referred to a lot of articles on the net addressing similar issues but have not found a solution for my issue. It will be great if anybody could point out where I am going wrong.
Here are the code snippets:
public void onCreate(Bundle savedInstanceState) {
final CustomAdapter adapter = new CustomAdapter(this, R.layout.mylist, mItems);
mListView = (RefreshableListView) findViewById(R.id.listview);
mListView.setAdapter(adapter);
// Callback to refresh the list
mListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshableListView listView) {
ivFooter.setImageResource(R.drawable.back);
new NewDataTask().execute();
}
});
}
and the AsyncTask
private class NewDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
String retVal = myArrayList.get(i);
if (i < myArrayList.size() - 1) {
i++;
} else {
i = 0;
}
return retVal;
}
@Override
protected void onPostExecute(String result) {
mItems.add(0, result);
mListView.completeRefreshing();
super.onPostExecute(result);
}
}
and Custom Adapter class
class CustomAdapter extends BaseAdapter {
public int getCount() {
return mItems.size();
}
public Object getItem(int position) {
return mItems.get(position);
}
public long getItemId(int position) {
return position;
}
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
return false;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mylist, null);
}
String quote = myArrayList.get(position);
if (quote != null) {
TextView tt = (TextView) v.findViewById(R.id.tvQuote);
ImageView iv = (ImageView) v.findViewById(R.id.ivPicture);
if (tt != null) {
tt.setText(quote);
}
if (iv != null) {
//Setting image
}
return v;
}
'mItems' is the original list with one item and I am adding 'myArrayList' to it. Like I said its working fine but adding elements (on refresh) one by one at the bottom ..while I want the new elements to be added on top. Any inputs would be of great help.
Thanks in advance.
Replace String quote = myArrayList.get(position);
with String quote = RefreshableListViewActivity.this.mItems.get(position);
and it works as expected.
Thanks to @Dante for helping me out.