【问题标题】:Differentiate row in listview according to sqlite result根据sqlite结果区分listview中的行
【发布时间】:2013-12-17 01:09:14
【问题描述】:

在我的项目中,我需要从 web 服务获取消息并在 listview 中查看。我需要区分 listview 中的已读和未读消息。我检索了消息并将其存储在 sqlite 数据库中。如何区分已读和未读消息。而且列表视图也没有正确填充。而不是显示文本,而是在某些行中显示文本视图 ID。

这是我的代码,

      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>

【问题讨论】:

  • 您使用数据库中的哪个变量来区分已读和未读消息?你能把你看到的错误人群的图片放上来吗?
  • 我有名为 Status 的列。对于具有 0 值的行必须不同于具有 1 值的行...
  • @Lena Bru,我无法上传屏幕截图...而不是加载 textview 详细信息,而是加载那些 textviews 文本的资源 ID
  • 发布 layoutResourceId 的 xml,您将行膨胀到其中,让我看看可能是资源 ID 的原因
  • @Lena bru!!我已经粘贴了我的自定义行布局

标签: android sqlite listview load populate


【解决方案1】:
  @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;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多