【问题标题】:How to limit "onListItemClick" to Time Range?如何将“onListItemClick”限制为时间范围?
【发布时间】:2014-06-05 10:18:02
【问题描述】:

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

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


和onListItem点击代码:

@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 日,则只有一个故事可读(可点击)?
是否需要在数据库中为每个故事添加一个日期列?
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);
}

}

谢谢

【问题讨论】:

  • 是否需要在数据库中为每个故事添加一个日期列?是的。不然怎么比较
  • 谢谢你的回答。那么我该怎么做呢?我真的很困惑! @TerrilThomas
  • 每当您添加一个故事时,都会获取引用它的系统时间并将其保存在数据库中
  • 你能写一个项目的例子吗? @TerrilThomas
  • 我可以简单地指导你。而不是用代码喂你。

标签: java android sqlite listview android-listview


【解决方案1】:

是的,您可以比较 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);
}
}

【讨论】:

  • 这个方法适用于列表视图中的所有项目吗?
  • yup.. 但在您将当前日期与数据库日期进行比较之前
  • 你能写一个项目的例子吗? @Karthick Pandiyan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多