I am building a chat application where I get a response in BroadcastReceiver. I send that data to IntentService where I successfully insert the data to Sqlite database. Now I would like to refresh the listview in my fragment according to database change automatically. My list is populated using CursorAdapter.
My code for MassageView Fragment is as follows:
public class MessageView_Fragment extends Fragment {
View rootView;
ListView lv;
ChatListAdapter chatAdapter;
ArrayList<Message_Master> arr;
MatrixCursor cr;
DBHelper db;
static String rMob;
ActionMode actionMode;
SharedPreferences prefs;
SimpleDateFormat sdf,sdf_old,sdf_today;
FrameLayout attacher;
boolean isBottom = true;
@SuppressLint("DefaultLocale")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout.fragment_message, container, false);
getActivity().getWindow().setBackgroundDrawableResource(R.drawable.chat_back);
rMob = getArguments().getString("rmob");
Log.e("rMob", rMob);
lv= (ListView)rootView.findViewById(R.id.chat_msg_list);
db = new DBHelper(getActivity());
db.UpdateUnseenToSeen(Long.valueOf(rMob));
Contact_Master cm = db.getContactDetails(rMob);
ActionBar mActionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(getActivity());
View mCustomView = mInflater.inflate(R.layout.custom_action_bar_chat, null);
RoundedImageView prof_pic = (RoundedImageView)mCustomView.findViewById(R.id.imageView1);
TextView tv_name = (TextView)mCustomView.findViewById(R.id.textView1);
if(cm.getName() == null)
{
tv_name.setText(rMob);
}
else
{
tv_name.setText(cm.getName());
}
TextView tv_details = (TextView)mCustomView.findViewById(R.id.textView2);
tv_details.setText(cm.getStatus());
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
attacher = (FrameLayout)rootView.findViewById(R.id.attacher);
prefs = getActivity().getSharedPreferences(SplashScreen_Activity.MyPrefs, Context.MODE_PRIVATE);
sdf = new SimpleDateFormat("dd MMM");
sdf_old = new SimpleDateFormat("MMMM dd, yyyy");
sdf_today = new SimpleDateFormat("h:mm a");
arr = db.getMessages(rMob);
cr = new MatrixCursor(new String[]{"_id","msg","type","dt","stat"});
boolean timeAdded = false;
if(arr.size()>0)
{
Calendar c = Calendar.getInstance();
for(int i = 0; i<arr.size();i++)
{
if(arr.get(i).getType() == 0)
{
cr.addRow(new String[]{String.valueOf(arr.get(i).getId()),arr.get(i).getMsg(),"0", sdf.format(arr.get(i).getMsgtime()),arr.get(i).getStatus()});
}
else
{
cr.addRow(new String[]{String.valueOf(arr.get(i).getId()),arr.get(i).getMsg(),"1",sdf.format(arr.get(i).getMsgtime()),arr.get(i).getStatus()});
}
}
}
chatAdapter = new ChatListAdapter(getActivity(),cr);
lv.setAdapter(chatAdapter);
chatAdapter.notifyDataSetChanged();
final EmojiconEditText emojiconEditText = (EmojiconEditText) rootView.findViewById(R.id.msg_edit);
final ImageButton emojiButton = (ImageButton) rootView.findViewById(R.id.smiley_btn);
final ImageButton submitButton = (ImageButton) rootView.findViewById(R.id.send_btn);
//On submit, add the edittext text to listview and clear the edittext
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String newText = emojiconEditText.getText().toString();
if(newText.trim().length()>0)
{
emojiconEditText.getText().clear();
long _id = db.insertMessage(1, newText, prefs.getString("regMobile", "0"), rMob);
// mAdapter.add(newText);
send(newText,_id,getActivity().getApplicationContext(),prefs.getString("regMobile", "0"));
cr.addRow(new String[]{String.valueOf(_id),newText,"1", sdf.format(Calendar.getInstance().getTime()),"W"});
chatAdapter.notifyDataSetChanged();
lv.setSelection(lv.getAdapter().getCount() - 1);
}
}
});
setHasOptionsMenu(true);
return rootView;
}
@SuppressLint("SimpleDateFormat")
public void RefreshList(long id,String msg)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM");
cr.addRow(new String[]{String.valueOf(id),msg,"0", sdf.format(Calendar.getInstance().getTime()),"W"});
chatAdapter.notifyDataSetChanged();
lv.setSelection(lv.getAdapter().getCount() - 1);
}
private void send(final String txt, final long _id, final Context context, final String myNumber) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
ServerUtilities.send(txt, rMob,_id,context,myNumber);
} catch (IOException ex) {
msg = "Message could not be sent";
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
if (!TextUtils.isEmpty(msg)) {
//Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
}
}.execute(null, null, null);
}}
Thanks in Advance.
Aucun commentaire:
Enregistrer un commentaire