使用listadapter刷新ListView?

时间:2011-07-21 09:02:18

标签: android listadapter

我想在每次更改基础数据时刷新ListView。请参阅以下内容 例如:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mListUsers = getUsers();
lvList = (ListView) findViewById(R.id.event_list);
lvList.setAdapter(new ListAdapter(mContext, R.id.event_list, mListUsers));

    btn_next=(Button)findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
 SimpleDateFormat postFormater = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH); 
 String newDateStr = lbl_date.getText().toString();         
 Calendar c = Calendar.getInstance();
 try {
 c.setTime(postFormater.parse(newDateStr));
 } catch (ParseException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
}
c.add(Calendar.DATE, 1);  // number of days to add
newDateStr = postFormater.format(c.getTime());
lbl_date.setText(newDateStr.toString());

mListUsers = getUsers();
lvList = (ListView) findViewById(R.id.event_list);
lvList.setAdapter(new ListAdapter(mContext, R.id.event_list, mListUsers));
}
});

}

public ArrayList<main_list_item> getUsers(){                
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
try {
            dbAdapter.createDataBase();
        } catch (IOException e) {
            Log.i("*** select ",e.getMessage());
        }
        dbAdapter.openDataBase();

        SimpleDateFormat curFormater1 = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = curFormater1.format(new Date(lbl_date.getText().toString()));
        String query;       
        if(all==1){
            query="SELECT id,dateS, dateE, (select eventname from tbEvent_name where tbEvent_name.id=eventId)as event FROM tbTransaction";
        }
        else{
            query="SELECT id,dateS, dateE, (select eventname from tbEvent_name where tbEvent_name.id=eventId)as event FROM tbTransaction WHERE SUBSTR(sDate,1,10)='"+dateString+"'";
        }

        ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
        dbAdapter.close();

        SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
        Date dateObj = null;

        ArrayList<main_list_item> usersList = new ArrayList<main_list_item>();
        for (int i = 0; i < stringList.size(); i++) {
            ArrayList<String> list = stringList.get(i);
            main_list_item user = new main_list_item();
            try {
                user.id=Integer.parseInt(list.get(0));
                user.event= list.get(3);

        String newDateStr1 = postFormater1.format(dateObj);

                user.dateS=list.get(1);
                user.dateE=list.get(2);

            } catch (Exception e) {
                e.printStackTrace();
            }
            usersList.add(user);
        }
        return usersList;
    }

 // ***ListAdapter***
    private class ListAdapter extends ArrayAdapter<main_list_item> {  // --CloneChangeRequired
        private ArrayList<main_list_item> mList;  // --CloneChangeRequired
        private Context mContext;

        public ListAdapter(Context context, int textViewResourceId,ArrayList<main_list_item> list) { // --CloneChangeRequired
            super(context, textViewResourceId, list);
            this.mList = list;
            this.mContext = context;
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View view = convertView;
            try{
            if (view == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.main_list_item, null);   // --CloneChangeRequired(list_item)
            }
            final main_list_item listItem = mList.get(position);    // --CloneChangeRequired                
            if (listItem != null) {
                // setting list_item views                      
                ((TextView) view.findViewById(R.id.lbl_id) ).setText( listItem.getId()+"");

                ((TextView) view.findViewById(R.id.lbl_event) ).setText( listItem.getEvent()+"");
                ((TextView) view.findViewById(R.id.lbl_sdate) ).setText( listItem.getsDate()+"");
                ((TextView) view.findViewById(R.id.lbl_edate) ).setText( listItem.geteDate()+"");

            }}catch(Exception e){
                e.printStackTrace();
            }

            return view;
        }       
    }

这是正确的方法吗?

在这段代码中,我想在'btn_next'点击时刷新listview。

而且我想分开两个部分,放置标题。

请给我指导..

提前致谢

Uncaught handler: thread main exiting due to uncaught exception
java.lang.NullPointerException
at com.apt.eTrack.ETrackActivity$4.onClick(ETrackActivity.java:153)
at android.view.View.performClick(View.java:2344)
at android.view.View.onTouchEvent(View.java:4133)
at android.widget.TextView.onTouchEvent(TextView.java:6510)
at android.view.View.dispatchTouchEvent(View.java:3672)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at    com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
Unable to open stack trace file '/data/anr/traces.txt': Permission denied

1 个答案:

答案 0 :(得分:1)

static ListAdapter mListAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mListUsers = getUsers();
    mListAdapter=new ListAdapter(mContext, R.id.event_list, mListUsers);
    lvList = (ListView) findViewById(R.id.event_list);
    lvList.setAdapter(mListAdapter);

    btn_next=(Button)findViewById(R.id.btn_next);
    btn_next.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SimpleDateFormat postFormater = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH); 
            String newDateStr = lbl_date.getText().toString();         
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(postFormater.parse(newDateStr));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            c.add(Calendar.DATE, 1);  // number of days to add
            newDateStr = postFormater.format(c.getTime());
            lbl_date.setText(newDateStr.toString());

            mListUsers = getUsers();
            mListAdapter.notifyDataSetChanged();
        }
    });
}