List View and Normal Layout in an Activity

0

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

See:

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>

Activity.java

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 one button are formed on each line. But I would just like a button at the top of Activity.

How to solve?

    
asked by anonymous 07.02.2015 / 05:02

1 answer

0

You should set a layout for your Activity that has the button and ListView . In this case you are only setting the layout of the items of ListView with all contents of Activity .

A simple layout of Activity , reusing your, 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 list item you can use the default layout android.r.layout.simple_list_item_1 ", which has only TextView :

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

And do not forget to call setContentView :

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

    // restante do seu código...
}
    
07.02.2015 / 18:17