emmgfx
emmgfx

Reputation: 575

setOnItemClickListener custom Listview

I'm learning to program in android and I've stuck with this ... Someone should know how to make the items on the list are clickable? I will make a new view by passing the id parameter ...

Thank you very much.

This is my actual code:

public class ListaLugares extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_lugares);
    ArrayList<Lugar> Lugares = getItems();
    setListAdapter(new LugarAdapter(this, R.layout.lista_item, Lugares));
}

public ArrayList<Lugar> getItems() {
    DatabaseHandler db = new DatabaseHandler(this);
    ArrayList<Lugar> listaLugares = db.getAllLugares2();
    db.close();
    return listaLugares;
}

private class LugarAdapter extends ArrayAdapter<Lugar> {

    private ArrayList<Lugar> items;

    public LugarAdapter(Context context, int textViewResourceId, ArrayList<Lugar> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.lista_item, null);
        }
        Lugar lugar = items.get(position);
        if (lugar != null) {
            TextView tnombre = (TextView) v.findViewById(R.id.nombre);
            TextView tdescripcion = (TextView) v.findViewById(R.id.descripcion);
            if (tnombre != null) {
                tnombre.setText(lugar.getNombre());
            }
            if (tdescripcion != null) {
                tdescripcion.setText(lugar.getDescripcion());
            }
        }
        return v;
    }
}

}

Upvotes: 0

Views: 199

Answers (1)

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

You can override onListItemClick within your class, for instance, like in example you can call some other Activity:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Bundle positionBundle = new Bundle();
    positionBundle.putInt("position", position);
    Intent i = new Intent(this, MyOtherActivity.class);
    i.putExtras(positionBundle);
    startActivity(i);
}

Upvotes: 3

Related Questions