I need you to return your name by clicking on the list

Asked

Viewed 72 times

1

I’m recreating one of my activitys to work with Action Bar, with a lot of 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 content 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

        }
    });

error. also can’t get the contents of the list.

  • I could tell you what the mistake is?

  • Activitythread.performLaunchActivity(Activitythread$Activityclientrecord, Intent) line: 2110

  • @Joannis This is not the error, it is the method where the error occurs. The error log comprises several lines of the logcat and must be pasted into the question itself (to do so edit the question and paste the log lines).

  • does not roll more because I changed the program already and this working. thanks!

2 answers

1

With great cost I got the following way

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;
    }
  • An important question: did you understand the difference between onItemClickListener and the onClickListener ?

  • no. = ( for list onclicklistener does not work but I do not know why if you can explain me I thank you

  • 1

    I will answer with the explanation!

1


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

The Onclicklistener serves for the entire View, no matter what item you click on.

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

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();
    }
});

Browser other questions tagged

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