2015-06-23 2 views
0

У меня есть MainActivity, который содержит панель поиска searchView int it. Текст запроса searchView передается в намерение, и это намерение запускает мою функцию SearchableActivity. В SearchableActivity я также получаю намерение с помощью строки запроса.ListView в ListActivity не отображает никаких данных

Теперь у меня есть некорректные данные в моем методе fetchResult, я затем привязываю эти данные к ListView, чтобы он отображался в ListView.

Очевидно, что я хочу, чтобы эти данные были отображены в ListView, но вместо этого я получаю пустой экран с отображением «Нет данных».

MainActivity.java

public class MainActivity extends ActionBarActivity { 

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


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    MenuItem searchItem = menu.findItem(R.id.action_search); 

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
    if(null != searchView) { 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     searchView.setIconifiedByDefault(false); 
    } 

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { 
     public boolean onQueryTextChange(String newText) { 
      return true; 
     } 

     public boolean onQueryTextSubmit(String query) { 
      Intent intent = new Intent(MainActivity.this, SearchableActivity.class); 
      intent.putExtra(Intent.ACTION_SEARCH, query); 
      startActivity(intent); 
      return true; 
     } 
    }; 

    if (searchView != null) { 
     searchView.setOnQueryTextListener(queryTextListener); 
    } 

    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

SearchableActivity.java

private List<String> arrayList; 
private ArrayAdapter<String> mArrayAdapter; 

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

    Intent intent = getIntent(); 
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     setListAdapter(fetchResults(query)); 
    } 
} 

private ArrayAdapter<String> fetchResults(String query) { 

    String [] data = {"String", "String", "String", "String", "String", "String", "String", 
      "String"}; 

    arrayList = new ArrayList<>(Arrays.asList(data)); 

    mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_artist, 
      R.id.list_item_artist_textview, arrayList); 

    return mArrayAdapter; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_searchable, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

activity_searchable.xml

<LinearLayout> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:drawSelectorOnTop="false"> 

</ListView> 

<TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="No data"/> 

</LinearLayout> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.spotifystreamer" > 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".SearchableActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 
</application> 

</manifest> 

ответ

0

В вашем SearchableActivity.java, вы сравниваете Intent действие. Но в MainActivity.java вы положили Intent Extra Intent.ACTION_SEARCH.

Попробуйте использовать -

intent.setAction(Intent.ACTION_SEARCH); // OR 
intent.setAction(query); // if "query" is the string variable that represents intent action. 

вместо -

intent.putExtra(Intent.ACTION_SEARCH, query); 

Я надеюсь, что это поможет.

0

Я не могу видеть, где вы добавляете адаптер в список.

listview.setAdapter (fetchResults (query));

+0

Да, я это сделал. В методе onCreate() внутри оператора if я вызываю метод setListAdapter(). – TheRealRave

Смежные вопросы