如何更改从数据库获取的数据?

发布于 2024-10-23 17:46:53 字数 938 浏览 1 评论 0原文

我使用以下代码从数据库中获取数据:

private void fillData() {
        cursor = mDbAdapter.fetchAllSubjects();
        startManagingCursor(cursor);

        String[] from = new String[] { DatabaseAdapter.KEY_TITLE, DatabaseAdapter.KEY_LECTURER, DatabaseAdapter.KEY_BEGIN };
        int[] to = new int[] { R.id.title, R.id.lecturer, R.id.time };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter subjects = new SimpleCursorAdapter(this, R.layout.subject_row, cursor, from, to);

        setListAdapter(subjects);
    }

现在我的问题是,我想从数据库中添加其他 3 列,并希望获取以下内容:

  • "("+DatabaseAdapter.KEY_TYPE+") "+DatabaseAdapter.KEY_TITLE
  • DatabaseAdapter.KEY_LECTURER
  • 新日期(DatabaseAdapter.KEY_BEGIN)
  • 新日期(DatabaseAdapter.KEY_END) -->这两个应该以 dd.MM 的方式位于一个 TextView 中。 HH:mm(这是从 BEGIN 开始的)- HH:mm(这是从 END 开始的)

我不知道如何才能做到这一点 - 请帮助我:)

I get my data out of my db with the following code:

private void fillData() {
        cursor = mDbAdapter.fetchAllSubjects();
        startManagingCursor(cursor);

        String[] from = new String[] { DatabaseAdapter.KEY_TITLE, DatabaseAdapter.KEY_LECTURER, DatabaseAdapter.KEY_BEGIN };
        int[] to = new int[] { R.id.title, R.id.lecturer, R.id.time };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter subjects = new SimpleCursorAdapter(this, R.layout.subject_row, cursor, from, to);

        setListAdapter(subjects);
    }

Now my problem is, that I want to add 3 other columns from my db and want to get the following:

  • "("+DatabaseAdapter.KEY_TYPE+") "+DatabaseAdapter.KEY_TITLE
  • DatabaseAdapter.KEY_LECTURER
  • new Date(DatabaseAdapter.KEY_BEGIN)
  • new Date(DatabaseAdapter.KEY_END)
    --> these two should be in one TextView in the way dd.MM. HH:mm (this is from BEGIN) - HH:mm (this is from END)

I don't know how I'm able to do that - please help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2024-10-30 17:46:53

好吧,我终于明白你真正想要什么了。

您可以创建自己的游标适配器,而不是直接使用“SimpleCursorAdapter”,在其中您可以根据需要对数据进行操作。
创建一个新的适配器“SubjectsAdapter.java”。在此适配器中,您将覆盖“bindView”和“newView”。这允许我们将视图应用到光标。但在此之前,我们有机会更改游标中的数据。

这会让您知道必须做什么。

    private void fillData() 
    {
        cursor = mDbAdapter.fetchAllSubjects();
        startManagingCursor(cursor);


        SubjectsAdapter subjectsAdapter = new SubjectsAdapter(this, cursor);
        setListAdapter(subjectsAdapter);
    }


//SubjectsAdapter.java - make changes to fix bugs/compilation errors. This is untested.
public class SubjectsAdapter extends ResourceCursorAdapter 
{
    public SubjectsAdapter(Context context, Cursor cur) {
        super(context, R.layout.subject_row, cur);
    }

    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) 
    {
         LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         return li.inflate(R.layout.subject_row, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) 
    {           
        TextView titleText = (TextView)view.findViewById(R.id.title);
          titleText.setText(cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_TITLE)));

        //You can add code to retrieve other columns here.

        //This is where you retrieve the date in long format from cursor, convert it to a required format, and then using it.
        TextView beginTimeText = (TextView)view.findViewById(R.id.time);            
        Long lBeginDate = cursor.getLong(cursor.getColumnIndex(DatabaseAdapter.KEY_BEGIN));
        String sBeginDate = getFormattedDate(lBeginDate);           
        beginTimeText.setText(sBeginDate);
   }

    private String getFormattedDate(Long lDate)
    {
         SimpleDateFormat smdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");       
        String sDate = smdf.format( lDate ));
        return sDate;
    }
}

Ok I finally figured out what you really wanted.

Instead of using "SimpleCursorAdapter" directly, you can create your own Cursor adapter, inside which you can mainipulate the data as you want.
Create a new Adapter "SubjectsAdapter.java". In this Adapter you will override the "bindView" and "newView". This allows us to apply a view to the cursor. But before doing so, gives us the opportunity to change the data from the cursor.

This will give you an idea what has to be done.

    private void fillData() 
    {
        cursor = mDbAdapter.fetchAllSubjects();
        startManagingCursor(cursor);


        SubjectsAdapter subjectsAdapter = new SubjectsAdapter(this, cursor);
        setListAdapter(subjectsAdapter);
    }


//SubjectsAdapter.java - make changes to fix bugs/compilation errors. This is untested.
public class SubjectsAdapter extends ResourceCursorAdapter 
{
    public SubjectsAdapter(Context context, Cursor cur) {
        super(context, R.layout.subject_row, cur);
    }

    @Override
    public View newView(Context context, Cursor cur, ViewGroup parent) 
    {
         LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         return li.inflate(R.layout.subject_row, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) 
    {           
        TextView titleText = (TextView)view.findViewById(R.id.title);
          titleText.setText(cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_TITLE)));

        //You can add code to retrieve other columns here.

        //This is where you retrieve the date in long format from cursor, convert it to a required format, and then using it.
        TextView beginTimeText = (TextView)view.findViewById(R.id.time);            
        Long lBeginDate = cursor.getLong(cursor.getColumnIndex(DatabaseAdapter.KEY_BEGIN));
        String sBeginDate = getFormattedDate(lBeginDate);           
        beginTimeText.setText(sBeginDate);
   }

    private String getFormattedDate(Long lDate)
    {
         SimpleDateFormat smdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");       
        String sDate = smdf.format( lDate ));
        return sDate;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文