Android Sqlite: Displaying database contents in textView?

2

I'd like to know how I display content from the Sqlite database in textviews. I'm using the following method to display in a CustomListView:

    public Cursor getAllData ()
{
    String buildSQL = "SELECT * FROM " + DatabaseHelper.DiferencaPercentual.TABELA; //pega todos os dados da tabela
    return database.rawQuery(buildSQL, null);
}

And then I refer this to an adapter saying what fields of my custom list view that is every thing:

    @Override
public void bindView(View view, Context context, Cursor cursor) 
{
    TextView textViewPersonName = (TextView) view.findViewById(R.id.tv1);
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv2);
    textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}

And then I pass this all to the activity, in the onCreate method:

        new Handler().post(new Runnable() {
        @Override
        public void run() {
            //customAdapter = new CustomCursorAdapter(ListarCliente.this, clienteDAO.findById(51));
            customAdapter = new ClienteAdapter(ListarCliente.this, clienteDAO.getAllData());
            listView.setAdapter(customAdapter);
        }
    });

And in a separate method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)
    {
        clienteDAO.insertData(data.getExtras().getString("tag_nome"),
                              data.getExtras().getString("tag_email"));

        customAdapter.changeCursor(clienteDAO.getAllData());
    }
}

My question is, does not it have a simpler way for me to grab the contents of the database and quickly display a textView, just for the user to be able to edit the information?

Thank you for your attention!

    
asked by anonymous 25.01.2015 / 02:07

1 answer

2

I'll give you an example that I'm sure works. Since you did not show your complete code, I'll present a form, you should adapt it and study it:

1 - You need to make each field of your listView clickable, so that the information of each item can be passed via putExtra(); individually- EXAMPLE:

...
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

...
...

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
                TextView idTextView = (TextView) view.findViewById(R.id.id);
                TextView titleTextView = (TextView) view.findViewById(R.id.titulo);
                TextView descTextView = (TextView) view.findViewById(R.id.desc);

                String id = idTextView.getText().toString();
                String titulo = titleTextView.getText().toString();
                String desc = descTextView.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(), ModifyTodoActivity.class);
                modify_intent.putExtra("titulo", titulo);
                modify_intent.putExtra("desc", desc);
                modify_intent.putExtra("id", id);

                startActivity(modify_intent);
            }
        });
    }

2 - To display a TextView create a new activity and make this logic:

Viewing Activity.class :

...
...

//título do texto.   
        titleText = (TextView) findViewById(R.id.subject_edittext);

//descrição do texto. 
        descText = (TextView) findViewById(R.id.description_edittext);


//RECEBE OS VALORES DA ATIVIDADE PRINCIPAL

        Intent intent = getIntent();
        String name = intent.getStringExtra("titulo");
        String desc = intent.getStringExtra("desc");

    //EXIBINDO NO TEXTVIEW, CONFORME VOCÊ PEDIU - NOME E DESCRIÇÃO SÃO EXEMPLOS

            titleText.setText(name);
            descText.setText(desc);

...
}

If you want to edit the fields, follow the logic below:

2 - You will need to create an activity that will only serve to modify the fields, it is a tip, therefore the client will be directed to an editing screen. DO NOT NEED SELECT, everything will be passed by parameters:

ModifyingActivity.class :

public class ModificandoActivity extends Activity implements OnClickListener {

...
...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

...
//Abra o banco
//NÃO SE PREOCUPE COM SELECT, OS TEXTOS VIRÃO POR getStringExtra();
//Abrir o banco é somente para dar o update e delete, CASO VOCÊ ASSIM QUEIRA

//título do texto. Aqui abrimos um EditText para que ele possa ser editado     
        titleText = (EditText) findViewById(R.id.subject_edittext);

//descrição do texto. Aqui abrimos um EditText para que ele possa ser editado
        descText = (EditText) findViewById(R.id.description_edittext);

//Botão update. Aqui abrimos um Button para para salvar as mudanças

        updateBtn = (Button) findViewById(R.id.btn_update);

//Botão delete. Aqui abrimos um Button para para deletar a o texto NO BANCO
        deleteBtn = (Button) findViewById(R.id.btn_delete);

//RECEBE OS VALORES DA ATIVIDADE PRINCIPAL

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String name = intent.getStringExtra("titulo");
        String desc = intent.getStringExtra("desc");

        _id = Long.parseLong(id);

//EXIBINDO NO EDITTEXT - NOME E DESCRIÇÃO SÃO EXEMPLOS

        titleText.setText(name);
        descText.setText(desc);

        updateBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
    }

//ESSA PARTE É FUNDAMENTAL, POIS O CLIENTE PODERÁ DELETAR OU DAR UPDATE
//DEPOIS QUE ELE ESCOLHER, O CASE IRÁ SETAR O NOVO VALOR OU DELETAR

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_update:

//SETANDO O NOVO TEXTO NA STRING
            String titulo = titleText.getText().toString();
            String desc = descText.getText().toString();

//UPDATE NO BANCO É UM EXEMPLO

            dbManager.update(_id, titulo, desc);
            this.returnHome();
            break;

        case R.id.btn_delete:
//ele pega o ID, busca no banco e deleta a coluna referente (é somente uma lógica)
            dbManager.delete(_id);
            this.returnHome();
            break;
        }
    }
    
25.01.2015 / 02:23