onListItemClick未被调用。我不知道为什么

时间:2015-05-14 18:42:49

标签: android listview android-actionbar android-actionbaractivity

这是我的活动:我正在尝试呼叫" onListItemClicked"但它只受ListActivity支持?我想使用ActionBarActivity,因为我想要一个带有" Logout"和"更新"。使用ListActivity它不会显示。我知道我做错了什么?

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 = (ListView)findViewById(android.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

                }
            }
        });

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

}

OnListItemClick...

}

这是我的布局:

<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="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用OnItemClickListener

listView .setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
         // Do Something on List ItemClick
      }
   });