How to use Listactivity with Actionbar?

Asked

Viewed 417 times

0

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

How to display it? It is necessary to inflate by BaseAdapter?

ListView sem ActionBar

Activity of ListView:

public class ListDespesasActivity extends ListActivity {



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



    DespesaDAO despesa = new DespesaDAO(this);
    List<Despesa> list = despesa.getLista();
    ListView listView = getListView();
    setListAdapter(new DespesaAdapter(this,list,listView));


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_list_despesas, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_add_despesa:
            Intent intent = new Intent(this, EditaDespesaActivity.class);
            startActivity(intent);
            return true;
    }

    return super.onOptionsItemSelected(item);
}
}

BaseAdapter:

public class DespesaAdapter extends BaseAdapter {
private Context context;
private List<Despesa> lista;
private ListView listView;


public DespesaAdapter(Context context, List<Despesa> lista, ListView listView) {
    this.context = context;
    this.lista = lista;
    this.listView = listView;
}


@Override
public int getCount() {
    return lista.size();
}

@Override
public Object getItem(int position) {
    return lista.get(position);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;




    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    final RelativeLayout layout = (RelativeLayout)
            inflater.inflate(R.layout.row, null);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(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>

  • Allan, could include your Manifest and your style file?

  • Manifest included, what would be the collection of styles? @Wakim

  • The archive styles.xml.

  • Edited with Styles.xml @Wakim

  • I would recommend changing your superclass Activity for ActionBarActivity, but this will cause some methods to be unavailable (e. g: setListAdapter) but then you just copy and implement it yourself (http://answall.com/questions/38337/addir-bot%C3%B5es-na-action-bar-quando-usa-listactivity/39384#39384).

1 answer

2


One Activity to have a Actionbar has to descend from Actionbaractivity, which is not the case Listactivity.

An Activity does not need to derive from Listactivity in order to use a Listview. The class Listactivity exists only to facilitate the use of Listview in a Activity.

So that your Actionbaractivity can handle a Listview needs a Layout containing a reference to and.

Declare a private field to store Listview

private ListView listView;

In the method onCreate get a reference to Listview and use the method ListView.setAdapter 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));
    }
}

Now the Actionbaractivity is a "Actionbarlistactivity"

To interact with the Listview use the listview field:

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

Although this solution is easy to implement, it would be advisable to use a Listfragment together with the Actionbaractivity

  • Perfect, thank you very much, now it’s all right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.