Capture the data corresponding to the index in the bank

5

I was able to feed my listview with SQLite data, however, I now want to capture the data (in the database) that each row in the listview matches.

For example:

Line 10 displays Code 1 and Name: Philip
Line 13 displays Code 4 and Name: Giovana

For example, there are 2 rows in the list, the method I was able to use, returned a String with the complete line. As soon as I click on a row in the listview, I want it to return the code (Cod.4, for example) that is being shown in the String stored in the adapter. For example, I want to click on line 10 and let me know that the data it is displaying corresponds to the bank's code 1 record.

Since there are 2 columns that are being displayed, I want a way to capture them separately.

I'm feeding lisview like this:

while(data.moveToNext()){
    //coluna 1 do banco

    theList.add("Cód.: " + data.getString(0) + "      " + "Nome: " + data.getString(1));
    ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
    listView.setAdapter(listAdapter);
}
    
asked by anonymous 13.01.2017 / 20:28

1 answer

1

You must implement the onItemClickListener() method, using the position returned ( int position ) by the adapter, retrieve the String. Then use Regex to extract the number that refers to the code and search for it Do you want the bank:

private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        // extrai com regex o codigo
        Pattern p = Pattern.compile("\s(\d+)\s");
        Matcher m = p.matcher(parent.getSelectedItem());
        boolean found = m.find();
        String cod = "";
        if (found){
            cod = m.group(1); // testa se é este grupo mesmo ou 0
        }else{} // Algum tratamento de erro

        // Faça a busca no banco através de uma Asynctask<> por exemplo.

    }
};

listView.setOnItemClickListener(mMessageClickedHandler);

Ready, the variable cod has the registry code in the database and you can use it to search the database.

PS. If you'd like to test some default regex , use this site , I recommend.

    
17.02.2017 / 01:50