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

时间:2022-03-24 10:34:54

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:

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

  • "("+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)
  • new Date(DatabaseAdapter.KEY_END) - >这两个应该在dd.MM中的一个TextView中。 HH:mm(这是从BEGIN开始) - HH:mm(这是从END开始)

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

我不知道我怎么能这样做 - 请帮帮我:)

1 个解决方案

#1


0  

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.

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

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

#1


0  

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.

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

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