0

我正在开发一个短信聊天应用程序。我想知道当活动打开时如何在列表视图底部添加最新的短信(发送或接收),就像股票短信应用程序或任何其他信使一样。

我尝试使用 ContentObserver。一旦 SMS 数据库发生一些变化,我就尝试使用新的数据集重新加载 ListView,但它不起作用。每次看到新的短信(发送接收)我都必须重新启动应用程序,以便重新加载列表。我希望这些新消息在我发送或接收它们后立即显示在聊天窗口中,就像我们通过 gtalk、msn 或任何 SMS 应用程序聊天的方式一样。

这是我的代码:-

public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
HashMap<Long, BodyType> bodies = new HashMap<Long, BodyType>();
private String name, number;
private ListView listView;
private Activity activity;
ThreadDataAdapter adapter;
Handler handler;
ArrayList<BodyType> items;
private Context context;
private static final String PHONE_NUMBER_SEPARATORS = " ()-./";
static HashMap<String, ContactInfo.ContactDetail> info = new HashMap<String, ContactInfo.ContactDetail>();

public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.chats);
    Bundle extras = getIntent().getExtras();
    activity = this;
    context = this;
    listView = getListView();
    if(extras != null)
    {
        number = extras.getString("address");
        ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
        if(nameInfo != null)
        {
            name = nameInfo.name;
        }
        else
        {
            name = number;
        }
    }
    TextView person = (TextView) findViewById(R.id.chat_person);
    person.setText(name);
    ImageButton callPerson = (ImageButton) findViewById(R.id.call_person);
    callPerson.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            popUpDialerAlert();
        }
    });

    buildMessageList();
    items = sortBodies(bodies);
    adapter = new ThreadDataAdapter(this, items);
    listView.setAdapter(adapter);
    listView.setStackFromBottom(true);

    getContentResolver().registerContentObserver(SMS_URI, true, new MyContentObserver(handler));
    //Intent intent = new Intent(this, RBSMSService.class);
    //startService(intent);
}

public void loadListView()
{
    items.clear();
    items = sortBodies(bodies);
    adapter = new ThreadDataAdapter(this, items);
    listView.setAdapter(adapter);
    listView.setStackFromBottom(true);
}

public void buildMessageList()
{
    Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
    startManagingCursor(cursor);
    while (cursor.moveToNext())
    {
        BodyType bodyInfo = new BodyType();
        String address = cursor.getString(cursor.getColumnIndex("address"));
        bodyInfo.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
        Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));
        bodyInfo.date = date;
        String type =  cursor.getString(cursor.getColumnIndexOrThrow("type"));
        if(type.equals("1"))
        {
            bodyInfo.type = "received";
        }
        else if(type.equals("2"))
        {
            bodyInfo.type = "sent";
        }
        else if(type.equals("3"))
        {
            bodyInfo.type = "draft";
        }

        String number = filterPhoneNumber(address);
        ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
        String personName = number;
        if(nameInfo != null)
        {
            personName = nameInfo.name;
        }
        else
        {
            personName = number;
        }
        if(personName.equals(name))
       {
           bodies.put(date, bodyInfo);
       }

    }
}

public void popUpDialerAlert()
{
    AlertDialog.Builder dialerAlert = new AlertDialog.Builder(this);
    dialerAlert.setTitle("Confirm");
    dialerAlert.setMessage("Call" + " " + name + "?");
    dialerAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("tel:" + number);
            Intent intent = new Intent((Intent.ACTION_CALL));
            intent.setData(uri);
            context.startActivity(intent);
        }
    });
    dialerAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
    dialerAlert.show();
}

public ArrayList<BodyType> sortBodies(HashMap<Long, BodyType> bodies)
{
    ArrayList<Long> dates = new ArrayList<Long>(bodies.keySet());
    Collections.sort(dates);
    ArrayList<BodyType> items = new ArrayList<BodyType>();
    for(Long date : dates)
    {
        for(Long key : bodies.keySet())
        {
            if(date.equals(key))
            {

                items.add(bodies.get(key));
            }
        }
    }
    return items;
}

public void printBodies(ArrayList<BodyType> items)
{
    for(BodyType bodyType : items)
    {
        log(bodyType.date.toString());
        log(bodyType.body);
        log(bodyType.type);
    }
}

static class BodyType
{
    public String type;
    public String body;
    public Long date;

}


public static ContactInfo.ContactDetail getContactsDetailWithNumber(String number)
{
    if(info.containsKey(number))
    {
        return info.get(number);
    }
    return null;
}

public static String filterPhoneNumber(String phoneNumber)
{
    if (phoneNumber == null)
    {
        return null;
    }

    int length = phoneNumber.length();
    StringBuilder builder = new StringBuilder(length);

    for (int i = 0; i < length; i++)
    {
        char character = phoneNumber.charAt(i);

        if (PHONE_NUMBER_SEPARATORS.indexOf(character) == -1)
        {
           builder.append(character);
        }
    }
    return builder.toString();
}

public class MyContentObserver extends ContentObserver
{

    public MyContentObserver(Handler handler)
    {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange)
    {
        bodies.clear();
        buildMessageList();
        items = sortBodies(bodies);
        runOnUiThread(new Runnable() {
            public void run() {
                loadListView();
            }
        });
        super.onChange(selfChange);
    }
}

public void log(String msg)
{
    Log.e("ThreadData", msg);
}
}

请帮忙!

谢谢

4

2 回答 2

1

您需要保留对列表(项目)的引用。

当内容提供者调用 OnChange 时,清除相同的列表(不要创建新对象,回收您的列表)并再次添加您的新邮件。由于此列表与您的适配器相关联,因此当您调用 notifyDataChanged 时它将更新您的列表视图。

必须回收相同的列表。

问候 Stéphane PS:请注意,您可以保留旧线程再次提出相同的问题。

于 2011-06-26T17:17:16.750 回答
0

因此,要在添加新项目时让列表滚动到底部,您应该将其设置为“转录模式”。但是,您必须重新启动应用程序才能显示新内容这一事实告诉我还有其他问题。

编辑:Stephane 是对的,收到通知时不要buildMessageList();再次调用,onChange只需更新适配器中的数据并调用notifyDatasetChanged()

于 2011-06-26T17:19:31.797 回答