在 ListActivity 中创建 'up' 操作栏
Creating the 'up' action bar within a ListActivity
我的问题是我似乎无法获得列表视图上方的操作栏,如图所示。
当我使用 extends ActionBarActivity
时,我无法访问 ListView class,因此无法使用 getListView()
.
等方法
我尝试使用这个 answer here 并创建我自己的 ListView 实现,我取得了一些成功。
这里的问题是我无法在单击项目时触发 onListItemClick
。
我不确定我是否错过了一个步骤,现在需要手动调用它,在这种情况下可能只是我没有 know/understand 方法的正确参数,onListItemClick(ListView lv, View v, int position, long id)
.
所以我的问题分为两部分。
- 有没有办法将 'up' 按钮添加到
ListActivity
- 如果我自己创建 class,如何调用
onListItemClick
方法。
谢谢。
EditContactsActivity.java
package uk.co.jackdh.tapchat;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import java.util.List;
public class EditContactsActivity extends ActionBarActivity {
public static final String TAG = EditContactsActivity.class.getSimpleName();
protected List<ParseUser> mUsers;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
private ListView mListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_contacts);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
//success
mUsers = parseUsers;
String[] usernames = new String[mUsers.size()];
int i = 0;
for (ParseUser user : mUsers) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditContactsActivity.this, android.R.layout.simple_list_item_checked, usernames);
getListView().setAdapter(adapter);
addFriendCheckMarks();
} else {
//error
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(EditContactsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
private void addFriendCheckMarks() {
mFriendsRelation.getQuery().findInBackground( new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
//List returned look for match.
for (int i = 0; i < mUsers.size(); i++) {
ParseUser user = mUsers.get(i);
for (ParseUser contact : parseUsers) {
if (contact.getObjectId().equals(user.getObjectId())) {
getListView().setItemChecked(i, true);
}
}
}
} else {
Log.e(TAG, e.getMessage());
}
}
});
}
@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);
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
if (getListView().isItemChecked(position)) {
//add friend
mFriendsRelation.add(mUsers.get(position));
} else {
//remove friend
mFriendsRelation.remove(mUsers.get(position));
}
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null ) {
// Log.e(TAG, e.toString());
}
}
});
}
}
activity_edit_contacts.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="uk.co.jackdh.tapchat.EditContactsActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Is there a way to add the 'up' button to ListActivity
Android 开发者教程中的答案是 here。基本上你可以继续使用 ActionbarActivity
class 并使用 getActionBar().setDisplayHomeAsUpEnabled(true);
方法来启用它。然后,您将根据 id R.id.home
.
的要求处理 onOptionsItemSelected
方法
How do I called the onListItemClick method if I create the class myself.
首先,您必须让 EditContactsActivity class 实现 OnItemClickListener 接口。
public class EditContactsActivity extends ActionBarActivity implements OnItemClickListener.
那么你已经实现了onItemClick 方法。最后将您的 class 设置为 mListView 的点击侦听器。
mListView.setOnItemClickListener(this);
详细教程在here
我的问题是我似乎无法获得列表视图上方的操作栏,如图所示。
当我使用 extends ActionBarActivity
时,我无法访问 ListView class,因此无法使用 getListView()
.
我尝试使用这个 answer here 并创建我自己的 ListView 实现,我取得了一些成功。
这里的问题是我无法在单击项目时触发 onListItemClick
。
我不确定我是否错过了一个步骤,现在需要手动调用它,在这种情况下可能只是我没有 know/understand 方法的正确参数,onListItemClick(ListView lv, View v, int position, long id)
.
所以我的问题分为两部分。
- 有没有办法将 'up' 按钮添加到
ListActivity
- 如果我自己创建 class,如何调用
onListItemClick
方法。
谢谢。
EditContactsActivity.java
package uk.co.jackdh.tapchat;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import java.util.List;
public class EditContactsActivity extends ActionBarActivity {
public static final String TAG = EditContactsActivity.class.getSimpleName();
protected List<ParseUser> mUsers;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
private ListView mListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_contacts);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
//success
mUsers = parseUsers;
String[] usernames = new String[mUsers.size()];
int i = 0;
for (ParseUser user : mUsers) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditContactsActivity.this, android.R.layout.simple_list_item_checked, usernames);
getListView().setAdapter(adapter);
addFriendCheckMarks();
} else {
//error
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(EditContactsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
private void addFriendCheckMarks() {
mFriendsRelation.getQuery().findInBackground( new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> parseUsers, ParseException e) {
if (e == null) {
//List returned look for match.
for (int i = 0; i < mUsers.size(); i++) {
ParseUser user = mUsers.get(i);
for (ParseUser contact : parseUsers) {
if (contact.getObjectId().equals(user.getObjectId())) {
getListView().setItemChecked(i, true);
}
}
}
} else {
Log.e(TAG, e.getMessage());
}
}
});
}
@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);
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
if (getListView().isItemChecked(position)) {
//add friend
mFriendsRelation.add(mUsers.get(position));
} else {
//remove friend
mFriendsRelation.remove(mUsers.get(position));
}
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null ) {
// Log.e(TAG, e.toString());
}
}
});
}
}
activity_edit_contacts.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="uk.co.jackdh.tapchat.EditContactsActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Is there a way to add the 'up' button to ListActivity
Android 开发者教程中的答案是 here。基本上你可以继续使用 ActionbarActivity
class 并使用 getActionBar().setDisplayHomeAsUpEnabled(true);
方法来启用它。然后,您将根据 id R.id.home
.
onOptionsItemSelected
方法
How do I called the onListItemClick method if I create the class myself.
首先,您必须让 EditContactsActivity class 实现 OnItemClickListener 接口。
public class EditContactsActivity extends ActionBarActivity implements OnItemClickListener.
那么你已经实现了onItemClick 方法。最后将您的 class 设置为 mListView 的点击侦听器。
mListView.setOnItemClickListener(this);
详细教程在here