我正在显示发送+收件箱消息的对话,获取并在列表视图中显示,每当有新消息到来或我是否会向该号码发送一条消息。我提供写下消息也在下面回复。这些消息当时没有显示,因为我需要回去参加这个活动。然后只显示消息。
我想立即显示一个,因为我使用了loadmanager但是没有工作。也没有显示任何错误。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.message_box);
messagelist = (ListView) findViewById(R.id.listViewSMS);
send = (ImageView) findViewById(R.id.send);
tempelates = (ImageView) findViewById(R.id.tempelates);
typemsg = (EditText) findViewById(R.id.typemsg);
reqnum = getIntent().getExtras().getString("contactname");
String messgid = getIntent().getExtras().getString("threadid");
Cursor c= getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id=" + messgid, null, null);
ArrayList<String> body = new ArrayList<String>();
ArrayList<String> msgtype = new ArrayList<String>();
ArrayList<String> timing = new ArrayList<String>();
ArrayList<String> newtime = new ArrayList<String>();
ArrayList<String> newdate = new ArrayList<String>();
ArrayList<String> newmonth = new ArrayList<String>();
ArrayList<String> newcheck = new ArrayList<String>();
if(c.moveToFirst())
{
do
{
String address = c.getString(c.getColumnIndexOrThrow("address")).toString();
body.add(c.getString(c.getColumnIndexOrThrow("body")).toString());
String getdate=c.getString(c.getColumnIndexOrThrow("date")).toString();
System.out.println("dteee"+getdate);
msgtype.add(c.getString(c.getColumnIndexOrThrow("type")).toString());
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Calendar calendar = Calendar.getInstance();
Long timestamp = Long.parseLong(getdate);
calendar.setTimeInMillis(timestamp);
newcheck.add(formatter.format(calendar.getTime()));
System.out.println("check time"+newcheck);
SimpleDateFormat tmp = new SimpleDateFormat("dd:yyyy");
Calendar calendar11 = Calendar.getInstance();
Long timestamp1 = Long.parseLong(getdate);
calendar11.setTimeInMillis(timestamp1);
newdate.add(formatter.format(calendar11.getTime()));
//String datee = tmp.format(calendar.getTime());
SimpleDateFormat tmp1 = new SimpleDateFormat("HH:mm");
Calendar calendar12 = Calendar.getInstance();
Long timestamp12 = Long.parseLong(getdate);
calendar12.setTimeInMillis(timestamp12);
newtime.add(formatter.format(calendar12.getTime()));
SimpleDateFormat month_date = new SimpleDateFormat("MMM:dd:yyyy");
Calendar calendar13 = Calendar.getInstance();
Long timestamp13 = Long.parseLong(getdate);
calendar13.setTimeInMillis(timestamp13);
newmonth.add(formatter.format(calendar13.getTime()));
//timing.add(newcheck);
}while(c.moveToNext());
}
else
{
System.out.println("Nomsg");
}
mAdapter = new MyAdapter(IndividualMessages.this,body,msgtype,newtime,newmonth);
messagelist.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(URL_LOADER, null, this);
send.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
String msg =typemsg.getText().toString();
if(msg.length()>140)
{for(int i=0;i<=msg.length()+1;i++)
{
try
{
String nowgoingmsg=msg.substring(i,i+140);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(reqnum,null,nowgoingmsg,null, null);
i=i+139;
}
catch(Exception e)
{
//e.printStackTrace();
String nowgoingmsg=msg.substring(i,msg.length()-1);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(reqnum,null,nowgoingmsg,null, null);
break;
}
}
}
else
{
try
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(reqnum,null,msg,null, null);
}catch(Exception e)
{
e.printStackTrace();
System.out.println("unale to send the message-----------------");
}
}
restoreSms(reqnum,typemsg.getText().toString());
typemsg.setText("");
finish();
}
private boolean restoreSms(String messagenumber,
String messagebody) {
boolean ret = false;
try {
ContentValues values = new ContentValues();
values.put("address", messagenumber);
values.put("body", messagebody);
Uri msguri=Uri.parse("content://sms/sent" );
IndividualMessages.this.getContentResolver().insert(msguri, values);
ret = true;
} catch (Exception ex) {
ret = false;
}
return ret;
}
});
}
public void OnBackPressed()
{
startActivity(new Intent(this,MessageListTabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
}
class MyAdapter extends BaseAdapter
{
private SparseBooleanArray mCheckStates;
ArrayList<String> body;
//ArrayList<String> timing;
ArrayList<String> msgtype;
LayoutInflater mInflater;
TextView tv1,tv,receive,sent;
LinearLayout l1,l2;
ArrayList<String> newtime;
ArrayList<String> newmonth;
MyAdapter(IndividualMessages individualmessages, ArrayList<String> body, ArrayList<String> msgtype,ArrayList<String> newtime,ArrayList<String>newmonth)
{
mCheckStates = new SparseBooleanArray(body.size());
mInflater = (LayoutInflater)IndividualMessages.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.body = body;
this.msgtype = msgtype;
this.newtime = newtime;
this.newmonth = newmonth;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return body.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 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.individual_message_row, null);
l1=(LinearLayout) vi.findViewById(R.id.chat1_area);
l2=(LinearLayout) vi.findViewById(R.id.chat2_area);
tv= (TextView) vi.findViewById(R.id.chat1_text);
tv1= (TextView) vi.findViewById(R.id.chat2_text);
receive= (TextView) vi.findViewById(R.id.receive);
sent= (TextView) vi.findViewById(R.id.sent);
try
{
if(msgtype.get(position).equalsIgnoreCase("1"))
{
l1.setVisibility(View.VISIBLE);
tv.setVisibility(View.VISIBLE);
receive.setVisibility(View.VISIBLE);
tv.setText(body.get(position));
receive.setText("Received:"+newtime.get(position)+","+newmonth.get(position));
l2.setVisibility(View.INVISIBLE);
tv1.setVisibility(View.INVISIBLE);
sent.setVisibility(View.INVISIBLE);
}
else
{
l2.setVisibility(View.VISIBLE);
tv1.setVisibility(View.VISIBLE);
sent.setVisibility(View.VISIBLE);
tv1.setText(body.get(position));
sent.setText("Sent:"+newtime.get(position)+","+newmonth.get(position));
l1.setVisibility(View.INVISIBLE);
tv.setVisibility(View.INVISIBLE);
receive.setVisibility(View.INVISIBLE);
}
}
catch(IndexOutOfBoundsException e){
}
return vi;
}
public void swapCursor(Object object) {
// TODO Auto-generated method stub
}
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
// TODO Auto-generated method stub
//mAdapter.changeCursor(arg1);
mAdapter.swapCursor(c);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
mAdapter.swapCursor(null);
}
@SuppressLint("SimpleDateFormat")
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("content://sms/");
CursorLoader cu = null;
switch (id) {
case URL_LOADER:
cu= new CursorLoader(IndividualMessages.this,uri,null,null,null, "date desc");
String messgid = getIntent().getExtras().getString("threadid");
Cursor c= getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id=" + messgid, null, null);
final ArrayList<String> body = new ArrayList<String>();
final ArrayList<String> msgtype = new ArrayList<String>();
final ArrayList<String> newcheck = new ArrayList<String>();
final ArrayList<String> newtime = new ArrayList<String>();
ArrayList<String> newdate = new ArrayList<String>();
final ArrayList<String> newmonth = new ArrayList<String>();
if(c.moveToFirst())
{
do
{
String address = c.getString(c.getColumnIndexOrThrow("address")).toString();
body.add(c.getString(c.getColumnIndexOrThrow("body")).toString());
String getdate=c.getString(c.getColumnIndexOrThrow("date")).toString();
msgtype.add(c.getString(c.getColumnIndexOrThrow("type")).toString());
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Calendar calendar = Calendar.getInstance();
Long timestamp = Long.parseLong(getdate);
calendar.setTimeInMillis(timestamp);
newcheck.add(formatter.format(calendar.getTime()));
System.out.println("check time"+newcheck);
SimpleDateFormat tmp = new SimpleDateFormat("dd:yyyy");
Calendar calendar11 = Calendar.getInstance();
Long timestamp1 = Long.parseLong(getdate);
calendar11.setTimeInMillis(timestamp1);
newdate.add(tmp.format(calendar11.getTime()));
//String datee = tmp.format(calendar.getTime());
SimpleDateFormat tmp1 = new SimpleDateFormat("hh:mm");
Calendar calendar12 = Calendar.getInstance();
Long timestamp12 = Long.parseLong(getdate);
calendar12.setTimeInMillis(timestamp12);
newtime.add(tmp1.format(calendar12.getTime()));
SimpleDateFormat month_date = new SimpleDateFormat("MMM:dd:yyyy");
Calendar calendar13 = Calendar.getInstance();
Long timestamp13 = Long.parseLong(getdate);
calendar13.setTimeInMillis(timestamp13);
newmonth.add(month_date.format(calendar13.getTime()));
}while(c.moveToNext());
}
else
{
System.out.println("Nomsg");
}
IndividualMessages.this.runOnUiThread(new Runnable() {
public void run() {
mAdapter = new MyAdapter(IndividualMessages.this,body,msgtype,newtime,newmonth);
messagelist.setAdapter(mAdapter);
sms.clear();
registerForContextMenu(messagelist);
}
});
default:
// An invalid id was passed in
}
return cu;
}
我也使用了片段活动的扩展