마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

전화번호부 이름저장과 sms 관련 질문입니다.

0 추천
안녕하세요

제가 주소록과 sms 관련해서 어플을 만들고 있는데요

일단 주소록 db table을 만들고 주소록에 친구를 등록하면

이름과 전화번호를 저장하게 했습니다.

그리고 문자를 받는 db table은 따로 만들고

문자가 오면 전화번호와 받은 메세지를 저장할수 있게했습니다.

여기서 질문인데요

문자가 오면 받은 전화번호를 가지고 주소록 db와 비교를 해서

같은 번호가 있으면 sms 보관함에 번호가 아닌

주소록에 등록된 이름으로 나오게 하려는데

그게 잘안되네요... 아직 초보라 table을 하나로 만들어서 해야할지

아니면 따로 두개를 가지고 비교를 해서 하는건지 잘 모르겠습니다.

public class Smsallview extends Activity {
   
 ListView list;
 dbHelper dbHelper;
 SQLiteDatabase db;
 String sql;
 Cursor cursor;
 ArrayList<String> Items;
 ArrayAdapter<String> Adapter;
 private String yousay, mysay, youphone, myphone, youname;
 final static String dbName = "testchat.db";
final static int dbVersion = 1;
 
  ArrayList<smsallviewListItem> arItem= new ArrayList<smsallviewListItem>();  //뷰합치기 관련
  
   
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smsallview);
       
       
        //db 사용하겠다
        dbHelper = new dbHelper(this);
        try{
         db = dbHelper.getWritableDatabase();
        } catch (SQLiteException ex){
         db = dbHelper.getReadableDatabase();
        }
        //db사용하겠다
        list = (ListView)findViewById(R.id.list);
       
       
        list.setDivider(null);
  list.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
      
  
  Cursor cursor;
  Cursor c;
      
  cursor = db.rawQuery("SELECT * FROM contact", null);
  c = db.rawQuery("SELECT * FROM friends", null);
   
  boolean checkDB = false;

  
   while(cursor.moveToNext() && c.moveToNext()){
       String sendphone = cursor.getString(1);
     String sendmgs = cursor.getString(2);
     String recephone = cursor.getString(3);
    String recemgs = cursor.getString(4);
    String fname = c.getString(1);
    String fphone = c.getString(2);
   /*
    if((c.getString(2)).equals(recephone) || (c.getString(2)).equals(sendphone)){
      checkDB = true;                                                                                                             
     
      break;                                  // 같은번호인지 비교하려고 넣으려고 했는데 이렇게 하니까 안되네요;;;
     }
    */
    if((sendphone != null) && (sendmgs != null)){
     mysay =  "메시지:"+ sendmgs ;
     youname =sendphone;
     
     arItem.add(new smsallviewListItem(1,"","",youname,mysay));
        
        }else{
        
        
        }
   
   if((recephone != null) && (recemgs != null)){

    yousay =  "메시지:"+ recemgs ;
    youname = recemgs;
    arItem.add(new smsallviewListItem(0,youname,yousay,"",""));              // 위에 myname하고 밑에  youname 부분을

   }                                                                                                                                fname으로 바꿔보았는데 이것도 아니네요.ㅜ
   else{
    
   }
    smsallviewMultiAdapter MyAdapter = new smsallviewMultiAdapter(Smsallview.this,arItem);
    list.setAdapter(MyAdapter);
  
   }
   cursor.close();
  }
       
}

class smsallviewListItem{
 smsallviewListItem(int aType, String chat1, String chattime1, String chat2,String chattime2){
  Type = aType;
  UpChat = chat1;
  ChatTime1 = chattime1;
  DownChat = chat2;
  ChatTime2 = chattime2;
 }
 
 int Type;
 String UpChat;
 String ChatTime1;
 String DownChat;
 String ChatTime2;
 
}

class smsallviewMultiAdapter extends BaseAdapter{
 LayoutInflater mInflater;
 ArrayList<smsallviewListItem> arSrc;
 
 public smsallviewMultiAdapter(Context context, ArrayList<smsallviewListItem> arItem){
  mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  arSrc = arItem;
 }
 
 public int getCount(){
  return arSrc.size();
 }
 
 public smsallviewListItem getItem(int position){
  return arSrc.get(position);
 }
 
 public long getItemId(int position){
  return position;
 }
 
 public int getItemViewType(int position){
  return arSrc.get(position).Type;
 }
 
 public int getViewTypeCount(){
  return 2;
 }
 
 public View getView(int position, View convertView, ViewGroup parent){
  if(convertView == null){
   int res = 0;
   switch(arSrc.get(position).Type){
   case 0:
    res = R.layout.smsallviewlay1;
    break;
   case 1:
    res = R.layout.smsallviewlay2;
    break;
   }
   convertView = mInflater.inflate(res, parent,false);
  }
  
  switch(arSrc.get(position).Type){
  case 0:
   
   TextView name = (TextView)convertView.findViewById(R.id.name1);
   name.setText(arSrc.get(position).UpChat);
   TextView phone = (TextView)convertView.findViewById(R.id.phone1);
   phone.setText(arSrc.get(position).ChatTime1);
   break;
  case 1:
   
   TextView name2 = (TextView)convertView.findViewById(R.id.name2);
   name2.setText(arSrc.get(position).DownChat);
   TextView phone2 = (TextView)convertView.findViewById(R.id.phone2);
   phone2.setText(arSrc.get(position).ChatTime2);
   break;
  }
  
  return convertView;
 }
}

 

대충 이런식으로 코딩을 했는데요

이건 문자가 오면 보관함에 받거나 보낸 메세지를

전화번호(친구) 별로 쭉 나오게 하려고 한겁니다.

질문드린부분하고 혹시나

같은 번호로 또 문자가 오면 지금은 밑에 또 나오는데

그게 아니고 같은번호는 같은번호별로 묶여서

이름은같고 메세지만 최근 메세지로 보이게 하는것 방법도

힌트라도 주셨으면 합니다...ㅜㅜ
모하지모해 (300 포인트) 님이 2013년 5월 29일 질문

1개의 답변

0 추천
db에 속성하나 추가하셔서 flag나 count 값등을 넣어서 한번 하면 flag나 count를 증가하시고 select 문을 이용하여 flag가 다르거나 count가 0보다 클 경우 안그리는 식으로 하시면 될 듯 합니다.
wind (2,240 포인트) 님이 2013년 5월 29일 답변
답변 감사합니다.
혹시 그거 말고 첨에 질문드린
다른 두 테이블에서

문자가 오면 받은 전화번호를 가지고 주소록 db와 비교를 해서

같은 번호가 있으면 sms 보관함에 번호가 아닌

주소록에 등록된 이름으로 나오게 하려는데

요 질문은 혹시 아시면 설명좀 부탁드려도 될까요
...