行选择ListView Android

时间:2014-10-09 01:22:30

标签: java android listview android-listview

我的问题是,当用户触摸ListView

上的一行时,我该怎么办?

我的应用程序加载一个json文件并使用Volley Library解析它,然后所有内容都很好地加载到自定义列表行

但是,当我连续排它时什么都不做

真烦人的事......

我使用自定义视图并且无法分配OnListItemClick

这是我的代码

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

}

提前致谢。

1 个答案:

答案 0 :(得分:2)

我没有看到你在ListView的任何地方调用setOnItemClickListener?看起来你的意思就是让Activity实现AdapterView.OnItemClickListener - 然后在单击列表项时调用名为onItemClick的重写方法。