2012-10-08 43 views
0

我正在學習在android中編程,我一直堅持這個...有人應該知道如何使列表上的項目是可點擊的?我將通過傳遞ID參數來創建新視圖...setOnItemClickListener自定義列表視圖

非常感謝。

這是我的實際代碼:

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

}

+0

我很確定這些項目默認是可點擊的。你應該爲你的ListView設置'OnItemClickListener',就這些了。 –

回答

3

您可以在類中重寫onListItemClick,例如,像例如,你可以調用一些其他的活動:

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