2017-01-12 3 views
0
nicht lösen

Ich baue eine searchView für meine Karte, daher habe ich die Locaionrovider-Klasse erweitert ContentProvider erstellt und erstellt AUTHORITY & CONTENT_UR darin. in der SearchableLocation Aktivität rief ich CONTENT_UR aber es gibt diesen Fehler: kann nicht CONTENT_URIandroid- kann Uri CONTENT_URI für searchview ContentProvider

Ich habe versucht, das Problem zu lösen, ist aber nicht erfolgreich.

Hier SearchableLocations:

public class SearchableLocations extends Activity { 
private TextView mTextView; 

private ListView mListView; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search_main); 
    mTextView = (TextView) findViewById(R.id.text); 
    mListView = (ListView) findViewById(R.id.search_list); 


    handleIntent(getIntent()); 


} 


protected void onNewIntent (Intent intent){ 

    handleIntent(intent); 

} 

private void handleIntent(Intent intent) { 
    if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
     Intent nameIntent= new Intent(this, NameActivity.class); 
     nameIntent.setData(intent.getData()); 
     startActivity(nameIntent); 
    }else if (Intent.ACTION_SEARCH.equals(intent.getAction())){ 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     showResults(query); 
    } 
} 


@SuppressLint("StringFormatInvalid") 
private void showResults (String query){ 
    Cursor cursor= getContentResolver().query(LocationProvider.CONTENT_URI, 
      null, null, new String[]{query}, null); 

    if (cursor ==null){ 
     mTextView.setText(getString(R.string.noresult, new Object[] 
     {query})); 
    } else { 
     int count = cursor.getCount(); 
     String countString = getResources().getQuantityString(
       R.plurals.search_results,count, new Object[] {count,query} 
     ); 

     mTextView.setText(countString); 

     String [] from = new String[] { 
       LocationDatabase.KET_NAME, 
       // LocationDatabase.KEY_LOCATION 
     }; 

     int[] to = new int[]{ 
       R.id.name, 
       // R.id.location 
     }; 

     SimpleCursorAdapter names = new SimpleCursorAdapter(
       this, R.layout.search_result, cursor, from, to 
     ); 
     mListView.setAdapter(names); 

     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


       Intent nameIntent = new Intent(getApplicationContext(), 
        NameActivity.class ); 

       Uri data = Uri.withAppendedPath(LocationProvider.CONTENT_URI, 
         String.valueOf(id)); 
       nameIntent.setData(data); 
       startActivity(nameIntent); 

      } 
     }); 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.options_menu, menu); 

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     searchView.setIconifiedByDefault(false); 
    } 

    return true; 
} 

LocationProvider

public class LocationProvider extends ContentProvider { 

String TAG="LocationProvider"; 

public static String AUTHORITY="com.playpersia.bicyclemap.LocationProvider"; 
public static final Uri CONTENT_URI =Uri.parse("content://" +AUTHORITY + "location"); 

// MIME types used for searching words or looking up a single location 

public static final String LOCATION_MIME_TYPE= ContentResolver.CURSOR_DIR_BASE_TYPE + 
     "/vnd.com.playpersia.bicyclemap"; 

public static final String DEFINITION_MIME_TYPE =ContentResolver.CURSOR_DIR_BASE_TYPE + 
     "/vnd.com.playpersia.bicyclemap"; 

private LocationDatabase mLocation; 

//UriMatcher 

private static final int SEARCH_NAMES=0; 
private static final int GET_NAMES=1; 
private static final int SEARCH_SUGGEST=2; 
private static final int REFRESH_SHORTCUT=3; 
private static final UriMatcher sURIMATCHER= buildUriMatcher(); 


private static UriMatcher buildUriMatcher(){ 
    UriMatcher matcher= new UriMatcher(UriMatcher.NO_MATCH); 

    matcher.addURI(AUTHORITY , "location", SEARCH_NAMES); 
    matcher.addURI(AUTHORITY, "location/#", GET_NAMES); 

    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST); 
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + 
      "/*", SEARCH_SUGGEST); 



    matcher.addURI(AUTHORITY,SearchManager.SUGGEST_URI_PATH_SHORTCUT,REFRESH_SHORTCUT); 
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT 
      +"/*", REFRESH_SHORTCUT); 

    return matcher; 

} 
@Override 
public boolean onCreate() { 

    mLocation= new LocationDatabase(getContext()); 

    return true; 
} 

@Nullable 
@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    switch (sURIMATCHER.match(uri)) 
    { 
     case SEARCH_SUGGEST: 
      if (selectionArgs == null){ 

       throw new IllegalArgumentException(
         "selectionArgs nust be provided for the Uri:" + uri); 
      } 

      return getSuggestion(selectionArgs[0]); 

     case SEARCH_NAMES: 

      if (selectionArgs == null) { 
       throw new IllegalArgumentException(
         "selectionArgs must be provided for the Uri: " + uri); 
      } 

      return search(selectionArgs[0]); 
     case GET_NAMES: 
      return getName(uri); 
     case REFRESH_SHORTCUT: 
      return refreshShortcut(uri); 
     default: 
      throw new IllegalArgumentException("Unknown Uri: " + uri); 

    } 

} 



private Cursor getSuggestion(String query) { 

    query=query.toLowerCase(); 
    String[] columns = new String[]{ 
      BaseColumns._ID, 
      LocationDatabase.KET_NAME, 
      LocationDatabase.KEY_LOCATION, 

      SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID 

    }; 

    return mLocation.getNameMatches(query, columns); 
} 

private Cursor search (String query){ 
    query=query.toLowerCase(); 
    String[] columns = new String[]{ 
      BaseColumns._ID, 
      LocationDatabase.KET_NAME, 
      LocationDatabase.KEY_LOCATION, 
    }; 

    return mLocation.getNameMatches(query, columns); 
+0

haben Sie importieren 'Import android.location.LocationListener'? –

+0

'Hier ist SearchbelLocations' ??? Kannst du deinen eigenen Klassennamen auch richtig schreiben? – greenapps

+0

@greenapps kümmern Sie sich um Ihre Sprache – Hadis

Antwort

0

Haben Sie Ihren Content-Provider in Ihrem Manifest registriert? Sie sollten ähnlich am Ende mit etwas:

<provider android:name=".LocationProvider" 
      android:authorities="${applicationId}.LocationProvider" 
      android:exported="false" 
      android:label="@string/provider_locations" /> 

ändern name und authorities Eigenschaften entsprechend mit Ihrer Anwendung benötigt, wenn

+0

Ich tat, aber immer noch das gleiche – Hadis

Verwandte Themen