如何自动刷新ListView?

时间:2014-03-15 07:41:31

标签: android listview sms refresh

在我的应用中,我使用已收到并ListView填充SMS。这是代码:

public class ChatActivity extends ListActivity {

private MyListAdapter adapter;
ArrayList<String> item_id = new ArrayList<String>();
ArrayList<String> item_phone_num = new ArrayList<String>();
ArrayList<String> item_msg_body = new ArrayList<String>();
ArrayList<String> item_time = new ArrayList<String>();
ArrayList<String> item_flag = new ArrayList<String>();
ArrayList<String> items = new ArrayList<String>();
private Button btn_send;
DbManager manager;
Cursor Cursor;
ViewHolder holder12;
String contact_for_chat;
String contact_no;
String message_body = "";
Calendar c;
SimpleDateFormat sdf;
String time;
EditText et_chat;
String flag;
String msg = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    Bundle bundle = getIntent().getExtras();
    contact_for_chat = bundle.getString("contact_name");
    contact_for_chat = contact_for_chat.replace(" ", "");
    contact_no = Util.getContactNumber(contact_for_chat, ChatActivity.this);
    Toast.makeText(getApplicationContext(), contact_no, Toast.LENGTH_LONG).show();
    final ViewHolder holder = new ViewHolder();
    manager = new DbManager(this);
    Cursor = manager.Return_SMS(contact_for_chat);
    showEvents(Cursor);

    c = Calendar.getInstance();
    sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
    time = sdf.format(c.getTime());
    setActionBar();
    findViewsById();
    adapter = new MyListAdapter(this);
    adapter.notifyDataSetChanged();
    setListAdapter(adapter);

    btn_send = (Button) findViewById(R.id.button1);
    btn_send.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SendSMS();
            refreshCursor();
        }
    });
}

protected void SendSMS() {
    SmsManager sms_manager = SmsManager.getDefault();
    message_body = et_chat.getText().toString();
    ArrayList<String> parts = sms_manager.divideMessage(message_body);
    sms_manager.sendMultipartTextMessage(contact_no, null, parts, null, null);
    flag = "1";
    manager.Insert_sms_data(time, contact_for_chat, message_body,flag);
    updateUI();
    msg+= "SMS to :" + contact_for_chat + " \n";
    msg += "having number:" + contact_no + " \n";
    msg += "as" +message_body + " \n";
    msg += "at"+ time + " \n";
    Toast.makeText(getApplicationContext(), ""+msg , Toast.LENGTH_LONG).show();

}

private void setActionBar() {
    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.actionbar_chat, null);
    TextView tv_chat = (TextView)mCustomView.findViewById(R.id.title_text);
    tv_chat.setText(contact_for_chat);
    ColorDrawable colorDaawable = new ColorDrawable(Color.parseColor("#CFCFC4"));
    mActionBar.setBackgroundDrawable(colorDaawable);
    mActionBar.setLogo(R.drawable.ic_launcher);
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
}

private void updateUI() {
    et_chat.setText("");
    refreshCursor();
    adapter.notifyDataSetChanged();
}


private void findViewsById() {
    et_chat = (EditText)findViewById(R.id.et_chat);
    btn_send = (Button)findViewById(R.id.button1);
}

private void showEvents(Cursor cursor) {

    item_id = new ArrayList<String>(cursor.getCount());
    item_phone_num = new ArrayList<String>(cursor.getCount());
    item_msg_body = new ArrayList<String>(cursor.getCount());
    item_time = new ArrayList<String>(cursor.getCount());
    item_flag = new ArrayList<String>(cursor.getCount());
    int i=0;
    while (cursor.moveToNext()) {
        item_id.add(i+"");
        item_time.add(cursor.getString(1));
        item_msg_body.add(cursor.getString(3));
        item_phone_num.add(cursor.getString(2));
        item_flag.add(cursor.getString(4));
        i++;
    }

  }
public class MyListAdapter extends BaseAdapter {
    Context con;
    private LayoutInflater layoutinf;
    ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
    ArrayList<String> items_ = new ArrayList<String>();

    public MyListAdapter(ChatActivity context) {
        con = context;
    }

    public int getCount() {
        return item_id.size();
    }

    public Object getItem(int position) {
        return item_id.size();
    }

    public long getItemId(int position) {
        return item_id.get(position).hashCode();
    }

    public View getView(final int position, View arg1, ViewGroup arg2) {

        View v = arg1;
        ViewHolder holder = null;
        if (v == null) {
            layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutinf.inflate(R.layout.row_chat, null);
            holder = new ViewHolder();

            holder.tv_contact = (TextView) v.findViewById(R.id.phone_num);
            holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body);
            holder.tv_time = (TextView) v.findViewById(R.id.time);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        if(item_flag.get(position).equals("1"))
        {
            holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);

        }
                    else if(item_flag.get(position).equals("0"))
                    {
                        holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow);

                    }

        holder.tv_contact.setText("" + item_phone_num.get(position));
        holder.tv_sms_body.setText(item_msg_body.get(position));
        holder.tv_time.setText(item_time.get(position));

        return v;
    }
}

public class ViewHolder {

    private TextView tv_contact;
    private TextView tv_sms_body;
    private TextView tv_time;

}
private void refreshCursor() {

    manager.open();
    Cursor = manager.Return_SMS(contact_for_chat);
    setListAdapter(adapter);
    manager.close();
}

我希望收到或发送SMS后,ListView应自动更新。我试过用它 adapter.notifyDataSetChanged(); 但它不起作用。如果ListView发送或接收,我如何更新我的应用SMS。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

notifyDataSetChanged告诉listview重新加载其数据。它仍然会调用适配器函数getView和getItemCount来执行此操作。那些需要返回正确的新数据。但是,您不会更新他们返回的数据 - 您只需在onCreate中设置item_id数组。由于数组永远不会更改,因此列表视图永远不会更新。

答案 1 :(得分:0)

使用此代码:

adapter.notifyDataSetChanged();