need to return name when clicking list

1

I'm recreating one of my activities to work with Action Bar, with much help from Mr. @Wakin I was able to make it work:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gerenciamento);

    abrebanco();
    buscardados();
    gerelista();
    //String[] tabelas = tab.toArray(new String[tab.size()]);

    List<String> l = getgeraList();

    ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, l);
    ListView lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(ad);



}

private List<String> getgeraList() {
    List<String> l = new ArrayList<String>();
    cursor.moveToLast();
    int x=cursor.getCount();
    int y=0;
    while(y<x){
    //nextdado();   
        l.add(retornadado());
        dadoanterior();
        y++;

    }
    return l;
}

I need to return the contents of what was written in the listview that was selected by the user.

The problem that the function:

 lv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

of the error. I also can not get the contents of the list.

    
asked by anonymous 06.11.2014 / 13:56

2 answers

1

The problem is that you are trying to use onClickListener to select a specific item from a ListView .

The OnClickListener serves the entire View, no matter what item you click .

In turn, the OnItemClickListener lets you select a specific item from your Adapter within your ListView , returning your Adaper and the clicked position.

In code:

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            //Selecione o objeto através do adapter da sua lista
            String lista = (String) adapterView.getAdapter().getItem(position);         
            Intent intent = new Intent(gerenciar2.this, MainActivity.class);
            intent.putExtra("tabbanco", lista);
            startActivity(intent);
            finish();
    }
});
    
07.11.2014 / 14:31
1

At great cost I got it as follows

public class gerenciar2 extends ActionBarActivity{
    boolean editar=false, adcionar=false, remover=false;
    SQLiteDatabase Banco = null;
    Cursor cursor;

    String tabbanco="Tabela1";
    TextView gerenciar;
    ListView lista;
    ArrayList<String> tab;

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

        abrebanco();
        buscardados();
        gerelista();


        List<String> l = getgeraList();

        final ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, l);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(ad);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                    String lista = ad.getItem(position); 
                    Intent intent = new Intent(gerenciar2.this, MainActivity.class);
                    intent.putExtra("tabbanco", lista);
                    gerenciar2.this.finish();
                    startActivity(intent);
            }
        });

    }

    private List<String> getgeraList() {
        List<String> l = new ArrayList<String>();
        cursor.moveToLast();
        int x=cursor.getCount();
        int y=0;
        while(y<x){ 
            l.add(retornadado());
            dadoanterior();
            y++;

        }
        return l;
    }
    
06.11.2014 / 16:35