List View and Normal Layout in an Activity

Asked

Viewed 293 times

0

Hello, I’m wanting a screen to have a button and, below that button, a list with several items. But it turns out that the buttons are multiplied.

Behold:

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nova"
        android:onClick="nova"
        android:layout_marginTop="20dp"
        android:text="@string/nova" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

Java activity.

package com.educin.leonardo.nomes;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TarefasActivity extends ListActivity implements AdapterView.OnItemClickListener {

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

        String[] de = {"nome"};
        int[] para = {R.id.nome};

        SimpleAdapter adapter = new SimpleAdapter(this, listarNomes(), R.layout.activity, de, para);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    private List<Map<String, Object>> nomes;

    private List<Map<String, Object>> listarNomes() {
        nmes = new ArrayList<Map<String, Object>>();

        Map<String, Object> item = new HashMap<String, Object>();

        item = new HashMap<String, Object>();
        item.put("nome", "Leonardo");
        nomes.add(item);

        item = new HashMap<String, Object>();
        item.put("nome", "Leo");
        nomes.add(item);

        return nome;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> map = nomes.get(position);

        String nome = (String) map.get("nome");

        Toast.makeText(this, nome, Toast.LENGTH_SHORT).show();
    }

    public void nova(View view) {
        if (view.getId() == R.id.nova) {
            startActivity(new Intent(this, NovaNovaActivity.class));
        }
    }

}

Explaining better: Two lines and a button are formed in each row. But I would just like a button at the top of Activity.

How to solve?

  • Just by tapping my eye, I see four two strange things. First this line: nmes = new ArrayList<Map<String, Object>>(); - The variable nmes does not exist, should be nomes. At the end of the return nome; should also be return nomes;. Already in the method onItemClick, that cast for String seems to me totally unnecessary. Moreover, because that nomes is a List<Map<String, Object>> instead of a List<String>, since each Map has only one key "nome" and the value is always of the type String?

  • This is why I changed the name of the variables when I posted to Stack, and I didn’t check if their names were correct, because the point of the topic is another

1 answer

0


You must set a layout for your Activity that has the button and the ListView. In that case you’re just setting up the layout of the items of ListView with all the content of Activity.

A simple layout of Activity, reusing yours, would be:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nova"
        android:onClick="nova"
        android:layout_marginTop="20dp"
        android:text="@string/nova" />

    <ListView
        android:id="@android:id/list" <!-- Obrigatorio no caso da ListActivity -->
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

Now for each item in the list you can use the default layout itself android.r.layout.simple_list_item_1, that has only one TextView:

SimpleAdapter adapter = new SimpleAdapter(this, listarNomes(), android.r.layout.simple_list_item_1, de, android.R.id.text1);

And don’t forget to call setContentView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    // restante do seu código...
}

Browser other questions tagged

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