0

我在第一个活动中有一个列表视图,我notifyDataSetChanged();

onResume()

onStart()

但什么也没发生。

当我回到列表视图的活动时,我想重新绘制(刷新)列表视图

代码:

package pit.opensource.customizedlist;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewCustomizedAdapter1Activity extends Activity{

    private ArrayList<Order> m_orders = null;
    private OrderAdapter m_adapter;
    private ListView list;
    private Button btnAdd;
    private Runnable viewOrders;
    private ProgressDialog m_ProgressDialog;
    DBHelper db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) this.findViewById(R.id.list);
        btnAdd = (Button) this.findViewById(R.id.btnAdd);

        db = new DBHelper(getApplicationContext());

        //  db.insertOrder(u);
        btnAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(),Activity1.class);
                startActivity(i);

            }
        });
        m_orders = new ArrayList<Order>();
        m_adapter = new OrderAdapter(getApplicationContext(), R.layout.row, m_orders); // data

        list.setAdapter(m_adapter);

        viewOrders = new Runnable(){ // inner class
            @Override
            public void run() {
                getOrders();
            }
        };

        // Creating a new thread to perform a long-running opertaions, rather than making it in the UI thread
        Thread thread =  new Thread(viewOrders);
        thread.start();

        // Showing a simple progress dialog 
        m_ProgressDialog = ProgressDialog.show(ListViewCustomizedAdapter1Activity.this,    
                "Please wait...", 
                "Retrieving data ...", 
                true);

    }
    @Override
    public void onResume(){
        super.onResume();
        m_adapter.notifyDataSetChanged();
    }
    @Override
    public void onStart(){
        super.onResume();
        // to re-bind the list after adding
        m_adapter.notifyDataSetChanged();
    }
    private Runnable r2 = new Runnable(){

        @Override
        public void run() {
            if(m_orders != null && m_orders.size() > 0){
                for(int i=0;i<m_orders.size();i++)
                    m_adapter.add(m_orders.get(i));
            }
            m_ProgressDialog.dismiss();
            //notifyDataSetChanged() : to refresh the DataSet to re-draw the ListView
            m_adapter.notifyDataSetChanged();
        }
    };

    // Getting the data, here we fill the data manually and simulate the Network operations
    private void getOrders(){
        try{
            m_orders = new ArrayList<Order>();
            /*
            Order o1 = new Order();
            o1.orderName = "SF services";
            o1.orderStatus = "Pending";
            o1.id=1;
            o1.imgName = "a";

            Order o2 = new Order();
            o2.orderName = "SF Advertisement";
            o2.orderStatus = "Completed";
            o2.id = 2;
            o2.imgName = "b";

            Order o3 = new Order();
            o3.orderName = "SF development";
            o3.orderStatus = "Completed";
            o3.id = 3;
            o3.imgName = "c1";      

            Order o4 = new Order();
            o4.orderName = "SF training";
            o4.orderStatus = "Completed";
            o4.id = 4;
            o4.imgName = "c";

            m_orders.add(o1);
            m_orders.add(o2);
            m_orders.add(o3);
            m_orders.add(o4);*/



            //fill from database
            m_orders = db.getAllOrders();

            Thread.sleep(5000);

            Log.i("ARRAY", ""+ m_orders.size());
        } catch (Exception e) {
            Log.e("BACKGROUND_PROC", e.getMessage());
        }
        runOnUiThread(r2);
    }

    // Here we create a customized ArrayAdapter -----------------------------------------------------
    private class OrderAdapter extends ArrayAdapter<Order> {

        private ArrayList<Order> items;
        Context context;

        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
            super(context, textViewResourceId, items);
            this.items = items;
            this.context = context;
        }
        @Override
        public View getView( int position, 
                View convertView // (ListItem)Row = textView ... etc
                , ViewGroup parent) {

            View v = convertView;
            if (v == null) {
                // LayoutInflater: to convert the 'layout XML file' to a corresponding Java 'View' object
                // getSystemService(): A system level service to retrieve the LayoutInflater
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            Order o = items.get(position);
            if (o != null) {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                Button btn = (Button) v.findViewById(R.id.btnMore);
                ImageView img = (ImageView) v.findViewById(R.id.icon);

                tt.setText("Name: "+o.orderName);                            
                bt.setText("Status: "+ o.orderStatus);
                Log.d("Adpdter status",o.orderStatus);
                // To get Image ID using the image name ---------
                try{
                    if(o.imgName!=null){
                        int resID = getResources().getIdentifier(o.imgName, "drawable", getPackageName());
                        img.setImageDrawable(getResources().getDrawable(resID));
                    }else{
                        img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
                    }
                }catch(Exception ex){}

                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        //btn.en
                        Intent i = new Intent(getApplicationContext(),Activity1.class);
                        //  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        //  startActivity(i);

                    }
                });
            }
            return v;
        }
    }
}
4

2 回答 2

0

呼叫notifyDataSetChanged()后立即呼叫db.update(),db.insert()等。

加法哦,看,代码!:)

这条线正确吗?

                m_adapter.add(m_orders.get(i));

或者你的意思是添加项目m_orders?适配器已经链接到m_orders并将反映数组中的任何内容。

如果我们改为Runnable r2

@Override
public void run() {
    m_orders.add(new Order("Pizza"));
    m_orders.add(new Order("Beer"));
    m_orders.add(new Order("Other American Health Food"));
    m_adapter.notifyDataSetChanged();
}

这会反映变化吗?(我对您的 Order 课程有些随意)。

于 2012-04-26T22:56:42.143 回答
0

数据是否真的发生了变化?没有看到更多代码,我猜“不”。

于 2012-04-26T22:54:44.500 回答