Reputation: 109
I'm ListView
after append any item to that doesn't refresh to view new inserted item(s). My function to add and refresh ListView
is this :
public void getReceivedSMSFromWebService(long start, long count) throws Exception{
getReceivedSMS(start, count);
Cursor cursor = db.getAllReceivedSMSFromDatabase();
cursor.moveToFirst();
while (cursor.moveToNext()) {
ReceiveFields tmp = new ReceiveFields();
tmp.setLastId( Long.valueOf(cursor.getString(cursor.getColumnIndex("lastId"))) );
tmp.setSmsNumber ( cursor.getString(cursor.getColumnIndex("lastId")) );
tmp.setMobileNumber( cursor.getString(cursor.getColumnIndex("mobileNumber")) );
tmp.setSenderName ( cursor.getString(cursor.getColumnIndex("senderName")) );
tmp.setSmsBody ( cursor.getString(cursor.getColumnIndex("smsBody")) );
tmp.setReceiveDate ( cursor.getString(cursor.getColumnIndex("receiveDate")) );
rows.add(tmp);
}
cursor.close();
receiveListView.notifyDataSetChanged();
Log.e( "++++++++++++++++++++++++ receiveListView COUNT " , String.valueOf( receiveListView.getCount() ) );
}
rows
in that is private List<ReceiveFields> rows;
and ReceiveFields
is data class structure :
public class ReceiveFields {
public long lastId;
public String smsNumber;
public String mobileNumber;
public String senderName;
public String smsBody;
public String receiveDate;
private Context ctx;
}
In that receiveListView.notifyDataSetChanged();
does not work correctly. but after I'm testing with this function :
private void setReceivedSMSToListView() {
receiveListView = new ViewReceivedSMSDetailes(getActivity(), rows);
setListAdapter(receiveListView);
}
That works correctly and can be show new inserted item. What's the problem and why is notifyDataSetChanged
not working correctly? Data can be added successfully and I don't have problem adding items. This class header is :
public class ReceivedSMS extends ListFragment implements AbsListView.OnScrollListener
ReceivedSMS class :
public class ReceivedSMS extends ListFragment implements AbsListView.OnScrollListener {
public List<ReceiveFields> rows;
private int prevVisibleItem;
private TSMS tsms;
private String username;
private String password;
public Long getLastID;
private boolean isFirstTime;
private Context context;
private DatabaseHandler db;
private SQLiteDatabase dbHelper;
private ViewReceivedSMSDetailes receiveListView;
public ReceivedSMS(Context context, String username, String password) {
this.username = username;
this.password = password;
this.context = context;
}
public ReceivedSMS(String username, String password, long start, long count, Context context) {
this.username = username;
this.password = password;
this.context = context;
tsms = new TSMS(context, new User("tsms_ir", "1234567"));
try {
getReceivedSMS(start, count);
} catch (Exception e1) {
e1.printStackTrace();
Log.e("Error in getReceivedSMS(start, count); ", "");
}
}
public ReceivedSMS(Context context) {
this.context = context;
rows = getReceivedSMSFromDB();
}
public void getReceivedSMSFromWebService(long start, long count) throws Exception{
getReceivedSMS(start, count);
Cursor cursor = db.getAllReceivedSMSFromDatabase();
cursor.moveToFirst();
List<ReceiveFields> tmp = new ArrayList<ReceiveFields>();
while (cursor.moveToNext()) {
tmp.add(new ReceiveFields(
Long.valueOf(cursor.getString(cursor.getColumnIndex("lastId"))),
cursor.getString(cursor.getColumnIndex("smsNumber")),
cursor.getString(cursor.getColumnIndex("mobileNumber")),
cursor.getString(cursor.getColumnIndex("senderName")),
cursor.getString(cursor.getColumnIndex("smsBody")),
cursor.getString(cursor.getColumnIndex("receiveDate"))));
}
cursor.close();
this.rows = tmp;
receiveListView.notifyDataSetChanged();
Log.e( "++++++++++++++++++++++++ receiveListView COUNT " , String.valueOf( receiveListView.getCount() ) );
}
public List<ReceiveFields> getReceivedSMS(long start, long count) throws UnsupportedEncodingException {
tsms = new TSMS(context, new User("tsms_ir", "1234567"));
try {
rows = tsms.getReceivedSMS(start, count);
saveRowsintoDatabase( rows );
} catch (TException e) {
e.printStackTrace();
Log.e(getClass().toString(), "ERROR IN Fetch SMS From WebService List<ReceiveFields> getReceivedSMS(long start, long count) "+ String.valueOf(e));
}
return rows;
}
public void addToCurrentList (List<ReceiveFields> receiveRow ){
rows.addAll(receiveRow);
}
public void saveRowsintoDatabase( List<ReceiveFields> receiveRow ){
/*
* SAVE fetched from web service to database
*/
for( ReceiveFields rf:receiveRow) {
String splitDate[] = rf.getReceiveDate().split("/");
CalendarTool ct = new CalendarTool(
Integer.valueOf(splitDate[0]),
Integer.valueOf(splitDate[1]),
Integer.valueOf(splitDate[2]));
try {
/*
* Find Duplicate row if not exist then can be write into DataBase
*/
String selectQuery = "SELECT lastId FROM ReceiveFields WHERE lastId = ? ";
String[] args = {String.valueOf(rf.lastId)};
Cursor cursor = dbHelper.rawQuery(selectQuery, args);
if (!cursor.moveToFirst()) {
db.addReceivedToDataBase(new ReceiveFields(
Long.valueOf(rf.lastId), rf.getSmsNumber(),
rf.getMobileNumber(), URLDecoder.decode(rf.getSenderName(), "UTF-8"),
URLDecoder.decode(rf.getSmsBody(), "UTF-8"), ct.getGregorianDate()));
}
cursor.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(getClass().toString(),"Error in db.addReceivedToDataBase(new ReceiveFields("+ String.valueOf(e));
}
}
}
public List<ReceiveFields> getReceivedSMSFromDB() {
tsms = new TSMS(context);
return tsms.getReceivedSMSFromDB();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHandler(context);
dbHelper = db.getWritableDatabase();
setReceivedSMSToListView();
}
private void setReceivedSMSToListView() {
receiveListView = new ViewReceivedSMSDetailes(getActivity(), rows);
setListAdapter(receiveListView);
}
public long getLastID() {
return rows.get(rows.size() - 1 ).getLastId();
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onCreate(savedInstanceState);
getListView().setOnScrollListener(this);
}
@Override
public void onListItemClick(ListView list, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (prevVisibleItem != firstVisibleItem) {
//if (prevVisibleItem < firstVisibleItem)
// Log.i("info","");
//else
// Log.e(getClass().toString(), "UP");
prevVisibleItem = firstVisibleItem;
if (firstVisibleItem == 0 && !isFirstTime) {
Log.i("Info", "residam avval");
}
if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
Log.e("getReceivedSMS FUNCTION ", String.valueOf(getLastID()));
try {
getReceivedSMSFromWebService(getLastID(), 20);
} catch (Exception e) {
e.printStackTrace();
Log.e(getClass().toString() , "Error in receive from WebService when scroll is in bottom of list "+ e);
}
Log.i("Info", "Connecting to server for get old messages if scrolling down finished");
}
}
}
}
ADAPTER :
public class ViewReceivedSMSDetailes extends BaseAdapter {
private LayoutInflater inflater;
private List<ReceiveFields> row;
public ViewReceivedSMSDetailes(Context context, List<ReceiveFields> row) {
this.row = row;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return row.size();
}
@Override
public ReceiveFields getItem(int position) {
return row.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.received_sms_list_fragment, null);
}
TextView tv_smsBody = (TextView)convertView.findViewById(R.id.receive_SmsBody);
TextView tv_Mobile = (TextView)convertView.findViewById(R.id.receive_Mobile);
tv_smsBody.setText(getItem(position).getMobileNumber());
tv_Mobile.setText(getItem(position).getMobileNumber());
return convertView;
}
}
UPDATE getReceivedSMSFromWebService
function :
public void getReceivedSMSFromWebService(long start, long count) throws Exception{
getReceivedSMS(start, count);
Cursor cursor = db.getAllReceivedSMSFromDatabase();
cursor.moveToFirst();
List<ReceiveFields> tmp = new ArrayList<ReceiveFields>();
while (cursor.moveToNext()) {
tmp.add(new ReceiveFields(
Long.valueOf(cursor.getString(cursor.getColumnIndex("lastId"))),
cursor.getString(cursor.getColumnIndex("smsNumber")),
cursor.getString(cursor.getColumnIndex("mobileNumber")),
cursor.getString(cursor.getColumnIndex("senderName")),
cursor.getString(cursor.getColumnIndex("smsBody")),
cursor.getString(cursor.getColumnIndex("receiveDate"))));
this.rows = tmp;
}
cursor.close();
receiveListView.setRow(tmp);
receiveListView.notifyDataSetChanged();
Log.e( "++++++++++++++++++++++++ receiveListView COUNT " , String.valueOf( receiveListView.getCount() ) );
}
Adapter :
public void setRow(List<ReceiveFields> row) {
this.row = row;
}
Upvotes: 1
Views: 224
Reputation: 6942
As you are not populating the data of the Adapter's List
your new data will not appear in the ListView
there are two ways to make this data appear in the ListView
for this you need to create a method in your Adapter class
public void setRow(List<ReceiveFields> row) {
this.row = row;
}
and after adding or removing the data from other list you need to set that list to the adapter in your case in method getReceivedSMSFromWebService
after closing cursor and before notifyDataSetChanged()
cursor.close();
receiveListView.setRow(row);
receiveListView.notifyDataSetChanged();
and another option is to set your Adapter again with a new List with populated data. as you had created a method for that then you just need to call that method.
setReceivedSMSToListView();
UPDATED :
Try with the following method :
public void getReceivedSMSFromWebService(long start, long count) throws Exception{
getReceivedSMS(start, count);
Cursor cursor = db.getAllReceivedSMSFromDatabase();
cursor.moveToFirst();
// List<ReceiveFields> tmp = new ArrayList<ReceiveFields>();
while (cursor.moveToNext()) {
ReceiveFields mReceiveFields = new ReceiveFields();
mReceiveFields.setLastId(Long.valueOf(cursor.getString(cursor.getColumnIndex("lastId"))));
mReceiveFields.setSmsNumber(cursor.getString(cursor.getColumnIndex("lastId")));
mReceiveFields.setMobileNumber(cursor.getString(cursor.getColumnIndex("mobileNumber")));
mReceiveFields.setSenderName(cursor.getString(cursor.getColumnIndex("senderName")));
mReceiveFields.setSmsBody(cursor.getString(cursor.getColumnIndex("smsBody")));
mReceiveFields.setReceiveDate(cursor.getString(cursor.getColumnIndex("receiveDate")));
// while here you are creating new List named **tmp** and
// assining to the rows which will delete your old data of rows list
// add object directly to the rows list
this.rows.add(mReceiveFields);
}
cursor.close();
// here you are you are assigning **tmp** as setRow() instead of that
receiveListView.setRow(rows);
receiveListView.notifyDataSetChanged();
Log.e( "++++++++++++++++++++++++ receiveListView COUNT " , String.valueOf( receiveListView.getCount() ) );
}
Upvotes: 2
Reputation: 1167
try to replace this:
if (convertView == null) {
convertView = inflater.inflate(R.layout.received_sms_list_fragment, null);
}
with this:
if (convertView == null) {
convertView = inflater.inflate(R.layout.received_sms_list_fragment, parent);
}
Upvotes: 0
Reputation: 4091
adapter.notifyDataSetChanged() doesn't work if the list which you are passing to the adapter is not populated one by one in a loop. In other words, if you assign a list to the list you are passing to the adapter, adapter.notifyDataSetChanged() will not work. For example, you are doing here..
this.rows = tmp;
here rows is the list you are passing to the adapter and tmp is another list, if you do like this you have done already i.e assign a list to the list you are passing in the adapter, notify.dataSetChanged() will not work. So solution is you should add values directly to the rows list instead of adding it first to tmp list and then assigning that list to the rows list.
Upvotes: 0