热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

避免重复收件箱的Listview项目-AvoidrepeatingListviewitemsforinbox

Iamdevelopingamessagingapplicationwithinboxactivityandconversationactivity.ForthatIam

I am developing a messaging application with inbox activity and conversation activity.For that I am using SQL database to save all received and sent messages and appending them to the listview of inbox activity. I am getting data from database to append. I don't want to append all messages with same number but with the latest one because other messages are going to the conversation. Help me with this. I've searched it but couldn't find it here and one more thing, I am just a learner. Any inputs will be appreciated!

我正在开发一个带有收件箱活动和对话活动的消息传递应用程序。为此,我使用SQL数据库保存所有收到和发送的消息,并将它们附加到收件箱活动的列表视图中。我从数据库中获取数据以追加。我不想附加所有具有相同号码但具有最新消息的消息,因为其他消息将进入对话。帮助我。我已经搜索了它,但在这里找不到它还有一件事,我只是一个学习者。任何输入将不胜感激!

I want this

我要这个

enter image description here

and Code for inbox activity is

和收件箱活动的代码是

public class ReceiveSMSActivity extends Activity {
DBAdapter myDb;
private List MyMessages = new ArrayList();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    openDB();

    getResultsAndAppend();
    populateListView();
}

private void getResultsAndAppend() {

    Cursor cursor = myDb.getAllRows();
    if (cursor.moveToFirst()) {
        do {
            // Process the data:
            // String name = cursor.getString(DBAdapter.COL_NAME);
            String number = cursor.getString(DBAdapter.COL_NUMBER);
            String message = cursor.getString(DBAdapter.COL_MESSAGE);

            MyMessages.add(new Message(message, number, true));

        } while (cursor.moveToNext());
    }

}

private void populateListView() {

    ArrayAdapter adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.listViewInbox);
    list.setAdapter(adapter);
}

private class MyListAdapter extends ArrayAdapter {
    public MyListAdapter() {
        super(ReceiveSMSActivity.this, R.layout.inbox_list_item_layout,
                MyMessages);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Make sure we have a view to work with (may have been given null)
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(
                    R.layout.inbox_list_item_layout, parent, false);
        }

        // Find the Message to work with.
        Message currentMessage = MyMessages.get(position);

        // Message:
        TextView message = (TextView) itemView.findViewById(R.id.tvMessage);
        message.setText(currentMessage.getMessage());

        // Number:
        TextView number = (TextView) itemView.findViewById(R.id.tvNumber);
        number.setText(currentMessage.getNumber());

        return itemView;
    }

}


@Override
protected void onDestroy() {
    super.onDestroy();
    closeDB();
}

private void openDB() {
    myDb = new DBAdapter(this);
    myDb.open();

}

private void closeDB() {
    myDb.close();
}

}

and DBAdapter() is

和DBAdapter()是

public class DBAdapter {


private static final String TAG = "DBAdapter";

// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;


public static final String KEY_NAME = "name";
public static final String KEY_NUMBER = "number";
public static final String KEY_MESSAGE = "message";
public static final String KEY_ISSELF= "self";


public static final int COL_NAME = 1;
public static final int COL_NUMBER = 2;
public static final int COL_MESSAGE = 3;
public static final int COL_ISSELF = 4;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_NUMBER, KEY_MESSAGE, KEY_ISSELF};


public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";

public static final int DATABASE_VERSION = 1;   

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "


        + KEY_NAME + " text not null, "
        + KEY_NUMBER + " string not null, "
        + KEY_MESSAGE + " string not null,"
        + KEY_ISSELF + " string not null"
        + ");";


private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;



public DBAdapter(Context ctx) {
    this.cOntext= ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to the database.
public long insertRow(String name, String number, String message, String isself) {

    // Create row's data:
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_NUMBER, number);
    initialValues.put(KEY_MESSAGE, message);
    initialValues.put(KEY_ISSELF, isself);

    // Insert it into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
    Cursor c = getAllRows();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, String number, String message, String isself) {
    String where = KEY_ROWID + "=" + rowId;


    // Create row's data:
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_NAME, name);
    newValues.put(KEY_NUMBER, number);
    newValues.put(KEY_MESSAGE, message);
    newValues.put(KEY_ISSELF, isself);

    // Insert it into the database.
    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}



/////////////////////////////////////////////////////////////////////
//  Private Helper Classes:
/////////////////////////////////////////////////////////////////////

/**
 * Private class which handles database creation and upgrading.
 * Used to handle low-level database access.
 */
private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
}

}

1 个解决方案

#1


A solution could be to parse all your message in a hashmap which will have the phone number as key and an model 'PeopleTexts', this model will have a list of message from the same people (number), number of texts, etc..

一个解决方案可能是在一个散列图中解析你的所有消息,这个散列图将把电话号码作为密钥和一个模型'PeopleTexts',这个模型将有一个来自同一个人(数字),文本数量等的消息列表。

After parsing it, you will simply have to iterate over the hashmap and not the db rows.

解析之后,您只需要迭代hashmap而不是db行。

This the logic. Now some snippets of code

这个逻辑。现在是一些代码片段

public class PeopleTexts {

   private String phoneNumber;
   private String name;
   private ArrayList peopleTexts;

   // add setters and getters...

   public int textCount() {
      return peopleTexts.size();
   }

   public void addMessage(Message message) {
      this.peopleTexts.add(message);
   }
}

Now you just have to do in your do...while()

现在你只需做你的... while()

//My Messages is now a Hashmap...

//我的消息现在是一个Hashmap ...

if (cursor.moveToFirst()) {
    do {
         // Process the data:
         // String name = cursor.getString(DBAdapter.COL_NAME);
         String number = cursor.getString(DBAdapter.COL_NUMBER);
         String message = cursor.getString(DBAdapter.COL_MESSAGE);

        if(MyMessages.containsKey(number)) {
          MyMessages.put(number, new PeopleTexts(name, number);
        }

        MyMessages.get(number).addMessage(new Message(number, message);

    } while (cursor.moveToNext());
}

I let you do the rest.

我让你做其余的事。


推荐阅读
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • mysql-cluster集群sql节点高可用keepalived的故障处理过程
    本文描述了mysql-cluster集群sql节点高可用keepalived的故障处理过程,包括故障发生时间、故障描述、故障分析等内容。根据keepalived的日志分析,发现bogus VRRP packet received on eth0 !!!等错误信息,进而导致vip地址失效,使得mysql-cluster的api无法访问。针对这个问题,本文提供了相应的解决方案。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
author-avatar
云下拾雨
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有