2013-12-16 24 views
0

在我的项目中,我需要从webservice和视图中获取消息,在listview.I需要区分listview.I中读取和未读消息检索到的消息,并将其存储在sqlite数据库中。如何区分读取和未读的消息。而且listview没有正确填充。不是显示文本,而是显示textview id在某一行。根据sqlite结果在列表视图中区分行

这里是我的代码,

 public class Message_Adapter extends ArrayAdapter<Message> implements OnClickListener { 
     Activity activity; 
     int layoutResourceId; 
     Message user; 
     ArrayList<Message> data = new ArrayList<Message>(); 

     public Message_Adapter(Activity act, int layoutResourceId, 
      List<Message> data) { 
      super(act, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.activity = act; 
      this.data = (ArrayList<Message>) data; 
      notifyDataSetChanged(); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      UserHolder holder = null; 

      if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(activity); 

      row = inflater.inflate(layoutResourceId, parent, false); 
      holder = new UserHolder(); 
      holder.fromnum = (TextView) row.findViewById(R.id.fromno); 
      holder.tonum = (TextView) row.findViewById(R.id.tonum); 
      holder.body= (TextView) row.findViewById(R.id.msgbody); 
      holder.date = (TextView) row.findViewById(R.id.msgdate); 
      holder.delete = (Button) row.findViewById(R.id.btn_delete); 
      holder.status = (TextView) row.findViewById(R.id.staty); 
      row.setTag(holder); 
      } else { 
      holder = (UserHolder) row.getTag(); 
      } 
      user = data.get(position); 
      smsm=user.getID(); 
      System.out.println("Smsm::::"+smsm); 
      holder.delete.setTag(user.getID()); 
      holder.fromnum.setText(user.getmsgfrom()); 
      holder.tonum.setText(user.getto()); 
      holder.body.setText(user.getmsg()); 
      holder.date.setText(user.getdate()); 
      holder.status.setText(user.getstatus()); 


      holder.delete.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(final View v) { 
       // TODO Auto-generated method stub 

       // show a message while loader is loading 

       AlertDialog.Builder adb = new AlertDialog.Builder(activity); 
       adb.setTitle("Delete?"); 
       adb.setMessage("Are you sure you want to delete "); 
       //final int user_id = Integer.parseInt(v.getTag().toString()); 
       adb.setNegativeButton("Cancel", null); 
       adb.setPositiveButton("Ok", 
        new AlertDialog.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, 
         int which) { 
         // MyDataObject.remove(positionToRemove); 
         DatabaseHandler dBHandler = new DatabaseHandler(
          activity.getApplicationContext()); 
         dBHandler.Delete_Contact(smsm); 
         InboxActivity.this.onResume(); 

        } 
        }); 
       adb.show(); 
      } 

      }); 
      row.setOnClickListener(new OnItemClickListener(position)); 

      return row; 

     } 

这是我的收件箱中的自定义布局,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="100sp" 
android:background="@drawable/edittext" 
android:orientation="vertical" > 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/fromno" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/tonum" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/msgbody" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/msgdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
    <TextView 
     android:id="@+id/staty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
</LinearLayout> 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:orientation="horizontal" > 
    </LinearLayout> 

</LinearLayout> 
</LinearLayout> 
+0

你用哪个变量从数据库中读取和未读邮件进行区分? 你可以把你看到的错误人口的图片放在一起吗? –

+0

我有名为Status.For那些有0值的行必须不同于具有1值的行... – Jolly

+0

@Lena Bru,我无法上传屏幕截图...不是加载textview的详细信息,而是加载资源ID这些textviews文字 – Jolly

回答

0
@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      UserHolder holder = null; 

     if (row == null) { 
     LayoutInflater inflater = LayoutInflater.from(activity); <== move this line to the constructor of the adapter,and use it as a member 

     row = inflater.inflate(layoutResourceId, parent, false); 
     holder = new UserHolder(); 
     holder.fromnum = (TextView) row.findViewById(R.id.fromno); 
     holder.tonum = (TextView) row.findViewById(R.id.tonum); 
     holder.body= (TextView) row.findViewById(R.id.msgbody); 
     holder.date = (TextView) row.findViewById(R.id.msgdate); 
     holder.delete = (Button) row.findViewById(R.id.btn_delete); 
     holder.status = (TextView) row.findViewById(R.id.staty); 
     row.setTag(holder); 
     } else { 
     holder = (UserHolder) row.getTag(); 
     } 
     user = data.get(position); 
     smsm=user.getID(); 
     System.out.println("Smsm::::"+smsm); 
     holder.delete.setTag(user.getID()); 
     holder.fromnum.setText(user.getmsgfrom()); 
     holder.tonum.setText(user.getto()); 
     holder.body.setText(user.getmsg()); 
     holder.date.setText(user.getdate()); 
     holder.status.setText(user.getstatus()); 


     holder.delete.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(final View v) { 
      // TODO Auto-generated method stub 

      // show a message while loader is loading 

      AlertDialog.Builder adb = new AlertDialog.Builder(activity); 
      adb.setTitle("Delete?"); 
      adb.setMessage("Are you sure you want to delete "); 
      //final int user_id = Integer.parseInt(v.getTag().toString()); 
      adb.setNegativeButton("Cancel", null); 
      adb.setPositiveButton("Ok", 
       new AlertDialog.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, 
        int which) { 
        // MyDataObject.remove(positionToRemove); 
        DatabaseHandler dBHandler = new DatabaseHandler(
         activity.getApplicationContext()); 
        dBHandler.Delete_Contact(smsm); 
        InboxActivity.this.onResume(); 

       } 
       }); 
      adb.show(); 
     } 

     }); 
     row.setOnClickListener(new OnItemClickListener(position));// do not put onItemClickListener here, it is supposed to go into the activity or fragment whereever your listview object resides, give it "listView.setOnItemClickListener()", it will be noticable in performance when your listview has a lot of rows, that their click listeners get restarted every time you scroll or refresh the list 



      if(Status == 0){ 
    //message is unread 
    }else { 
     // message is read 
} // this status will change whenever you call notifyDataSetChanged on the adapter, because it will refresh all the rows in view 


     return row; 

    }