How to make a ListView "Clickable"?

2

I have a simple phonebook system, where there is Activity s for:

  • Registration;
  • Editing the registry;
  • Removing the registration;
  • And one last to see all contacts within a 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();
    }
}
    
asked by anonymous 13.02.2015 / 19:44

2 answers

2

You should create a listener, like this:

    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.d("##","Items " +  list[arg2] );
        }
    });
    
14.02.2015 / 16:29
1

I recommend that you work with multiple fragments in one activity, rather than multiple activity. These codes will work inside a fragment, in an activity will need some modifications.

Add this line in 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 its own layout. When you click the fragment, the Verify registry will be called.

    
27.02.2015 / 03:53