使用Thread自动更新Listview

时间:2013-11-10 08:36:01

标签: java android mysql multithreading listview

我正在服务器中使用数据库MySQL实现聊天。现在,我想在Thread中更新我的自定义Listview,但我不知道怎么做?

继承我的代码:

chat_con_activity.java

    import android.app.Activity;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.os.SystemClock;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import com.example.classes.IpUpdater;
    import com.example.classes.Messages_con_adapter;
    import com.example.classes.Send_Message;
    import com.example.classes.View_Messages;
    import com.example.tabs.user_online;
    import com.example.wifivoip.R;

    public class chat_con_activity extends Activity implements Runnable{
        private ListView list = null;
        ArrayAdapter<String> la;
        Button send_btn;
        EditText chat_msg;
        String user;
        String cTo;
        String[] names;
        String[] namesfrm;
        String[] msgs;
        String[] datez;
        int stop = 0;
        Context cn = this;
        Thread x;
        int count;
        Messages_con_adapter mca;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.send_chat);
            send_btn = (Button) findViewById(R.id.chat_btn_send);
            chat_msg = (EditText) findViewById(R.id.chat_text_send);
            user = getIntent().getStringExtra("cFrom");
            cTo = getIntent().getStringExtra("cTo");



            list = (ListView) findViewById(R.id.chat_list_view);

            Toast.makeText(getApplicationContext(), ""+user+" "+cTo, Toast.LENGTH_SHORT).show();
            display(); 

            x = new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while(true){
                        try{
                            //Thread.sleep(1000);

                            View_Messages vm = new View_Messages(user, cTo);
                            int cnt = vm.getIfNew();
                            System.err.println(""+cnt+" "+count);
                            if(cnt != count){
                                System.err.println(""+cnt+" "+count);


                                names = vm.getAllNamesTo();
                                namesfrm = vm.getAllNamesFrom();
                                msgs = vm.getAllMsgs();
                                datez = vm.getAllDates();
                                count = vm.getIfNew();

                                list.setAdapter(la);
                                list.setSelection(list.getAdapter().getCount() - 1);

                            }
                            if(stop == 1){
                                break;
                            }

                        }catch (Exception e) {
                            // TODO: handle exception
                        }
                    }
                }
            });x.start();

            send_btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String msg = chat_msg.getText().toString().trim();
                    if(!msg.equals("")){
                        new Send_Message().sendMessage(user, cTo, msg);
                        display();
                        chat_msg.setText("");
                    }else{
                        Toast.makeText(getApplicationContext(), "No message to send!!", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }


        @Override
        public void onDestroy(){
            super.onDestroy();
            stop = 1;
        }



        public void display(){
            View_Messages vm = new View_Messages(user, cTo);
            names = vm.getAllNamesTo();
            namesfrm = vm.getAllNamesFrom();
            msgs = vm.getAllMsgs();
            datez = vm.getAllDates();
            count = vm.getIfNew();

            list.setDivider(null);

            la = new Messages_con_adapter(this, user, names, namesfrm,msgs,datez,cTo);
            list.setAdapter(la);
            list.setSelection(list.getAdapter().getCount() - 1);
        }

我的适配器是: Message_con_adapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Messages_con_adapter extends ArrayAdapter<String>{
    String user;
    String[] name;
    String[] namefrm;
    String[] msgs;
    String[] dates;
    Context context;
    View rowView;


    public Messages_con_adapter(Context context,String user, String[] name,String[] namefrm, String[] msgs,String[] dates,String To) {
        super(context, R.layout.chat_con,name);

        this.user = user;
        this.context = context;
        this.msgs = msgs;
        this.name = name;
        this.namefrm = namefrm;
        this.dates = dates;
        this.To = To;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);




        if(namefrm[position].equals(user)){
            rowView = inflater.inflate(R.layout.chat_con_home, parent, false);
            TextView dateTimeHome = (TextView) rowView.findViewById(R.id.time_c_home);
            TextView c_home = (TextView) rowView.findViewById(R.id.c_u_home);
            TextView c_home_m = (TextView) rowView.findViewById(R.id.c_u_home_m);
            dateTimeHome.setText(dates[position]);
            c_home.setText(user);
            c_home_m.setText(msgs[position]);


        }else
        {
            rowView = inflater.inflate(R.layout.chat_con, parent, false);
            TextView dateTimeAway = (TextView) rowView.findViewById(R.id.time_c_away);
            TextView c_away = (TextView) rowView.findViewById(R.id.c_u_away);
            TextView c_away_m = (TextView) rowView.findViewById(R.id.c_u_away_m);
            dateTimeAway.setText(dates[position]);
            c_away.setText(namefrm[position]);
            c_away_m.setText(msgs[position]);

        }

        return rowView;
    }
先生,不知道吗?请帮忙。 =(

提前致谢。

4 个答案:

答案 0 :(得分:1)

永远不要从非UI线程更新列表视图,而是考虑使用处理程序。

答案 1 :(得分:1)

您可以更新列表视图,

首先,您需要更新列表视图,这取决于您。

您可以在listview中使用onscroll侦听器来更新listview。

现在的问题是如何在自定义列表视图中更新数据,

当需要更新数据时,您可以调用此方法,

public void updateDate(String user, String[] name,String[] namefrm, String[] msgs,String[] dates,String To) {
    super(context, R.layout.chat_con,name);

    this.user = user;
    this.context = context;
    this.msgs = msgs;
    this.name = name;
    this.namefrm = namefrm;
    this.dates = dates;
    this.To = To;
}

此方法使用ur previos适配器对象调用,然后调用

adapterObject.notifyDataSetChanged();

您的列表视图已更新。

答案 2 :(得分:0)

我有更好的解决方案。您可以绑定listView和您的数据库,如果数据库刷新新数据,Android将自动更新您的ListView,因此您的工作将只是从服务器接收消息并放入数据库。

为此,您需要实现ContentProvider - 它是SQLite数据库的包装器。之后你实现了CursorAdapter - 它与ArrayAdapter有点不同。最后一步是通过ContentResolver调用ContentProvider,它返回一个游标,并通知你的适配器有关表中的更改。有关详细信息,请参阅http://www.vogella.com/articles/AndroidSQLite/article.html#sqliteoverview_listviews

答案 3 :(得分:0)

要更新与UI相关的组件,请使用 AsyncTask 来执行此操作。
它非常简单,也是最佳实践。
这是doc
从您提供的代码中,使用您在线程的run()方法中执行的相同代码 AsynTask's doInBackgrond()方法。
编辑
由于您是Android新手,您可能需要查看此处的example