androidlistviewandroid-listviewandroid-actionbarandroid-actionbaractivity

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


My activity: it is showing me two errors "Required: android.widget.ListView" I imported the ListView widget already to my Activity so I dont understand what's the issue.

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);

}
}

My layout which I changed from .android to @+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>

Solution

  • It's because your code uses the default ListView implementation using ListActivity. What you need to do is create a custom ListView like this..

    <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>
    

    And your 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
    
                }
            }
        });