bobinthehouse
bobinthehouse

Reputation: 31

buttons in listview help?

I need to put button into list view to go to another view.

my button: AddTime = (Button) findViewById(R.id.Add_Time);

i tryd to get it working using:

public class myclass extends ListActivity implements OnClickListener {

and i have public void onClick(View arg0) { }

and i need it to go to another place i think using this Intent intent = new Intent(this, myclass.class); startActivity(intent);

how to i get this all working in listview?

Upvotes: 0

Views: 123

Answers (4)

DynoZ
DynoZ

Reputation: 169

I suggest you to use a list view with strings. Make a buttons array and when you click on one, via onClick get the button in the array with the position parameter

Upvotes: 0

Jazz
Jazz

Reputation:

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:text="@+id/TextView01" android:id="@+id/txtRow"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <Button android:id="@+id/btnRow" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Click "></Button>
</LinearLayout>

Upvotes: 0

Jazz
Jazz

Reputation:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ListView android:id="@android:id/list" android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

Upvotes: 0

Jazz
Jazz

Reputation:

use this

package example.buttonWithList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class buttonWithList extends ListActivity {
    /** Called when the activity is first created. */
    String[] items={"azhar","j"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(new bsAdapter(this));


    }
    public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        public bsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;

        }

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

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

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

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

            }*/
                LayoutInflater inflater=cntx.getLayoutInflater();
                row=inflater.inflate(R.layout.row, null);
                TextView tv=(TextView)row.findViewById(R.id.txtRow);
                Button btn=(Button)row.findViewById(R.id.btnRow);
                tv.setText(items[position]);
                btn.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        onListItemClick(this, this, position, null);
                    }
                    });


            return row;
        }

        protected void onListItemClick(OnClickListener onClickListener,
                OnClickListener onClickListener2, int position, Object object) {
            // TODO Auto-generated method stub
            //Toast.makeText(this, items[position], 3000);
            System.out.println(items[position]);
        }
    }
}

Upvotes: 0

Related Questions