How to get the registry ID that is populated in Listview

Asked

Viewed 864 times

1

How do I get the Listview ID? Follow my shorthand code:

UPDATE

How do I get the REGISTRY ID that is populated by Sqlite in Listview.

public class MainActivity extends ActionBarActivity {
    private SQLiteDatabase database;
    private SimpleCursorAdapter dataSource;        
    
    private static final String[] campos = {"nomeObjeto", "nomePessoa", "_id"};
    
    ListView listView;
    BaseDAO helper;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        listView = (ListView) findViewById(R.id.lvObjetosEmprestados);
        
        helper = new BaseDAO(this);        
        database = helper.getWritableDatabase();
        AtualizarObjetos();
    }    
    
    
    public void AtualizarObjetos(){    
       dataSource = new SimpleCursorAdapter(this, R.layout.objetosemprestadosrow, objetosEmprestados, campos, new int [] {R.id.lblNomeObjeto, R.id.lblEmprestadoPara});
       listView.setAdapter(dataSource);            
    }
}
  • What do you mean by Listview ID? By the code posted is R.id.lvObjetosEmprestados

2 answers

2


I assume you are referring to the registration ID that corresponds to the line that is clicked.

For this you need to declare and assign a OnItemClickListener à ListView

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> a, View v, int pos, long id) {

        // id é o valor que você procura
    }
});
  • You’re making this mistake: The method setOnItemClickListener(Adapterview.Onitemclicklistener) in the type Adapterview<Listadapter> is not applicable for the Arguments (new Onitemclicklistener(){})

  • 1

    See if the import for the class OnItemClickListener which should be: import android.widget.AdapterView.OnItemClickListener;

  • Look, it worked out (:

0

By its code the id is R.id.lvObjectsEmpres. But you can get it by using this function that every view has: listview.getId();

Remembering that R.id.lvObjectsEmprestates must be in the activity_main layout and be from a listview. Otherwise it will break.

Browser other questions tagged

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