bakusek
bakusek

Reputation: 135

Android Picasso Fragment Pic from URL

I've got problem to load pic from URL do my fragment by Picasso. When I start app it's start without any errors but pic is not loading. My app has one MainActivity and three fragments. Below I paste xml of mainlayout and one fragment class. The best for me is this scenario: when user click button on fragment three(red on image), pic will load from url to imageView which is located on fragment two (yellow on image) above of fragment three.three fragments Please help

package testowy.com.testowyfragment2;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 2015-07-04.
 */
public class Klasadown extends Fragment {
    private klasadownlistener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragmentdown, container, false);
        ImageView img = (ImageView) view.findViewById(R.id.imgVV);
        Context c = getActivity().getApplicationContext();
        Picasso.with(c).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
                .fit().into(img);

        View.OnClickListener clickListener = new View.OnClickListener() {

            public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.btnMenu:
                            updateText("Menu");
                            SetImage();
                            break;
                        case R.id.btnKontakt:
                            updateText("Kontakt");
                            break;
                        default:
                            break;
                    }
                }

            };
            Button btnMenu = (Button) view.findViewById(R.id.btnMenu);
            Button btnKontakt = (Button) view.findViewById(R.id.btnKontakt);

            btnKontakt.setOnClickListener(clickListener);
            btnMenu.setOnClickListener(clickListener);


            return view;

        }

            public interface klasadownlistener {
                public void onItemSelected(String txt);
            }

            private void updateText(String txt) {
                listener.onItemSelected(txt);
            }

            public void SetImage() {
                ImageView img = (ImageView) getView().findViewById(R.id.imgVV);
                Picasso.with(getActivity().getApplicationContext()).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").into(img);
            }

            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                if (activity instanceof klasadownlistener) {
                    listener = (klasadownlistener) activity;
                } else {
                    throw new ClassCastException(activity.toString() + " musi implementowa� interfejs: OverviewFragment.OverviewFragmentActivityListener");
                }
            }

        }


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <fragment
            android:id="@+id/fragmentup"
            class="testowy.com.testowyfragment2.Klasaup"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5">

        </fragment>

        <fragment
            android:id="@+id/fragmentcenter"
            class="testowy.com.testowyfragment2.Klasacenter"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3">

        </fragment>

        <fragment
            android:id="@+id/fragmetdown"
            class="testowy.com.testowyfragment2.Klasadown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:layout_weight="3">

        </fragment>

    </LinearLayout>

package testowy.com.testowyfragment2;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2015-07-04.
 */
public class Klasacenter extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.fragmentcenter, container, false);
        return view;
    }
    public void SetText(String txt){
        TextView view = (TextView) getView().findViewById(R.id.fragmentcenterText);
        view.setText(txt);

    }

    }

package testowy.com.testowyfragment2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;


public class MainActivity extends Activity implements Klasadown.klasadownlistener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public void onItemSelected(String txt) {
        Klasacenter fragment = (Klasacenter) getFragmentManager()
                .findFragmentById(R.id.fragmentcenter);

        // sprawdzamy czy fragment istnieje w tej aktywno�ci
        if (fragment != null && fragment.isInLayout()) {
            // ustawiamy teskt we fragmencie
            fragment.SetText(txt);
            ImageView img = (ImageView) findViewById(R.id.imgV);
            Picasso.with(this).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").fit().into(img);
        }
    }

    }

Upvotes: 2

Views: 5673

Answers (1)

bakusek
bakusek

Reputation: 135

It's stupid to say but I forget to add internet permission:)

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

now everything is ok!

Upvotes: 3

Related Questions