当我将 class 扩展到 ActionBarActivity 时,我无法使用 ListView。我只有扩展到ListActivity才能访问ListView。任何

I can't use ListView when I am extending my class to ActionBarActivity. I can only access ListView when I extend to ListActivity. Any

我的 activity:它向我显示了两个错误 "Required: android.widget.ListView" 我已经将 ListView 小部件导入到我的 Activity,所以我不明白这是什么问题。

package com.example.test.test;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.List;


public class HomepageActivity extends ActionBarActivity {

protected List<ParseObject> mStatus;

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

    final ListView listView = findViewById(R.id.listView);

    Parse.initialize(this, "dcAMNT7HVOmOw0JDMelkg5UDr388O3xSgICiSK3N", "1aHIAldsUScxlbkWGkoyvHoHWM9YEtpTb6QIijrb");

    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser != null) {
        // show user the homepage status
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Status");
        query.orderByDescending("createdAt");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> status, ParseException e) {
                if (e == null) {
                    //success
                    mStatus = status;
                    StatusAdapter adapter = new StatusAdapter(getApplicationContext(), mStatus);
                    listView.setAdapter(adapter);
                } else {
                    //there was a problem, Alert user

                }
            }
        });

    } else {
        // show the login screen
        Intent takeUserToLoginScreen = new Intent(HomepageActivity.this, LoginActivity.class);
        startActivity(takeUserToLoginScreen);
    }

}

@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);
    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();
    switch (id) {
        case R.id.updateStatus:
            //take user to update status activity

            Intent intent = new Intent(this, UpdateStatusActivity.class);
            startActivity(intent);

            break;

        case R.id.logoutUser:
            //log out the user
            ParseUser.logOut();

            //take user back to login screen
            Intent takeUserToLogin = new Intent(this, LoginActivity.class);
            startActivity(takeUserToLogin);

            break;
    }
    return super.onOptionsItemSelected(item);

}
}

我将布局从 .android 更改为 @+id...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

</RelativeLayout>

I can't use ListView when I am extending my class to ActionBarActivity

是的,你可以。

Now that I am using ListView, the action bar on the top doesn't show

您需要从 ActionBarActivity 继承,因为您似乎正在使用 appcompat-v7。然后,要检索您的 ListView,您可以调用 findViewById(),就像您对任何其他小部件所做的那样。在你的情况下,它将是 findViewById(android.R.id.list),给定你的 ListView android:id 值。

Here is a sample application 有一个 ActionBarActivity 正在托管一个 ListView.

这是因为您的代码使用了使用 ListActivity 的默认 ListView 实现。您需要做的是像这样创建一个自定义 ListView..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>

</RelativeLayout>

还有你的activity...

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


ListView listView = (ListView)findViewById(R.id.list);

Parse.initialize(this, "dcAMNT7HVOmOw0JDMelkg5UDr388O3xSgICiSK3N",    "1aHIAldsUScxlbkWGkoyvHoHWM9YEtpTb6QIijrb");

ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
    // show user the homepage status
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Status");
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> status, ParseException e) {
            if (e == null) {
                //success
                mStatus = status;
                StatusAdapter adapter = new StatusAdapter(getApplicationContext (), mStatus);
                listView.setAdapter(adapter);
            } else {
                //there was a problem, Alert user

            }
        }
    });

您可以从 ActionBarActivity 扩展并拥有一个 ListView。只需添加列表视图而不是使用 setListAdapter(),执行:

    ArrayList<String> items = new ArrayList<String>();
    //populate items
    items.add(String1)....
    ListView listView = (ListView) findViewById(R.id.listview);
    ViewGroup header = (ViewGroup)getLayoutInflater().inflate(R.layout.list_item_listview_header, listView, false);
    listView.addHeaderView(header, null, false);
    ListViewAdapter adapter = new YourListViewAdapter(this,  items);
    listView.setAdapter(adapter);