当我将我的课程扩展到ActionBarActivity时,我无法使用ListView。我只能在扩展到ListActivity时访问ListView。任何

时间:2015-05-14 16:10:27

标签: android listview android-listview android-actionbar android-actionbaractivity

我的活动:它向我展示了两个错误"必需: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>

3 个答案:

答案 0 :(得分:1)

  

当我将课程扩展到ActionBarActivity

时,我无法使用ListView

是的,你可以。

  

现在我正在使用ListView,顶部的操作栏不显示

您需要继承ActionBarActivity,因为您似乎正在使用appcompat-v7。然后,要检索ListView,请调用findViewById(),就像对其他任何小部件一样。在您的情况下,根据您的findViewById(android.R.id.list) ListView值,它将为android:id

Here is a sample application,其中ActionBarActivity正在托管ListView.

答案 1 :(得分:0)

这是因为您的代码使用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>

你的活动......

@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

            }
        }
    });

答案 2 :(得分:0)

您可以从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);