2012-10-08 3 views
0

나는 안드로이드에서 프로그래밍하는 것을 배우고 있으며 이것으로 붙어 있습니다 ... 누군가가 목록의 항목을 클릭 할 수있게 만드는 방법을 알아야합니까? id 매개 변수를 전달하여 새로운 뷰를 만들 것입니다 ...setOnItemClickListener custom 목록보기

대단히 고마워요.

이 내 실제 코드 :

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

I을 항목들이 기본적으로 클릭 가능하다는 것을 확신합니다. 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); 
} 
관련 문제