Android:无法解析方法'查询'

时间:2017-06-03 07:14:57

标签: android sqlite android-sqlite

我试过这段代码:

        String table = "builder.build()";
        String[] columns = new String[] { "distinct id", "date", "sum(goal)" };
        String selection2 = "id=? and date=?";
        String[] arguments = new String[] { "0", "Sep 15, 2015" };
        String groupBy = "id, date";
        String having = null;
        String orderBy = "2";

        cursor = cr.query(table, columns, selection2, arguments, groupBy, having, orderBy);

从这个答案:

GROUP BY query using sqlite.query() function

我在Android Studio中遇到以下查询失败:

  

无法解析方法'查询(java.lang.String,java.lang.String [],java.lang.String,java.lang.String [],java.lang.String,java.lang.String,java .lang.String)'

怎么了?

斯特芬

完整代码

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import static android.database.DatabaseUtils.dumpCursorToString;

public class MainActivity extends AppCompatActivity {

    private final int REQUEST_PERMISSION_READ_CALENDAR = 1;


    // TextView of the scrollview items
    private TextView mItemTextView;


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

        // Get the recource id form the item TextView
        mItemTextView = (TextView) findViewById(R.id.item_text_view);

        // get Data from the Calendar Provider
        new DataFetchTask().execute();

    }


    // Use an async task to do the data fetch off of the main thread.
    public class DataFetchTask extends AsyncTask<Void, Void, Cursor> {

        // Invoked on a background thread
        @Override
        protected Cursor doInBackground(Void... params) {

            // Check if permissions are available
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

                Log.v("MainActivity ", "no Permission");

                ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                        Manifest.permission.READ_CALENDAR
                }, REQUEST_PERMISSION_READ_CALENDAR);
                Log.v("MainActivity ", "Permission requested");

            } else {

                Log.v("MainActivity ", "Permission available");

            }
            Calendar beginTime = Calendar.getInstance();
            beginTime.set(2017, 6 - 1, 1, 8, 0, 0);     // month - 1, value is 0-based
            long startMillis = beginTime.getTimeInMillis();
            Log.v("MainActivity ", "Begin Time: " + startMillis);

            Calendar endTime = Calendar.getInstance();
            endTime.set(2017, 6 - 1, 6, 8, 0, 0);       // month - 1, value is 0-based
            long endMillis = endTime.getTimeInMillis();
            Log.v("MainActivity ", "End Time: " + endMillis);


            Cursor cursor = null;
            ContentResolver cr = getContentResolver();

            // Construct the query with the desired date range.
            Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
            ContentUris.appendId(builder, startMillis);
            ContentUris.appendId(builder, endMillis);

            // submit the query
            String table = "builder.build()";
            String[] columns = new String[] { "distinct id", "date", "sum(goal)" };
            String selection2 = "id=? and date=?";
            String[] arguments = new String[] { "0", "Sep 15, 2015" };
            String groupBy = "id, date";
            String having = null;
            String orderBy = "2";

            cursor = cr.query(builder.build(), columns, selection2, arguments, groupBy, having, orderBy);

            return cursor;
        }


        // Invoked on UI thread
        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);
            // Initialize anything that you need the cursor for, such as setting up
            // the screen with the first word and setting any other instance variables

            Log.v("MainActivity ", "On Post Execute, while " + cursor.moveToNext());

            Log.d("###", dumpCursorToString(cursor));

            Log.d("###", String.valueOf(cursor.getCount()));

        }

        // converts the date from milliseconds to date
        public String dateFormater(String milliseconds) {
            if (milliseconds == null) {
                return null;

            } else {
                Long longDate = Long.valueOf(milliseconds);
                Calendar cal = Calendar.getInstance();
                int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis());
                Date da = new Date(longDate - (long) offset);
                cal.setTime(da);

                //this is full string
                //String time;

                //this is only time
                //time = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(da);

                //this is only date
                return DateFormat.getDateInstance(DateFormat.MEDIUM).format(da);
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

简单地通过查找我发现,根据https://developer.android.com/reference/android/content/ContentResolver.html,有3种查询方法,但它们都没有你想要传递的参数类型。例如,对于所有3个参数类型,第一个参数类型为 Uri ,并且您尝试传递字符串

第二件事是你试图传递7个参数,只有查询方法需要5或6个参数。

这可能不是您所希望的解决方案,但也许您可以使用它并找出其余部分。