我有一个MainActivity,其中包含一个searchView int动作栏。 searchView查询中的文本被传递给一个intent,然后这个intent启动我的SearchableActivity。在SearchableActivity中,我也使用查询字符串接收intent。
现在我的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 :(得分:0)
我看不到将适配器添加到列表视图的部分。
listview.setAdapter(fetchResults(查询));
答案 1 :(得分:0)
在 SearchableActivity.java 中,您正在比较Intent Action。但是在 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);
我希望这会对你有所帮助。