建议在ListActivity中显示我的操作栏

时间:2015-02-21 16:58:41

标签: android listview android-actionbar

您好我的应用程序拍摄照片/视频或从画廊中选择一个,然后尝试发送给联系人,参加工作正常问题是发送我有一个ListActivity确实显示我的联系人和我做单选择o多重选择,当我选择1个或多个时,动作栏中应该出现一个发送图像(按钮),允许继续下一个阶段,问题是ListActivity根本没有ActionBar!因此,当我选择朋友应用程序崩溃时,请帮助我实现这个想法......

我的代码: RecipientsActivity(显示朋友的ListView)



package com.gorydev.selfdestructapp;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

import java.util.ArrayList;
import java.util.List;


public class RecipientsActivity extends ListActivity{

    protected List<ParseUser> mFriends;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;
    protected MenuItem mSendMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipients);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// para permitir la seleccion multiple de amigos
    }

    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseContants.KEY_FRIENDS_RELATION);
        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
        query.orderByAscending(ParseContants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> parseUsers, ParseException e) {
                if(e == null) {
                    mFriends = parseUsers;
                    String[] usersnames = new String[mFriends.size()];
                    int i = 0;
                    for (ParseUser user : mFriends) { //Obtengo todos los nombres de usuario que estan en mUsers
                        usersnames[i] = user.getUsername();
                        i++;
                    }
                    //Ahora se crea el adaptador que mostrara los datos en el ListView
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),  //esto porque no es una Activity
                            android.R.layout.simple_list_item_checked,
                            usersnames);
                    setListAdapter(adapter);
                }else{
                    //Fail
                    AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
                    builder.setMessage(R.string.query_friends_error);
                    builder.setTitle(R.string.query_friends_title);
                    builder.setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }


    @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_recipients, menu);
        mSendMenuItem = menu.getItem(0);
        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();

        //noinspection SimplifiableIfStatement
        switch(id) {
            case R.id.action_settings:
                return true;
            case R.id.action_send:
                ParseObject message = createMessage();
                //send(message);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if(l.getCheckedItemCount() >= 1)
            mSendMenuItem.setVisible(true);
        else
            mSendMenuItem.setVisible(false);
    }

    protected ParseObject createMessage(){
        ParseObject message = new ParseObject(ParseContants.CLASS_MESSAGES);
        message.put(ParseContants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
        message.put(ParseContants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername());
        message.put(ParseContants.KEY_RECIPIENT_IDS, getRecipientIds());

        return message;
    }

    private ArrayList<String> getRecipientIds() {
        ArrayList<String> recipientIds = new ArrayList<String>();
        for(int i=0; i<getListView().getCount(); i++){
            if(getListView().isItemChecked(i)){
                recipientIds.add(mFriends.get(i).getObjectId());
            }
        }
        return recipientIds;
    }

    //TODO BUG when choosing file the app crashes!!!!!!!!
}
&#13;
&#13;
&#13;

RecipientsMenu.xml文件(我有我的菜单项):

&#13;
&#13;
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.gorydev.selfdestructapp.RecipientsActivity">

    <item android:id="@+id/action_send"
          android:title="@string/action_send_title"
          android:orderInCategory="100"
          app:showAsAction="always"
          android:visible="false"
          android:icon="@drawable/ic_action_send_now"/>
</menu>
&#13;
&#13;
&#13;

伙计们请提供示例,我有搜索但是很困扰我

3 个答案:

答案 0 :(得分:1)

我有同样的问题,我通过编写自己的类来解决它,如下所示:

 public class ActionBarListActivity extends ActionBarActivity {

    private ListView mListView;

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

然后你可以像这样扩展它而不是ListActivity

public class RecipientsActivity extends ActionBarListActivity {
   .
   .
   .
}

答案 1 :(得分:0)

onCreate()方法中添加以下代码:

setHasOptionsMenu(true);

并在super方法中调用onCreateOptionsMenu()方法,如下所示:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

按照Apurva的建议使用Contextual Action Bar。

答案 2 :(得分:0)

如果您可以设法实现ActionBar库,例如Sherlock。

,这会更简单
private class RecipientsActivity extends SherlockListActivity {

}