Android中列表片段中的按钮监听器

时间:2015-04-21 10:37:39

标签: android xml android-fragments onclicklistener

我正在开发一个Android应用程序,我在其中使用自定义baseadapter和ListFragment。我想要做一个事情,如果我按下我的按钮然后它会向我显示任何类似于startActivity的事件,但是当我在baseAdapter中使用它时它会给我一个“startActivity”不在的错误基础适配器的范围。

我的代码如下。

我正在使用基本适配器的ListFragment:

    public class MyBaseAdapter extends BaseAdapter {

    ArrayList<ListData> myList = new ArrayList<ListData>();
    LayoutInflater inflater;
    Context context;
    Button btn;

    public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
        this.myList = myList;
        this.context = context;

        inflater = LayoutInflater.from(this.context);    // only context can also be used
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        mViewHolder.btn1 = detail(convertView, R.id.tvTitle);

            if(myList.get(position).getTitle().equals("Title 1")) {
                  btn.setVisibility(View.VISIBLE);
              }else{
                  btn.setVisibility(View.GONE);
              }
//error: The method startActivity(Intent) is undefined for the type MyBaseAdapter           
            startActivity(new Intent(context.getApplicationContext(), AlertMyNetwork.class));

             mViewHolder.btn1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
  // toast is working perfectly                 
                    Toast.makeText(context, "adsadads", 1).show();
//error: The method startActivity(Intent) is undefined for the type click Listener
                    startActivity(new Intent(context.getApplicationContext(), AlertMyNetwork.class));

//          Intent i = new Intent(context, AlertMyNetwork.class);
//          startActivity(i);
//          
//          context.startActivity(context.getApplicationContext() , AlertMyNetwork.class);


                }
            });




        mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
        mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
        mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());

        return convertView;
    }

    // or you can try better way
    private TextView detail(View v, int resId, String text) {
        TextView tv = (TextView) v.findViewById(resId);
        tv.setText(text);
        return tv;
    }

    private Button detail(View v, int resId){

        btn = (Button) v.findViewById(R.id.button1);
        return btn;
    }

    private ImageView detail(View v, int resId, int icon) {
        ImageView iv = (ImageView) v.findViewById(resId);
        iv.setImageResource(icon); //

        return iv;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
        Button btn1;
    }

}

我的基本适配器代码:

public class MyBaseAdapter extends BaseAdapter {

    ArrayList<ListData> myList = new ArrayList<ListData>();
    LayoutInflater inflater;
    Context context;
    Button btn;

    public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
        this.myList = myList;
        this.context = context;

        inflater = LayoutInflater.from(this.context);    // only context can also be used
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        mViewHolder.btn1 = detail(convertView, R.id.tvTitle);

            if(myList.get(position).getTitle().equals("Title 1")) {
                  btn.setVisibility(View.VISIBLE);
              }else{
                  btn.setVisibility(View.GONE);
              }

        mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
        mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
        mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());

        return convertView;
    }

    // or you can try better way
    private TextView detail(View v, int resId, String text) {
        TextView tv = (TextView) v.findViewById(resId);
        tv.setText(text);
        return tv;
    }

    private Button detail(View v, int resId){

        btn = (Button) v.findViewById(R.id.button1);
        return btn;
    }

    private ImageView detail(View v, int resId, int icon) {
        ImageView iv = (ImageView) v.findViewById(resId);
        iv.setImageResource(icon); //

        return iv;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
        Button btn1;
    }

}

我的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"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:padding="10dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />

    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/star1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Title" />

        <TextView
            android:id="@+id/tvDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Description" />
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

context.startActivity(intent);班级

中尝试BaseAdapter

答案 1 :(得分:1)

如果您想从startActivity拨打BaseAdapter,请使用以下代码:

context.startActivity(yourIntent);.

如果您想在ListFragment内打电话,请按以下方式拨打电话:

getActivity().startActivity(yourIntent);.

尝试以下可在BaseAdapter

中正常运行的代码
context.startActivity(new Intent(context.getApplicationContext(), AlertMyNetwork.class));

mViewHolder.btn1.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {         

          context.startActivity(new Intent(context.getApplicationContext(), AlertMyNetwork.class));

          //Intent i = new Intent(context, AlertMyNetwork.class);
          //context.startActivity(i);

          context.startActivity(context.getApplicationContext() , AlertMyNetwork.class);
    }
});