有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java setOnItemClickListener自定义列表视图

我正在学习用安卓系统编程,我一直坚持着这个。。。有人应该知道如何使列表上的项目可点击?我将通过传递id参数创建一个新视图

非常感谢

这是我的实际代码:

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) 个答案