Akari
Akari

Reputation: 856

how to use a custom button in a list to redirect the user to another activity

I've written a big code and I've used a list view within one of my task layouts :

enter image description here

I want to know how I can make a listener for each update button in this case in order to move to another activity , I know how to use more than one activity and moves between them But I want to know how to make a listener only .

The custom adapter code :

package com.example.task_9;

import java.util.ArrayList;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class TempLyaout extends ArrayAdapter<String> {

    Context context;
    ArrayList<String> sa;


    public TempLyaout(Context context , ArrayList<String> sa) {
        super(context,R.layout.temp,sa);
        this.context=context;
        this.sa = sa;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View rowView = l.inflate(R.layout.temp, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
        Button update = (Button) rowView.findViewById(R.id.button1);


//              myDb.open();
//              Cursor cursor= myDb.getRowByName(sa.get(position));
//              myDb.updateRowByName(cursor.getInt(DBAdapter.COL_ROWID), sa.get(position), cursor.getString(DBAdapter.COL_PASSWD), cursor.getInt(DBAdapter.COL_AGE), isAdmin);
//              myDb.close();




        textView.setText(sa.get(position));


        return rowView;
    }






}

This is the related class :

package com.example.task_9;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class UsersActivity extends Activity {

    Intent redirect;
    ListView list;
    ArrayList<String> sa;
    DBAdapter myDb;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    @Override
    protected void onStart() {

        super.onStart();

        setContentView(R.layout.users);

        list= (ListView) findViewById(R.id.usersList);

        redirect = getIntent();

        openDB();

        /*********************************************/


        Cursor cursor = myDb.getAllRows();

        cursor.moveToFirst();

        ArrayList<String> ha = new  ArrayList<String>();

        while(cursor.moveToNext()) {

            ha.add(cursor.getString(DBAdapter.COL_NAME));

        }

        cursor.close();


         @SuppressWarnings("rawtypes")
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ha);

         list.setAdapter(new TempLyaout(UsersActivity.this,ha));


            list.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        Toast.makeText(getApplicationContext(),
          "Click ListItem Number " + position, Toast.LENGTH_LONG)
          .show();

      }

    }); 



    } // end onStart method

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    protected void onDestroy() {
        super.onDestroy();  
        closeDB();

    }// end onDestroy method



   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private void openDB() {
        myDb = new DBAdapter(this);
        myDb.open();
    } // end openDB method 

  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private void closeDB() {
        myDb.close();
    } // end closeDB method 


///////////////////////////////////////////////////////////////////////////////////////////////////////////////

}// end main class 

Upvotes: 2

Views: 995

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

In your getView

       Button update = (Button) rowView.findViewById(R.id.button1);
       update.setOnClickListener(mClickListener);

Then

        private OnClickListener mClickListener = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(ListActivtiy.this,SecondActivity.class);
                startActivity(intent); 
        }
    }; 

Also use a viewholder for smooth scrolling and performance

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Upvotes: 1

Related Questions