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

データは実際に変更されましたか? これ以上コードを見なければ、私は「いいえ」だと思います。

于 2012-04-26T22:54:44.500 に答える
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 に答える