将新数据添加到sqlite数据库后,simpleadapter的listview没有刷新

时间:2013-06-06 11:04:15

标签: android eclipse android-listview refresh simpleadapter

我的listview存在问题,当我向数据库添加任务时,我需要使用这个新添加的任务更新我的listview ....

我是android和Eclipse的新手......

这是我的Main代码(显示ListView

public class Main extends ListActivity {

    Button newCat;
    TextView todaytaskId;
    ResultDatabase controller5 = new ResultDatabase(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        newCat = (Button) findViewById(R.id.bNewCat);
        newCat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent nextScreen = new Intent(getApplicationContext(),
                        CategoryList.class);

                startActivity(nextScreen);
            }
        });

        ArrayList<HashMap<String, String>> todaytaskList = controller5
                .getTodayTasks();

        if (todaytaskList.size() != 0) {
            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    todaytaskId = (TextView) view
                            .findViewById(R.id.todaytaskId);
                    String valtaskId = todaytaskId.getText().toString();
                    Intent objIndent = new Intent(getApplicationContext(),
                            DelayTask.class);
                    objIndent.putExtra("todaytaskId", valtaskId);
                    startActivity(objIndent);
                }
            });

            SimpleAdapter adapter = new SimpleAdapter(Main.this, todaytaskList,
                    R.layout.view_today_task, new String[] { "taskId",
                            "taskName", "taskTime" }, new int[] {
                            R.id.todaytaskId, R.id.todaytasktv,
                            R.id.todaytasktimetv});
            lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        }

    }


}

这是我从ResultDatabase类

中选择任务的代码
public ArrayList<HashMap<String, String>> getTodayTasks() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM tasks where taskDate = date('now') AND taskDone = 'No'";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("taskId", cursor.getString(0));
            map.put("taskName", cursor.getString(1));
            map.put("taskTime", cursor.getString(3));
            // map.put("taskDate", cursor.getString(4));
            wordList.add(map);
        } while (cursor.moveToNext());
    }
    return wordList;

}

1 个答案:

答案 0 :(得分:0)

修改适配器上连接的数据集后,必须调用 notifyDataSetChanged 以通知适配器使用新数据更新视图。您还必须在Cursor上调用requery以使用新数据更新游标。为了澄清你应该在notifyDataSetChanged之前调用requery。

检查下面的示例代码

 public class WeatherAppActivity extends ListActivity {

 Button buton;
 ItemsAdapter lista;

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

List<String> initialList = new ArrayList<String>();
initialList.add("Bucuresti");
initialList.add("Sibiu");

lista=new ItemsAdapter(this, initialList);
buton=(Button)findViewById(R.id.button1);
buton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
         lista.add(""+System.currentTimeMillis()); // e chiar getText()
         lista.notifyDataSetChanged();

    }
});

setListAdapter(lista);

}

 class ItemsAdapter extends ArrayAdapter<String> {

public ItemsAdapter(Context context, List<String> list) {
    super(context, R.layout.lista, list);
}

@Override
public View getView(final int position, View row, final ViewGroup parent) {
    final String item = getItem(position);

    ItemWrapper wrapper = null;
    if (row == null) {
        row = getLayoutInflater().inflate(R.layout.lista, parent, false);
        wrapper = new ItemWrapper(row);

        row.setTag(wrapper);
    } else {
        wrapper = (ItemWrapper) row.getTag();
    }
    wrapper.refreshData(item);

    return row;
}

class ItemWrapper {

    TextView text;

    public ItemWrapper(View row) {
        text = (TextView) row.findViewById(R.id.elementLista);
    }

    public void refreshData(String item) {
        text.setText(item);
    }

}
}    

}