【问题标题】:How do I get the ID item in the database?如何获取数据库中的 ID 项?
【发布时间】:2016-05-06 14:08:48
【问题描述】:

我是 android 开发新手,必须在点击的项目数据库中获取 id 的值。 已经搜索了几个帖子,但没有找到答案。

下面是我的代码列表视图:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seleciona_jogador_act);

        final BancoController bc = new BancoController(this);
        final ArrayList<JogadorEntidade> jogadorEntidade = bc.arrayJogador(this);

        listView = (ListView) findViewById(R.id.lvSelecionar);

        final SelecionaAdapter adapter = new SelecionaAdapter(this, R.layout.adapter_seleciona, jogadorEntidade);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setBackgroundTintList(ColorStateList.valueOf(background));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertInserirJogador(SelecionaJogadorAct.this);
            }
        });


        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //I NEED THIS CODE
                
                Context context = getApplicationContext();
                CharSequence text = "ID: " + ", Posicao: " + position;
                int duration = Toast.LENGTH_SHORT;
                Toast.makeText(context, text, duration).show();

                //bc.deletaRegistro(id_player);

                Intent intent = getIntent();
                SelecionaJogadorAct.this.finish();
                startActivity(intent);
            }
        });
    }

我的适配器:

public class SelecionaAdapter extends ArrayAdapter<JogadorEntidade> {

    Context context;
    int layoutID;
    ArrayList<JogadorEntidade> alJogador;

    public SelecionaAdapter(Context context, int layoutID, ArrayList<JogadorEntidade> alJogador){
        super(context, layoutID, alJogador);
        this.context = context;
        this.alJogador = alJogador;
        this.layoutID = layoutID;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        PlayerHolder holder = null;

        if (row == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutID, parent, false);

            holder = new PlayerHolder();
            holder.txtNome = (TextView)row.findViewById(R.id.txtNomeSelecionar);
            holder.txtVitorias = (TextView)row.findViewById(R.id.txtVitóriasSelecionar);
            holder.txtDerrotas = (TextView)row.findViewById(R.id.txtDerrotasSelecionar);

            row.setTag(holder);
        }else {
            holder = (PlayerHolder)row.getTag();
        }

        JogadorEntidade jogadorEntidade = alJogador.get(position);
        holder.txtNome.setText(jogadorEntidade.getNome());
        holder.txtVitorias.setText("V: " + jogadorEntidade.vitórias);
        holder.txtDerrotas.setText("D: " + jogadorEntidade.derrotas);
        return row;
    }

    static class PlayerHolder{
        TextView txtNome;
        TextView txtVitorias;
        TextView txtDerrotas;
    }

JogadorEntidade:

public class JogadorEntidade {

    public String nome;
    public Integer id_jogador;
    public String vitórias;
    public String derrotas;

    public Integer getId_jogador() {
        return id_jogador;
    }

    public void setId_player(int id_player) {
        this.id_jogador = id_player;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getVitórias() {
        return vitórias;
    }

    public void setVitórias(String vitórias) {
        this.vitórias = vitórias;
    }

    public String getDerrotas() {
        return derrotas;
    }

    public void setDerrotas(String derrotas) {
        this.derrotas = derrotas;
    }

    public JogadorEntidade(String nome, String vitórias, String derrotas){
        super();
        this.nome = nome;
        this.vitórias = vitórias;
        this.derrotas = derrotas;
    }

    public JogadorEntidade(){}

public void insereJogador(JogadorEntidade jogadorEntidade) {

        db = banco.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(banco.NOME_JOGADOR, jogadorEntidade.getNome());
        values.put(banco.VITORIAS, jogadorEntidade.getVitórias());
        values.put(banco.DERROTAS, jogadorEntidade.getDerrotas());

        db.insert(CriaBanco.TABELA_JOGADOR, null, values);
        db.close();
    }
    

    public Cursor carregaJogador() {
        Cursor cursor;
        String[] campos = {banco.NOME_JOGADOR, banco.VITORIAS, banco.DERROTAS};
        db = banco.getReadableDatabase();
        cursor = db.query(banco.TABELA_JOGADOR, campos, null, null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        db.close();
        return cursor;
    }
    

    public void deletaRegistro(int id){
        String where = CriaBanco.ID_JOGADOR + "=" + id;
        db = banco.getReadableDatabase();
        db.delete(CriaBanco.TABELA_JOGADOR, where, null);
        db.close();
    }

    public ArrayList<JogadorEntidade> arrayJogador(Context context) {
        ArrayList<JogadorEntidade> al = new ArrayList<JogadorEntidade>();
        BancoController bancoController = new BancoController(context);
        Cursor cursor;
        cursor = bancoController.carregaJogador();

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                String nome = cursor.getString(cursor.getColumnIndex(banco.NOME_JOGADOR));
                String vitorias = cursor.getString(cursor.getColumnIndex(banco.VITORIAS));
                String derrotas = cursor.getString(cursor.getColumnIndex(banco.DERROTAS));

                JogadorEntidade jogadorEntidade = new JogadorEntidade(nome, vitorias, derrotas);

                al.add(jogadorEntidade);
                while (cursor.moveToNext()) {
                    nome = cursor.getString(cursor.getColumnIndex(banco.NOME_JOGADOR));
                    vitorias = cursor.getString(cursor.getColumnIndex(banco.VITORIAS));
                    derrotas = cursor.getString(cursor.getColumnIndex(banco.DERROTAS));
                    jogadorEntidade = new JogadorEntidade(nome, vitorias, derrotas);
                    al.add(jogadorEntidade);
                }
            }
        }
        return al;
    }

【问题讨论】:

  • 数据库控制器的代码在哪里?
  • @cricket_007 我正在用数据库控制器更新我的帖子,你能帮我吗?

标签: android listview android-arrayadapter onitemclicklistener


【解决方案1】:

首先您需要在查询时请求 id(如果 id 是在数据库中创建的,则列 mame 是 _id 或者您可以使用 tablename._id 或任何您需要的):

String[] campos = {"_id", banco.NOME_JOGADOR, banco.VITORIAS, banco.DERROTAS};

那么需要在读取游标的时候给对象加上id:

public ArrayList<JogadorEntidade> arrayJogador(Context context) {
    ArrayList<JogadorEntidade> al = new ArrayList<JogadorEntidade>();
    BancoController bancoController = new BancoController(context);
    Cursor cursor;
    cursor = bancoController.carregaJogador();

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            String nome = cursor.getString(cursor.getColumnIndex(banco.NOME_JOGADOR));
            String vitorias = cursor.getString(cursor.getColumnIndex(banco.VITORIAS));
            String derrotas = cursor.getString(cursor.getColumnIndex(banco.DERROTAS));
            long id = cursor.getLong(cursor.getColumnIndex("_id",0));

            JogadorEntidade jogadorEntidade = new JogadorEntidade(id, nome, vitorias, derrotas);

            al.add(jogadorEntidade);
            while (cursor.moveToNext()) {
                nome = cursor.getString(cursor.getColumnIndex(banco.NOME_JOGADOR));
                vitorias = cursor.getString(cursor.getColumnIndex(banco.VITORIAS));
                derrotas = cursor.getString(cursor.getColumnIndex(banco.DERROTAS));
            long id = cursor.getLong(cursor.getColumnIndex("_id",0));
                jogadorEntidade = new JogadorEntidade(id, nome, vitorias, derrotas);
                al.add(jogadorEntidade);
            }
        }
    }
    return al;
}

无论如何,这不是 android 的方式。您应该阅读所有列表,然后显示它。您应该使用 viewholder 模式,并且只在您要显示它时才加载播放器。此外,您应该移至回收站视图,而不是使用列表。此时可以考虑弃用列表视图。看教程:http://developer.android.com/training/material/lists-cards.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2019-12-03
    • 2020-03-06
    相关资源
    最近更新 更多