Android 如何用SQLite中的一些特定数据填充listview??不完整
在我的android短信应用程序中,我不想在本机收件箱和已发送邮件中显示这些短信。我已经创建了一个SQLite数据库,用于将发送和接收的短信存储为私有应用程序 Sqlite数据库代码:Android 如何用SQLite中的一些特定数据填充listview??不完整,android,database,sqlite,listview,sms,Android,Database,Sqlite,Listview,Sms,在我的android短信应用程序中,我不想在本机收件箱和已发送邮件中显示这些短信。我已经创建了一个SQLite数据库,用于将发送和接收的短信存储为私有应用程序 Sqlite数据库代码: protected static final String Table1=("CREATE TABLE " +TABLE_SMS+" (" + KEY_MESSAGEID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +SMS_Ti
protected static final String Table1=("CREATE TABLE "
+TABLE_SMS+" ("
+ KEY_MESSAGEID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+SMS_Time+ " TEXT, "
+PHONE_NUMBER+ " TEXT, "
+MESSAGE_BODY+ " TEXT, "
+FLAG+" TEXT);");
public void Insert_sms_data(String formatedTime, String number, String body,String flag){
try{
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(SMS_Time, formatedTime);
cv.put(PHONE_NUMBER, number);
cv.put(MESSAGE_BODY, body);
cv.put(FLAG, flag);
DB.insert(TABLE_SMS, null, cv);
DB.close();
}
catch(Exception ex)
{
Log.e("ERROR in insertion", ex.toString());
}
}
public Cursor Return_All(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM "+"SMS_TABLE_RCV", null);
return cur;
}
SMS接收并存储在DB中,如下所示:
flag = "0";
DBmanager= new DbManager(context);
DBmanager.open();
DBmanager.Insert_sms_data(formatedTime ,number,body,flag);
DBmanager.close();
public class ChatActivity extends ListActivity {
private MyListAdapter adapter;
ArrayList<String> item_id = new ArrayList<String>();
ArrayList<String> item_phone_num = new ArrayList<String>();
ArrayList<String> item_msg_body = new ArrayList<String>();
ArrayList<String> item_time = new ArrayList<String>();
ArrayList<String> item_flag = new ArrayList<String>();
ArrayList<String> items = new ArrayList<String>();
private Button btn_send;
DbManager manager;
Cursor Cursor;
ViewHolder holder12;
String contact_for_chat;
String contact_no;
String message_body = "";
Calendar c;
SimpleDateFormat sdf;
String time;
EditText et_chat;
String flag;
String msg = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
contact_for_chat = bundle.getString("contact_name");
contact_for_chat = contact_for_chat.replace(" ", "");
contact_no = Util.getContactNumber(contact_for_chat, ChatActivity.this);
Toast.makeText(getApplicationContext(), contact_no, Toast.LENGTH_LONG).show();
final ViewHolder holder = new ViewHolder();
manager = new DbManager(this);
Cursor = manager.Return_All();
showEvents(Cursor);
c = Calendar.getInstance();
sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
time = sdf.format(c.getTime());
setActionBar();
findViewsById();
adapter = new MyListAdapter(this);
setListAdapter(adapter);
btn_send = (Button) findViewById(R.id.button1);
btn_send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SendSMS();
}
});
}
protected void SendSMS() {
SmsManager sms_manager = SmsManager.getDefault();
message_body = et_chat.getText().toString();
ArrayList<String> parts = sms_manager.divideMessage(message_body);
sms_manager.sendMultipartTextMessage(contact_no, null, parts, null, null);
flag = "1";
manager.Insert_sms_data(time, contact_no, message_body,flag);
msg+= "SMS to :" + contact_for_chat + " \n";
msg += "having number:" + contact_no + " \n";
msg += "as" +message_body + " \n";
msg += "at"+ time + " \n";
Toast.makeText(getApplicationContext(), ""+msg , Toast.LENGTH_LONG).show();
}
private void setActionBar() {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar_chat, null);
TextView tv_chat = (TextView)mCustomView.findViewById(R.id.title_text);
tv_chat.setText(contact_for_chat);
ColorDrawable colorDaawable = new ColorDrawable(Color.parseColor("#CFCFC4"));
mActionBar.setBackgroundDrawable(colorDaawable);
mActionBar.setLogo(R.drawable.ic_launcher);
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
private void findViewsById() {
et_chat = (EditText)findViewById(R.id.et_chat);
btn_send = (Button)findViewById(R.id.btn_send);
}
private void showEvents(Cursor cursor) {
item_id = new ArrayList<String>(cursor.getCount());
item_phone_num = new ArrayList<String>(cursor.getCount());
item_msg_body = new ArrayList<String>(cursor.getCount());
item_time = new ArrayList<String>(cursor.getCount());
item_flag = new ArrayList<String>(cursor.getCount());
int i=0;
while (cursor.moveToNext()) {
item_id.add(i+"");
item_time.add(cursor.getString(1));
item_msg_body.add(cursor.getString(3));
item_phone_num.add(cursor.getString(2));
item_flag.add(cursor.getString(4));
i++;
}
}
public class MyListAdapter extends BaseAdapter {
Context con;
private LayoutInflater layoutinf;
ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
ArrayList<String> items_ = new ArrayList<String>();
public MyListAdapter(ChatActivity context) {
con = context;
}
public int getCount() {
return item_id.size();
}
public Object getItem(int position) {
return item_id.size();
}
public long getItemId(int position) {
return item_id.get(position).hashCode();
}
public View getView(final int position, View arg1, ViewGroup arg2) {
View v = arg1;
ViewHolder holder = null;
if (v == null) {
layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutinf.inflate(R.layout.row_chat, null);
holder = new ViewHolder();
holder.tv_contact = (TextView) v.findViewById(R.id.phone_num);
holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body);
holder.tv_time = (TextView) v.findViewById(R.id.time);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
if(item_flag.get(position).equals("1"))
{
holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);
}
else
{
holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow);
}
holder.tv_contact.setText("" + item_phone_num.get(position));
holder.tv_sms_body.setText(item_msg_body.get(position));
holder.tv_time.setText(item_time.get(position));
return v;
}
}
public class ViewHolder {
private TextView tv_contact;
private TextView tv_sms_body;
private TextView tv_time;
}
发送短信和在listview中显示短信的代码如下:
flag = "0";
DBmanager= new DbManager(context);
DBmanager.open();
DBmanager.Insert_sms_data(formatedTime ,number,body,flag);
DBmanager.close();
public class ChatActivity extends ListActivity {
private MyListAdapter adapter;
ArrayList<String> item_id = new ArrayList<String>();
ArrayList<String> item_phone_num = new ArrayList<String>();
ArrayList<String> item_msg_body = new ArrayList<String>();
ArrayList<String> item_time = new ArrayList<String>();
ArrayList<String> item_flag = new ArrayList<String>();
ArrayList<String> items = new ArrayList<String>();
private Button btn_send;
DbManager manager;
Cursor Cursor;
ViewHolder holder12;
String contact_for_chat;
String contact_no;
String message_body = "";
Calendar c;
SimpleDateFormat sdf;
String time;
EditText et_chat;
String flag;
String msg = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
contact_for_chat = bundle.getString("contact_name");
contact_for_chat = contact_for_chat.replace(" ", "");
contact_no = Util.getContactNumber(contact_for_chat, ChatActivity.this);
Toast.makeText(getApplicationContext(), contact_no, Toast.LENGTH_LONG).show();
final ViewHolder holder = new ViewHolder();
manager = new DbManager(this);
Cursor = manager.Return_All();
showEvents(Cursor);
c = Calendar.getInstance();
sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
time = sdf.format(c.getTime());
setActionBar();
findViewsById();
adapter = new MyListAdapter(this);
setListAdapter(adapter);
btn_send = (Button) findViewById(R.id.button1);
btn_send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SendSMS();
}
});
}
protected void SendSMS() {
SmsManager sms_manager = SmsManager.getDefault();
message_body = et_chat.getText().toString();
ArrayList<String> parts = sms_manager.divideMessage(message_body);
sms_manager.sendMultipartTextMessage(contact_no, null, parts, null, null);
flag = "1";
manager.Insert_sms_data(time, contact_no, message_body,flag);
msg+= "SMS to :" + contact_for_chat + " \n";
msg += "having number:" + contact_no + " \n";
msg += "as" +message_body + " \n";
msg += "at"+ time + " \n";
Toast.makeText(getApplicationContext(), ""+msg , Toast.LENGTH_LONG).show();
}
private void setActionBar() {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar_chat, null);
TextView tv_chat = (TextView)mCustomView.findViewById(R.id.title_text);
tv_chat.setText(contact_for_chat);
ColorDrawable colorDaawable = new ColorDrawable(Color.parseColor("#CFCFC4"));
mActionBar.setBackgroundDrawable(colorDaawable);
mActionBar.setLogo(R.drawable.ic_launcher);
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
private void findViewsById() {
et_chat = (EditText)findViewById(R.id.et_chat);
btn_send = (Button)findViewById(R.id.btn_send);
}
private void showEvents(Cursor cursor) {
item_id = new ArrayList<String>(cursor.getCount());
item_phone_num = new ArrayList<String>(cursor.getCount());
item_msg_body = new ArrayList<String>(cursor.getCount());
item_time = new ArrayList<String>(cursor.getCount());
item_flag = new ArrayList<String>(cursor.getCount());
int i=0;
while (cursor.moveToNext()) {
item_id.add(i+"");
item_time.add(cursor.getString(1));
item_msg_body.add(cursor.getString(3));
item_phone_num.add(cursor.getString(2));
item_flag.add(cursor.getString(4));
i++;
}
}
public class MyListAdapter extends BaseAdapter {
Context con;
private LayoutInflater layoutinf;
ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
ArrayList<String> items_ = new ArrayList<String>();
public MyListAdapter(ChatActivity context) {
con = context;
}
public int getCount() {
return item_id.size();
}
public Object getItem(int position) {
return item_id.size();
}
public long getItemId(int position) {
return item_id.get(position).hashCode();
}
public View getView(final int position, View arg1, ViewGroup arg2) {
View v = arg1;
ViewHolder holder = null;
if (v == null) {
layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutinf.inflate(R.layout.row_chat, null);
holder = new ViewHolder();
holder.tv_contact = (TextView) v.findViewById(R.id.phone_num);
holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body);
holder.tv_time = (TextView) v.findViewById(R.id.time);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
if(item_flag.get(position).equals("1"))
{
holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);
}
else
{
holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow);
}
holder.tv_contact.setText("" + item_phone_num.get(position));
holder.tv_sms_body.setText(item_msg_body.get(position));
holder.tv_time.setText(item_time.get(position));
return v;
}
}
public class ViewHolder {
private TextView tv_contact;
private TextView tv_sms_body;
private TextView tv_time;
}
公共类ChatActivity扩展了ListActivity{
专用MyListAdapter适配器;
ArrayList项_id=新建ArrayList();
ArrayList item_phone_num=新建ArrayList();
ArrayList item_msg_body=新建ArrayList();
ArrayList项_time=新建ArrayList();
ArrayList item_flag=新建ArrayList();
ArrayList items=新建ArrayList();
私人按钮btn_发送;
数据库管理器;
光标;
视窗夹持器12;
字符串联系人用于聊天;
串接点号;
字符串消息_body=“”;
日历c;
简化格式sdf;
串时间;
编辑文本和聊天;
字符串标志;
字符串msg=“”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle Bundle=getIntent().getExtras();
contact_for_chat=bundle.getString(“contact_name”);
contact_for_chat=contact_for_chat.替换(“,”);
contact_no=Util.getContactNumber(contact_用于聊天,ChatActivity.this);
Toast.makeText(getApplicationContext(),联系人号,Toast.LENGTH.show();
最终视图持有者=新的视图持有者();
manager=新的DbManager(此);
Cursor=manager.Return_All();
showEvents(光标);
c=Calendar.getInstance();
sdf=新的简化格式(“dd:MMMM:yyyy HH:mm:ss a”);
time=sdf.format(c.getTime());
setActionBar();
findViewsById();
适配器=新的MyListAdapter(此);
setListAdapter(适配器);
btn_发送=(按钮)findViewById(R.id.button1);
btn_send.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
SendSMS();
}
});
}
受保护的void SendSMS(){
SmsManager sms_manager=smsmsmanager.getDefault();
message_body=et_chat.getText().toString();
ArrayList parts=sms\u manager.divideMessage(消息体);
sms_manager.sendMultipartTextMessage(联系人编号,空,部件,空,空);
flag=“1”;
经理。插入短信数据(时间、联系人、消息正文、标志);
msg+=“短信发送至:”+联系您进行聊天+“\n”;
msg+=“具有号码:”+联系人号+“\n”;
消息+=“作为”+消息正文+“\n”;
msg+=“在”+时间+“\n”;
Toast.makeText(getApplicationContext(),“”+msg,Toast.LENGTH_LONG).show();
}
私有void setActionBar(){
ActionBar mActionBar=getActionBar();
mActionBar.setDisplayHomeAsupaned(false);
mActionBar.setDisplayShowTitleEnabled(假);
LayoutInflater mInflater=LayoutInflater.from(this);
视图mCustomView=mInflater.flate(R.layout.actionbar\u chat,null);
TextView tv_chat=(TextView)mCustomView.findviewbyd(R.id.title_text);
tv_chat.setText(联系_聊天);
COLORDAWABLE COLORDAWABLE=新的COLORDAWABLE(Color.parseColor(“#cfcfcfc4”);
mActionBar.退根拉深(可着色);
mActionBar.setLogo(R.drawable.ic_启动器);
mActionBar.setDisplayHomeAsupaned(true);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
私有void findViewsById(){
et_chat=(EditText)findViewById(R.id.et_chat);
btn_send=(按钮)findviewbyd(R.id.btn_send);
}
私有void showEvents(光标){
item_id=newarraylist(cursor.getCount());
item_phone_num=新的ArrayList(cursor.getCount());
item_msg_body=newArrayList(cursor.getCount());
item_time=newarraylist(cursor.getCount());
item_flag=newarraylist(cursor.getCount());
int i=0;
while(cursor.moveToNext()){
项目标识添加(i+“”);
item_time.add(cursor.getString(1));
item_msg_body.add(cursor.getString(3));
添加项目(cursor.getString(2));
item_flag.add(cursor.getString(4));
i++;
}
}
公共类MyListAdapter扩展了BaseAdapter{
上下文con;
私人停车场更平坦的停车场;
ArrayList itemChecked=新建ArrayList();
ArrayList项=新建ArrayList();
公共MyListAdapter(聊天活动上下文){
con=上下文;
}
public int getCount(){
返回项目_id.size();
}
公共对象getItem(int位置){
返回项目_id.size();
}
公共长getItemId(int位置){
返回item_id.get(position).hashCode();
}
公共视图getView(最终整数位置,视图arg1,视图组arg2){
视图v=arg1;
ViewHolder=null;
如果(v==null){
layoutinf=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=布局充气(R.layout.row\u chat,空);
holder=新的ViewHolder();
holder.tv_contact=(TextView)v.findViewById(R.id.phone_num);
holder.tv\u sms\u body=(TextView)v.findViewById(R.id.msg\u body);
holder.tv_time=(TextView)v.findViewById(R.id.time);
v、 setTag(支架);
}否则{
holder=(ViewHolder)v.getTag();
}
if(item_flag.get(position).equals(“1”))
{
支架、电视、手机、机身、背景资源(可拉深、泡泡、绿色);
}
其他的
{
支架、电视、手机、机身、背景资源(R.可拉深、泡泡黄);
}
holder.tv_contact.setText(“+item_phone_num.get(position));
holder.tv_sms_body.setText(item_msg_body.get(position));
holder.tv_time.setText(item_time.get(position));
返回v;
}
}
公共类视图持有者{
私人文本视图电视联系人;
私家短信电视;
私人文本查看电视时间;
}
}
我检索到以下结果:
(来源:) 联络电话