slozano95
slozano95

Reputation: 278

Row selection ListView Android

my question is how can i do something when the user touches a row on the ListView

My app loads a json file and parses it using the Volley Library then everything is loaded nicely on a custom list row

But when I hit a row it does nothing

Really annoying thing ...

Im using a custom view and it has been impossible to assign the OnListItemClick

Here is my code

//all necessary libraries here

public class InicioPasajero extends Activity {

private static final String TAG = InicioPasajero.class.getSimpleName();

// Movies json url
private static final String url = "URL_RETURNING_JSON";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
ImageButton b_ajustes;
ImageButton b_filtros;

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

    b_ajustes= (ImageButton) findViewById(R.id.Bajustes);
    b_filtros= (ImageButton) findViewById(R.id.Bfiltros);
    b_ajustes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            Intent a=new Intent(InicioPasajero.this, MiPerfil.class);
            startActivity(a); 
        }
    });

    listView = (ListView) findViewById(R.id.list);



    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Cargando...");
    pDialog.show();

    // changing action bar color


    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("n"));
                            movie.setThumbnailUrl(obj.getString("i"));
                            movie.setRating(obj.getString("r"));
                            movie.setYear(obj.getString("h"));

                            // Genre is json array
                            JSONArray genreArry = obj.getJSONArray("g");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);

                            // adding movie to movies array
                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}
protected void onListItemClick(ListView movieList, View view, int posicion, long id) {
    Log.i("Sel:","si");
    // Hacer algo cuando un elemento de la lista es seleccionado
    TextView textoTitulo = (TextView) view.findViewById(R.id.title);

    CharSequence texto = "Seleccionado: " + textoTitulo.getText();
    Toast.makeText(getApplicationContext(), texto, Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}


/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}*/
@Override
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
         // no hacemos nada.
         return true;
     }

     return super.onKeyDown(keyCode, event);
 }

}

Thanks in advance.

Upvotes: 0

Views: 93

Answers (1)

stkent
stkent

Reputation: 20128

I don't see you calling setOnItemClickListener on your ListView anywhere? It also looks like you meant to have the Activity implement AdapterView.OnItemClickListener - then the overridden method named onItemClick would get called when a list item is clicked.

Upvotes: 2

Related Questions