How to make a Listview "Clickable"?

Asked

Viewed 1,212 times

2

I have a simple phone book system where there’s Activitys to:

  • Register;
  • Edition of the register;
  • Removal of the register;
  • And one last to see all contacts within one ListView.

All records come from a table in the database. I would like to know how to access each record when tapping a record line in the list.

package com.br.projetoeducadastro;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Contatos extends Activity {

    private SQLiteDatabase db;
    private CursorAdapter dataSource;

    private static final String campos[] = {"_id", "nome", "telefone"};

    ListView list;
    SQLiteHelper helper;
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Contatos.this, android.R.layout.simple_list_item_1);
    TextView tvNome, tvId, tvTelefone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contatos);

        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);

        helper = new SQLiteHelper(this);
        db = helper.getWritableDatabase();

        Cursor c = db.query("clientes", campos, null, null, null, null, null);

        if (c.getCount() > 0) {
            dataSource = new SimpleCursorAdapter(this, R.layout.row, c, campos,
                    new int[]{R.id.tvId, R.id.tvNome, R.id.tvTelefone});
            list.setAdapter(dataSource);

        } else {
            Toast.makeText(this, "Nenhum Registro Encontrado", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Contatos.this, Index.class);
            startActivity(intent);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
  • You need to create a Listener. http://stackoverflow.com/questions/10295226/how-to-create-listview-onitemclicklistener

  • Hi Lucas Moresco, thanks for your help, but I don’t understand why my list is wrong in this passage: list.setOnItemClickListener(new Adapterview.Onitemclicklistener() { @Override public void onItemClick(Adapterview<?> arg0, View arg1, int arg2, long Arg3) { Log.d("##","Items " + list[arg2] ); } }); }

2 answers

2

You must create a Istener this way:

    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.d("##","Items " +  list[arg2] );
        }
    });
  • Lucas Moresco, thanks for the help, but my "list[arg2]" is giving error, could explain me?

  • I’ve got it all worked out, thanks!

1

I recommend that you work with multiple Ragments in an Activity, rather than multiple Activity. These codes will work within a Fragment, in an Activity will need some modifications.

Add this line to Oncreateview():

list.setAdapter(new ListAdapter(getActivity);

Add these lines:

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

        public ListAdapter(Context c) {
            inflater = LayoutInflater.from(c);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return c.getCount(); //
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null ){

                convertView = View.inflate(getActivity(), R.layout.linha_listview, null);

                viewHolder = new ViewHolder();
                viewHolder.TextView1 = (TextView) convertView.findViewById(R.id.textview1);
                viewHolder.TextView2 = (TextView) convertView.findViewById(R.id.textview2);                    

                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("onClick - position",     String.valueOf(position));
                        Fragment registro = new VerRegistro();                            

                        Bundle bundle = new Bundle();

                        bundle.putString("id", String.valueOf(position));

                        registro.setArguments(bundle);
                        FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.replace(R.id.fragment_container, registro);
                        ft.addToBackStack(null);
                        ft.commit();

                    }
                });
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }        
            return convertView;
        }

        private class ViewHolder {
            TextView TextView1;
            TextView TextView2;
        }
    }

Note that the listview line will have a layout of its own. Clicking the Verregistro Ragment will be called.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.