Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overridePendingTransition not work in adapter(android)

I want run new Activity in Adapter use animation, but i have some error

06-19 09:54:29.253: E/AndroidRuntime(11464): FATAL EXCEPTION: main
06-19 09:54:29.253: E/AndroidRuntime(11464):
java.lang.ClassCastException: android.app.Application cannot be cast
 to android.app.Activity 06-19 09:54:29.253: E/AndroidRuntime(11464):
    at apdapters.NewsAdapter$3.onClick(NewsAdapter.java:134) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.view.View.performClick(View.java:3517) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 android.view.View$PerformClick.run(View.java:14155) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Handler.handleCallback(Handler.java:605) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Handler.dispatchMessage(Handler.java:92) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 android.os.Looper.loop(Looper.java:154) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 android.app.ActivityThread.main(ActivityThread.java:4624) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 java.lang.reflect.Method.invokeNative(Native Method) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 java.lang.reflect.Method.invoke(Method.java:511) 06-19 09:54:29.253:
 E/AndroidRuntime(11464):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
 06-19 09:54:29.253: E/AndroidRuntime(11464):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 06-19
 09:54:29.253: E/AndroidRuntime(11464):     at
 dalvik.system.NativeStart.main(Native Method)

Bottom my code, i dont know what its not work

holder.newsItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isOnline()) {
                    Intent i = new Intent();
                    i.setClass(getContext(), NewsDetali.class);
                    i.putExtra("id", ArrayListNews.get(position).getId());
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);

//error this line
                    ((Activity) context).overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

                } else {
                    Toast.makeText(getContext(), "Отсутствует подключение к интернету", Toast.LENGTH_LONG).show();
                }
            }
        });

UPD:

All code

public class NewsAdapter extends ArrayAdapter<NewsObject>{
    ArrayList<NewsObject> ArrayListNews;
    int Resourse;
    Context context;
    LayoutInflater vi;

    private ImageLoader imageLoader;
    final View imageLayout;
    //final ProgressBar  spinner;

    public NewsAdapter(Context context, int resource, ArrayList<NewsObject> objects, ImageLoader imageLoader) {
        super(context, resource, objects);

        ArrayListNews = objects;
        Resourse = resource;
        this.context = context;

        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageLoader = imageLoader;

        imageLayout = vi.inflate(R.layout.item_pager_image, null);
        //spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if(convertView == null) {
            convertView = vi.inflate(Resourse, null);
            holder = new ViewHolder();

            Typeface face=Typeface.createFromAsset(context.getAssets(), "font/AvenirNext-Regular.ttf");

            holder.imageNews = (ImageView) convertView.findViewById(R.id.imageNews);
            holder.nameNews = (TextView) convertView.findViewById(R.id.nameNews);
            holder.dateNews = (TextView) convertView.findViewById(R.id.dateNews);
            holder.newsItem = (RelativeLayout) convertView.findViewById(R.id.newsItemAdapter);
            holder.loadImg = (PlayGifView) convertView.findViewById(R.id.loading_bg);

            holder.nameNews.setTypeface(face);
            holder.dateNews.setTypeface(face);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        imageLoader.displayImage(ArrayListNews.get(position).getImageLink(), holder.imageNews, null, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String arg0, View arg1) {

            }

            @Override
            public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {

            }

            @Override
            public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {

                int color = Color.parseColor("#4D000000"); //The color u want             
                holder.imageNews.setColorFilter(color);
                holder.nameNews.setText(ArrayListNews.get(position).getName());
                holder.dateNews.setText(ArrayListNews.get(position).getDate());
            }

            @Override
            public void onLoadingCancelled(String arg0, View arg1) {
                Log.e("Script", "OnLoadeingStarted");
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String uri, View view, int current, int total) {
                Log.e("Script", "OnProgressUpdate" + uri + " " + total);
            }
        });




        holder.newsItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                    Intent i = new Intent(getContext(), NewsDetali.class);

                    i.putExtra("id", ArrayListNews.get(position).getId());
                    i.putExtra("title", ArrayListNews.get(position).getName());
                    i.putExtra("image", ArrayListNews.get(position).getImageLink());
                    i.putExtra("description", ArrayListNews.get(position).getDescription());
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(i);


                    //danger line!!! Error this
                    overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

            }
        });

        return convertView;
    }


    static class ViewHolder {
        public ImageView imageNews;
        public TextView nameNews;
        public TextView dateNews;
        public RelativeLayout newsItem;
        public PlayGifView loadImg;
    }



}
like image 736
c127hzbj5ep19hb Avatar asked Nov 29 '22 01:11

c127hzbj5ep19hb


2 Answers

This might help

Activity activity = (Activity) context;
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.act_start_in_from_right, R.anim.act_start_out_to_left);
like image 120
Neal Ahluvalia Avatar answered Dec 06 '22 10:12

Neal Ahluvalia


change like this code

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
getActivity().overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);

then receive like in newActivity

Bundle b = getIntent().getExtras();
int value = b.getInt("key");
like image 32
sasikumar Avatar answered Dec 06 '22 08:12

sasikumar