Java 如何限制;onListItemClick";到时间范围?

Java 如何限制;onListItemClick";到时间范围?,java,android,sqlite,listview,android-listview,Java,Android,Sqlite,Listview,Android Listview,我的项目中有一个,该列表显示来自数据库(sqlite)的数据。 现在,我可以只在特定日期设置启用/禁用ListView项目吗?例如,如果系统日期为(例如)2014年6月29日,则只有一个故事是可读的(可点击的)?。 是否需要为每个故事在数据库中添加日期列? 主活动完成代码为: public class MainActivity extends ListActivity { DBAdapter db; List<MyStory> stories; ListView list; @Ov

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


现在,我可以只在特定日期设置启用/禁用ListView项目吗?例如,如果系统日期为(例如)2014年6月29日,则只有一个故事是可读的(可点击的)?。

是否需要为每个故事在数据库中添加日期列?
主活动完成代码为:

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);
}
public类MainActivity扩展了ListActivity{
DBAdapter-db;
列举故事;
列表视图列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=getListView();
db=新的DBAdapter(getBaseContext());
db.open();
stories=db.getAllContacts();
如果(stories.size()==0){
字符串destPath=“/data/data/”+getPackageName()+“/databases”;
试一试{
CopyDB(getBaseContext().getAssets().open(“StoryDB”),
新文件输出流(destPath+“/stories”);
Log.i(DBAdapter.TAG,“DB Copy OK”);
stories=db.getAllContacts();
刷新显示();
Log.i(DBAdapter.TAG,storiess.size()+“=stories count”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}否则{
刷新显示();
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
Mystory story=stories.get(位置);
Intent next=新的Intent(this,ShowStory.class);
next.putExtra(“thisstory”,story);
星触觉(next);
}
public void CopyDB(InputStream InputStream,OutputStream OutputStream)
抛出IOException{
//---一次复制1K字节---
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=inputStream.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
inputStream.close();
outputStream.close();
}
公共空间刷新显示(){
Log.i(DBAdapter.TAG,stories.size()+“=storeis count”);
ArrayAdapter=新故事适配器(此为故事);
setListAdapter(适配器);
}
}


谢谢

是的,您可以在McClick中比较日期

  @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);
}
}

是否需要在数据库中为每个故事添加一个日期列?是的。否则你将如何比较你的答案。那么我该怎么做呢?我真的很困惑!@TerrilMomas每当你添加一个故事时,请将引用的系统时间保存在数据库中,你能为一个项目编写一个示例吗?@TerrilMomas我可以简单地指导你。而不是向您提供代码。此方法适用于listview中的所有项目吗?是的。。但是在您将当前日期与数据库日期进行比较之前,您可以为一个项目编写一个示例吗?@karthigh Pandiyan
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);
}
  @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);
}
}