I am trying to implement SwipeListView
with Pull-to-Refresh on a ListView
. it integrated successfully but it adds one item to the list after it. list view not refreshing. and no item added to the list.
I have implemented the SwipeListView
from here and Pull to Refresh from here.
how can i add more than one item in listview and refresh the list after added the item.
JAVA code
package com.eample.swipelistviewexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener2;
import com.fortysevendeg.swipelistview.PullToRefreshSwipeListView;
import com.fortysevendeg.swipelistview.SwipeListView;
public class Contact extends Activity {
public PullToRefreshSwipeListView ptorefreshList;
public SwipeListView swipeListView;
ProgressDialog dialog;
private ArrayList<String> mItems;
CategoryAdapter adapter;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mItems = new ArrayList<String>();
for(int i=0;i<10;i++)
mItems.add("Musethe place "+i);
ptorefreshList=(PullToRefreshSwipeListView) findViewById(R.id.example_list);
swipeListView = ptorefreshList.getRefreshableView();
adapter = new CategoryAdapter(Contact.this,
mItems);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setListview();
ptorefreshList.setOnRefreshListener(new OnRefreshListener<SwipeListView>() {
@Override
public void onRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
new NewDataTask().execute();
}
});
}
private void setListview() {
swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.
@Override
public void onClickFrontView(final int position) {
//do something here
}
@Override
public void onOpened(int position, boolean toRight) {
// TODO Auto-generated method stub
super.onOpened(position-1, toRight);
}
@Override
public void onMove(int position, float x) {
// TODO Auto-generated method stub
super.onMove(position-1, x);
}
@Override
public int onChangeSwipeMode(int position) {
// TODO Auto-generated method stub
return SwipeListView.SWIPE_MODE_DEFAULT;
}
@Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position-1, action, right);
}
});
ptorefreshList.setAdapter(adapter);
ptorefreshList.setLongClickable(true);
swipeListView.setSwipeOpenOnLongPress(false);
}
public class CategoryAdapter extends BaseAdapter {
LayoutInflater inflator;
ArrayList<String> category;
public CategoryAdapter(Context context,ArrayList<String> category)
{
inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.category=category;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return category.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int p=position;
View vi=convertView;
if(convertView==null)
vi=inflator.inflate(R.layout.custom_row,null);
TextView tv=(TextView) vi.findViewById(R.id.example_itemname);
tv.setText(category.get(position));
return vi;
}
}
private class NewDataTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= 11) {
dialog = new ProgressDialog(Contact.this,AlertDialog.THEME_HOLO_LIGHT);
} else {
dialog = new ProgressDialog(Contact.this);
}
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "WWW.MUSETHEPLACE.COM";
}
@Override
protected void onPostExecute(String result) {
// This should be called after refreshing finished
mItems.add(0, result);
adapter = new CategoryAdapter(Contact.this,
mItems);
ptorefreshList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
super.onPostExecute(result);
}
}
public int convertDpToPixel(float dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
}