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 variablenmes
does not exist, should benomes
. At the end of thereturn nome;
should also bereturn nomes;
. Already in the methodonItemClick
, that cast forString
seems to me totally unnecessary. Moreover, because thatnomes
is aList<Map<String, Object>>
instead of aList<String>
, since eachMap
has only one key"nome"
and the value is always of the typeString
?– Victor Stafusa
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
– leonero