Jump to content

Recommended Posts

Posted

Bom dia,

Tenho dois botões para cada item da minha ListView, só que eles não funcionam, no sentido em que quando carrego quero que apareça uma Toast a dizer "ola"

lista_perguntas.setOnItemClickListener(new OnItemClickListener() {


        	@Override
        	@SuppressWarnings("unchecked")
         	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        		HashMap<String, String> map = (HashMap<String, String>) lista_perguntas.getItemAtPosition(position);
        		editra_resposta.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                    	tipo_ediccao = 1;
                    	Toast.makeText(lista_perguntas.this, "ola", Toast.LENGTH_SHORT).show();
                    }
                });
        	}
         });
Posted

Convinha avisares o pessoal que estás a falar de algo que só existe na plataforma Android 😉

Olhando para o código não vejo nada de especialmente errado, mas que variável é essa "editra_resposta" onde estás a adicionar o evento? É o botão? Se executares a aplicação passo a passo o código de criação da janela de diálogo é apresentado?

Posted

editar_resposta é o botão

o evento está no OnCreat:

Mas é mais simples de perceber com isto:  😉

package pt.projecto.estudos;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class lista_perguntas extends Activity {


private ListView lista_perguntas;
private Button editar_pergunta;
private Button editra_resposta;
private int tipo_ediccao = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	setContentView(R.layout.listar_perguntas);
    super.onCreate(savedInstanceState);

    editra_resposta = (Button) findViewById(R.id.edit_resposta);
    editar_pergunta = (Button) findViewById(R.id.edit_pergunta);
    

    lista_perguntas = (ListView) findViewById(R.id.listviewpeguntas);

        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

        HashMap<String, String> map;
        
        map = new HashMap<String, String>();
        
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 1");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 2");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 3");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 4");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 5");
        listItem.add(map);
        
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.item_pergunta,
               new String[] {"titulo"}, new int[] {R.id.perguntas_value});

        lista_perguntas.setAdapter(mSchedule);
        
        lista_perguntas.setOnItemClickListener(new OnItemClickListener() {


        	@Override
        	@SuppressWarnings("unchecked")
         	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
			//on récupère la HashMap contenant les infos de notre item (titre, description, img)
        		HashMap<String, String> map = (HashMap<String, String>) lista_perguntas.getItemAtPosition(position);
        		editra_resposta.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                    	tipo_ediccao = 1;
                    	Toast.makeText(lista_perguntas.this, "oi gato", Toast.LENGTH_SHORT).show();
                    }
                });
        	}
         });
    // TODO Auto-generated method stub
}

}
  • 1 month later...
Posted

Tens uma confusãozita nesse código. Experimenta isto:

package pt.projecto.estudos;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class lista_perguntas extends Activity {


private ListView lista_perguntas;
private Button editar_pergunta;
private Button editra_resposta;
private int tipo_ediccao = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	setContentView(R.layout.listar_perguntas);
    super.onCreate(savedInstanceState);

    editra_resposta = (Button) findViewById(R.id.edit_resposta);
    editar_pergunta = (Button) findViewById(R.id.edit_pergunta);
    

    lista_perguntas = (ListView) findViewById(R.id.listviewpeguntas);

        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

        HashMap<String, String> map;
        
        map = new HashMap<String, String>();
        
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 1");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 2");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 3");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 4");
        listItem.add(map);
        map = new HashMap<String, String>();
        map.put("titulo", "Pergunta 5");
        listItem.add(map);
        
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.item_pergunta,
               new String[] {"titulo"}, new int[] {R.id.perguntas_value});

        lista_perguntas.setAdapter(mSchedule);
        
        lista_perguntas.setOnItemClickListener(new OnItemClickListener() {


        	@Override
        	@SuppressWarnings("unchecked")
         	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
			//on récupère la HashMap contenant les infos de notre item (titre, description, img)
        		HashMap<String, String> map = (HashMap<String, String>) lista_perguntas.getItemAtPosition(position);
                }
        });	
        editra_resposta.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                    	tipo_ediccao = 1;
                    	Toast.makeText(lista_perguntas.this, "oi gato", Toast.LENGTH_SHORT).show();
                    }
                });
}

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.