如何让Android ListView自动刷新?

时间:2014-03-21 06:27:03

标签: android android-listview refresh simplecursortreeadapter

在我的应用程序中,我有一个ListView。使用SimpleCursorAdapter在ListView中填充数据。我希望每当数据库发生变化或单击按钮时自动刷新ListView。我已经尝试过adapter.notifyDataSetChanged()但它没有效率。我无法找到一个好的解决方案。我希望只要用户在EditText中输入值,然后按下发送按钮并将输入的数据保存在SQLite数据库中,就会刷新ListView。这是我的代码:

public class chatCursor extends Activity {

static MyListAdapter adapter;
ArrayList<String> item_id;
ArrayList<String> item_phone_num;
ArrayList<String> item_msg_body;
ArrayList<String> item_time;
ArrayList<String> item_flag;
ArrayList<String> items;
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 = "";
ListView lv_chat;
String[] from = new String[]{"Message_body","Time"};
int[] toIDs = new int[]{R.id.msg_body,R.id.time};

@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, chatCursor.this);
    Toast.makeText(getApplicationContext(), contact_no, Toast.LENGTH_LONG).show();
    manager = new DbManager(this);
    Cursor = manager.Return_SMS(contact_for_chat);

    c = Calendar.getInstance();
    sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
    time = sdf.format(c.getTime());


    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());
    findViews();
    showList();
    //setActionBar();

    btn_send.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            SendSMS();
            showList();
        }
    });

    lv_chat.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
            int itemId = Integer.valueOf(String.valueOf(position));

        }});
}

private void showList() {

    showEvents(Cursor);
    adapter = new MyListAdapter(this,R.layout.activity_chat, 
            Cursor, 
            from, 
            toIDs);

    lv_chat.setAdapter(adapter);
    adapter.notifyDataSetChanged();

}

private void findViews() {
    et_chat = (EditText)findViewById(R.id.et_chat);
    btn_send = (Button)findViewById(R.id.button1);
    lv_chat = (ListView)findViewById(R.id.list);
    lv_chat.setDivider(this.getResources().getDrawable(android.R.color.transparent));

}

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);

    if(message_body.length()>0)
    {
        et_chat.setText("");
    }
    showList();
    Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();

private void showEvents(Cursor cursor) {

    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 SimpleCursorAdapter {

    Cursor myCursor;
    Context myContext;

    public MyListAdapter(Context context, int layout,
            Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        myCursor = c;
        myContext = context;
    }

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

    public Object getItem(int position) {
        return item_msg_body.get(position);
    }

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

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

        View v = arg1;
        ViewHolder holder = null;
        if (v == null) {

            LayoutInflater 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"))
        {
        RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);

        RelativeLayout.LayoutParams dateparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        dateparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        dateparams.addRule(RelativeLayout.BELOW, R.id.msg_body);

            holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);
            holder.tv_sms_body.setLayoutParams(params);
            holder.tv_time.setLayoutParams(dateparams);
        }
        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;

}

}

任何帮助都将不胜感激。谢谢提前

1 个答案:

答案 0 :(得分:0)

调用adapter.notifydatasetchanged()方法刷新listview