listview中的按钮有帮助吗?

时间:2011-04-22 12:56:09

标签: android

我需要将按钮放入列表视图以转到另一个视图。

我的按钮:AddTime =(按钮)findViewById(R.id.Add_Time);

我试图让它使用:

public class myclass extends ListActivity实现了OnClickListener {

我有 public void onClick(查看arg0){     }

我需要它去另一个地方,我认为使用这个  Intent intent = new Intent(this,myclass.class);     startActivity(意向);

如何让这一切都在listview中工作?

4 个答案:

答案 0 :(得分:0)

使用此

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]);
        }
    }
}

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

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>

答案 3 :(得分:0)

我建议您使用带有字符串的列表视图。制作一个按钮数组,当你点击一个时,通过 onClick 获取数组中带有位置参数的按钮