简体   繁体   中英

the ListView in ListActivity will not display any data

I have a MainActivity which contains a searchView int he action bar. The text in the searchView query is passed into an intent, this intent then starts my SearchableActivity. In SearchableActivity I receive the intent with the query string as well.

Now I have some mock data in my fetchResult method, I then bind this data to the ListView so it can be displayed in the ListView.

Obviously I want this mock data to be displayed in the ListView but instead I get a blank screen with "No data" displayed.

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>

I can`t see the part where you add the adapter to the listview.

listview.setAdapter(fetchResults(query));

In your SearchableActivity.java , you are comparing Intent Action. But in MainActivity.java , you have put Intent Extra Intent.ACTION_SEARCH .

Try using -

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

instead of -

intent.putExtra(Intent.ACTION_SEARCH, query);

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM