Error setItemOnClickListener after click

Asked

Viewed 53 times

1

I am having a problem in the application where after I click some row of mine ListView, the application simply crashes (unable to see the error in Debugger).

The idea is after clicking on ListView the line data go to the EditText in my other Activity.

Follow the code below:

    public class FillList extends AsyncTask <String,String,String> {
    String z="";
    List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();
    @Override public void onPreExecute()
    {
        progbar2.setVisibility(View.VISIBLE);
    }

    @Override public  void onPostExecute(String r){
        progbar2.setVisibility(View.GONE);
        //Toast.makeText(MegaPermanentes_Usuarios.this, r, Toast.LENGTH_SHORT).show();

        String[] from = {"A", "B"};
        int[] views = {R.id.lblproname, R.id.lblproend};
        final SimpleAdapter ADA = new SimpleAdapter(MegaPermanentes_Usuarios.this, prolist, R.layout.lsttemplate, from, views);
        listPro.setAdapter(ADA);

        listPro.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Bundle params = new Bundle();
                String resposta = (String) parent.getAdapter().getItem(position);
                Intent it = new Intent(MegaPermanentes_Usuarios.this, MegaPermanentes.class);
                it.putExtra("nome",resposta);
                startActivity(it);

            }
        });


    }

    @Override
    public String doInBackground(String... params) {

        try{
            Connection con = connectionClass.connectionclass();
            if(con == null){
                z = "Conexão falhou";
            }else
                {
                String query = "select nome,endereco from usuarios";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();

                while(rs.next()){
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString ("Nome"));
                    datanum.put("B", rs.getString("Endereco"));
                    prolist.add(datanum);
                }


            }


        }catch (Exception ex)
        {
            z = "Error Retrieving Data";
        }
        return z;
    }


}

Here’s the part of onItemClickListener:

 listPro.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Bundle params = new Bundle();
                String resposta = (String) parent.getAdapter().getItem(position);
                Intent it = new Intent(MegaPermanentes_Usuarios.this, MegaPermanentes.class);
                it.putExtra("nome",resposta);
                startActivity(it);

            }
        });
  • try to recover the String answer = ADA.getItem(position)

  • Would look like this? String resposta = (String) ADA.getItem(position);? And yet you’re crashing.

  • The class Megapermanent is registered on Androidmanifest.xml?

  • @Leonardodias, yes: </activity>&#xA; <activity android:name=".MegaPermanentes" />&#xA; <activity android:name=".MegaPermanentes_Usuarios"></activity>

  • Try to do it that way then: String response = (String) (listPro.getItemAtPosition(position));

  • Yes, actually it would be String answer = (String) ADA.getItem(position). get("A"); Because getItem is returning you a Hashmap

  • @Andreozawa would be .get("A") same? In AS did not recognize this syntax.

  • @Leonardodias without advance also, continues crashing at the same time that I click on ListView.

Show 3 more comments

1 answer

0

By giving yourself to realize, you create an object using the Map and inserts into each item of your ListView. Therefore, to recover the data, you should also use the Map. See below for an example:

// recuperando o valor do item após o click
Map<String, String> obj = (Map<String, String>) getAdapter().getItem(position);
// recuperando o nome
String name = (String) obj.get("A");
// recuperando o endereço
String address = (String) obj.get("B");
  • I’m unable to use the method getAdapter after the (Map<String, String)

Browser other questions tagged

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