How to use ListActivity with ActionBar?

0

You can only access the menu of a ListView that is without ActionBar by the menu key in the emulator.

How to display it? Do you need to inflate by BaseAdapter ?

ActivityofListView:

publicclassListDespesasActivityextendsListActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);DespesaDAOdespesa=newDespesaDAO(this);List<Despesa>list=despesa.getLista();ListViewlistView=getListView();setListAdapter(newDespesaAdapter(this,list,listView));}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){getMenuInflater().inflate(R.menu.menu_list_despesas,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseandroid.R.id.home:NavUtils.navigateUpFromSameTask(this);returntrue;caseR.id.action_add_despesa:Intentintent=newIntent(this,EditaDespesaActivity.class);startActivity(intent);returntrue;}returnsuper.onOptionsItemSelected(item);}}

BaseAdapter:

publicclassDespesaAdapterextendsBaseAdapter{privateContextcontext;privateList<Despesa>lista;privateListViewlistView;publicDespesaAdapter(Contextcontext,List<Despesa>lista,ListViewlistView){this.context=context;this.lista=lista;this.listView=listView;}@OverridepublicintgetCount(){returnlista.size();}@OverridepublicObjectgetItem(intposition){returnlista.get(position);}@OverridepubliclonggetItemId(inti){returni;}@OverridepublicViewgetView(intposition,Viewview,ViewGroupviewGroup){finalintauxPosition=position;LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);finalRelativeLayoutlayout=(RelativeLayout)inflater.inflate(R.layout.row,null);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView<?>adapterView,Viewview,inti,longl){Intentintent=newIntent(context,EditaDespesaActivity.class);intent.putExtra("valor", lista.get(auxPosition).getValor());
            intent.putExtra("data", lista.get(auxPosition).getData());
            intent.putExtra("descricao", lista.get(auxPosition).getDescricao());
            context.startActivity(intent);
        }
    });





    TextView data = (TextView)
            layout.findViewById(R.id.ddata);
    data.setText(lista.get(position).getData());

    TextView desc = (TextView)
            layout.findViewById(R.id.ddesc);
    desc.setText(lista.get(position).getDescricao());

    TextView valor = (TextView)
            layout.findViewById(R.id.vvalor);
    valor.setText(lista.get(position).getValor());

    return layout;
}
}

Manifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.prjctt.allan.financeiro" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".EditaDespesaActivity"
        android:label="@string/title_activity_edita_despesa"
        android:parentActivityName=".ListDespesasActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ListDespesasActivity" />
    </activity>
    <activity
        android:name=".ListDespesasActivity"
        android:label="@string/title_activity_despesas"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
    <activity
        android:name=".EditaReceitaActivity"
        android:label="@string/title_activity_edita_receita" >
    </activity>
</application>

Styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

    
asked by anonymous 19.01.2015 / 14:50

1 answer

2

An Activity to have an ActionBar must descend from ActionBarActivity , which is not the case for ListActivity >.

An Activity does not need to derive from ListActivity so that it can use a ListView. The ListActivity class exists only to make it easier to use ListView in an Activity .

In order for your ActionBarActivity to handle a ListView you need a Layout that contains one and a reference to it.

Declare a private attribute to save the ListView

private ListView listView;

In the onCreate method get a reference to the ListView and use the ListView.setAdapter method to pass an instance of DespesaAdapater

public class ListDespesasActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        //Layout que contem a ListView
        setContentView(R.layout.activity_main);

        DespesaDAO despesa = new DespesaDAO(this);
        List<Despesa> list = despesa.getLista();

        // O mesmos que ListView listView = getListView();
        ListView listView = (ListView)findViewById(android.R.id.list);

        //O mesmo que setListAdapter(new DespesaAdapter(this,list,listView));
        listView.setAdapter(new DespesaAdapter(this,list,listView));
    }
}

ActionBarActivity is an "ActionBarListActivity"

To interact with the ListView use the listView attribute:

listView.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> a, View v,int position, long id) 
    {
        ....
        ....
     }
});

While this solution is easy to deploy, it would be advisable to use a ListFragment along with ActionBarActivity     

19.01.2015 / 16:18