0

I am working on an Android application in which I am using my custom baseadapter and ListFragment. I want to make a thing that, if I press my button then it will show me any event like to startActivity, but when I am using it in baseAdapter it is giving me an error that "startActivity" is not in a scope of base adapter.

My code is given below.

My ListFragment where I am using base adapter:

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

}

My Base Adapter code:

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

}

My XML code:

<?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>
5
  • you need to use context reference to call startActivity Commented Apr 21, 2015 at 10:40
  • @HimanshuAgarwal Can you please give me an example here for this. Commented Apr 21, 2015 at 10:44
  • update your code and show how and where are you calling startActivity Commented Apr 21, 2015 at 10:48
  • Try passing getActivity ().getApplicationcontext () in base adapter constructor instead of getBasecontext Commented Apr 21, 2015 at 10:51
  • @HimanshuAgarwal i have updated my code please check it out. I am facing a same problem here. Commented Apr 21, 2015 at 10:58

2 Answers 2

1

Try context.startActivity(intent); in BaseAdapter Class

Sign up to request clarification or add additional context in comments.

1 Comment

also try passing getActivity ().getApplicationcontext () in base adapter constructor instead of getBasecontext()
1

If you want to call startActivity from BaseAdapter then use below code:

context.startActivity(yourIntent);.

If you want to call inside your ListFragment then call it like this:

getActivity().startActivity(yourIntent);.

Try below code that will work fine in your BaseAdapter class

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

2 Comments

it is giving me this error by calling this: The method startActivity(Intent) in the type Context is not applicable for the arguments (Class<AlertMyNetwork>)
@UKhan try above code use context before startActivity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.