如何将“onListItemClick”限制为时间范围?

时间:2014-06-05 10:18:03

标签: java android sqlite listview android-listview

我的项目中有一个ListView,该列表显示数据库中的数据(sqlite)。

public void refreshDisplay() {
    ArrayAdapter<MyStory> adapter = new StoryAdapter(this, story);
setListAdapter(adapter);
}


和onListItemClick代码:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    MyStory story = stories.get(position);

    Intent next = new Intent(this, ShowStory.class);
    next.putExtra("thisstory", story);
    startActivity(next);
}

现在,我是否可以仅在特定日期设置启用/禁用ListView项目?例如,如果系统日期是(例如)2014年6月29日,则只有一个故事可读(可点击)?
是否需要在数据库中为每个故事添加一个Date列?
MainActivity完整代码是:

public class MainActivity extends ListActivity {
DBAdapter db;
List<MyStory> stories;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    list = getListView();
    db = new DBAdapter(getBaseContext());
    db.open();
    stories = db.getAllContacts();

    if (stories.size() == 0) {

        String destPath = "/data/data/" + getPackageName() + "/databases";

        try {
            CopyDB(getBaseContext().getAssets().open("StoryDB"),
                    new FileOutputStream(destPath + "/stories"));
            Log.i(DBAdapter.TAG, "DB Copy-OK");

            stories = db.getAllContacts();

        refreshDisplay(); 
            Log.i(DBAdapter.TAG, storiess.size() + "= stories count");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } else {
        refreshDisplay();
    }



@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Mystory story = stories.get(position);

    Intent next = new Intent(this, ShowStory.class);
    next.putExtra("thisstory", story);
    startActivity(next);
}


public void CopyDB(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    // ---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}



public void refreshDisplay() {
    Log.i(DBAdapter.TAG, stories.size() + "= storeis count");

    ArrayAdapter<MyStory> adapter = new StoryAdapter(this, stories);

    setListAdapter(adapter);
}

}

由于

1 个答案:

答案 0 :(得分:0)

是的,您可以比较onItemClick

中的日期
  @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(Date compare)
{
 // do nothing
}
else
{
MyStory story = stories.get(position);

Intent next = new Intent(this, ShowStory.class);
next.putExtra("thisstory", story);
startActivity(next);
}
}