-1

I'm trying to change src of ImageView from inside non-activity class and display Error dialog message at the same time, the dialog message is appeared but without any changes on the src of ImageView this is my code by using inflater, I don't know what is the problem.

 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate( R.layout.control_fragment, null );

        ImageView imgView = (ImageView)view.findViewById(R.id.okimg);
        imgView.setImageResource(R.drawable.clear);

Updated: Complete class

public class AddressControl extends AddressControlTask {

private View anchorView;

// You must send a context
public static void run(Context context, View anchorView, String zipCode, String houseNumber) {
    new AddressControl(context, anchorView).execute(zipCode, houseNumber);
}

private AddressControl(Context context, View anchorView) {
    super(context);
    this.anchorView = anchorView;
}

@Override
public Class<? extends AbstractTask> getTaskClass() {
    return getClass();
}

@Override
protected void onPostExecute(final List<Address> result) {
    if (result.size() == 0) {
        result.add(null);


        destroyDialog();

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View view = inflater.inflate( R.layout.control_address_fragment, null );

        ImageView okaddress = (ImageView)view.findViewById(R.id.address_ok);

        okaddress.setImageResource(R.drawable.clear);



        final CustomAlertDialog alertDialog = new CustomAlertDialog(getContext());
        alertDialog.setPositiveButton("Ok", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
        alertDialog.setTitle("Wrong");

        alertDialog.setText("Address is not correct");
        alertDialog.show();
        super.onPostExecute(result);
    } if(result.size() == 1) {
        super.onPostExecute(result);
    } else {
        destroyDialog();
        Context context = getContext();
        CustomSelectDialog dialog = new CustomSelectDialog(context, anchorView);
        dialog.setOnItemSelectListener(new CustomSelectDialog.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String text, Object value) {
                List<Address> addressList = new ArrayList<>();
                addressList.add((Address) value);
                onPostExecute(addressList);
            }
        });
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                onPostExecute(new ArrayList<Address>());
            }
        });
        for(Address address : result) {
            dialog.addItem(StringUtils.getAddressText(address), address);
        }
        dialog.setTitle("Enter true address, please");
        dialog.show();
    }
}
}
Samah Ahmed
  • 419
  • 8
  • 24
  • 1
    Please share more code. Where are you adding your imageview in the code? – alDiablo Mar 30 '17 at 10:28
  • May be because both view will be different object. inflated one and device rendered – Kishore Jethava Mar 30 '17 at 10:29
  • Post some of the Activity's code where you want to change image. – Abid Khan Mar 30 '17 at 10:39
  • The layout you are inflating here needs to be somewhere in Activity class. If you want to change the Image inside the Activity class you are calling `AddressControl` class from? then it won't work in the way you are trying. Instead you have to access the image object from Activity. – Abid Khan Mar 30 '17 at 10:46
  • Why super.onPostExecute(result); is not the first line in the onPostExecute()? – Dhrumil Shah - dhuma1981 Mar 30 '17 at 10:52
  • @SamahAhmed Do you want to change the Image inside the Activity class you are calling the AddressControl class from? – Abid Khan Mar 30 '17 at 11:01
  • @AbidKhan if I'm calling AddressControl class from Fragment like that , how I can know if the result 0 will change this icon to clear icon! This is the reason I need to change icon from inside AddressControl class. AddressControl.run(getActivity(), itbControllAddress, zipCode, houseNumber); – Samah Ahmed Mar 30 '17 at 11:04
  • Ok where this icon are you showing in the fragment right? and on success of task? – Abid Khan Mar 30 '17 at 11:08

3 Answers3

1
runOnUiThread(new Runnable() {

   @Override
   public void run() {
       LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View view = inflater.inflate( R.layout.control_address_fragment, null );

    ImageView okaddress = (ImageView)view.findViewById(R.id.address_ok);

    okaddress.setImageResource(R.drawable.clear);
   }
});

Move your UI changes to MainThread. Only these will be reflected

Make use of functino runOnUIThread() this will channel your instructions to mainthread and thus will update UI. You can also move your UI related logic in this api

Neji
  • 6,591
  • 5
  • 43
  • 66
  • I tried like that but also without any result ((Activity)getContext()).runOnUiThread(new Runnable() { ..... } }); – Samah Ahmed Mar 30 '17 at 10:58
0

You can make the ImageView static in your Fragment

public class YourFragment extends Fragment{
public static ImageView okaddress;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.control_address_fragment, container, false);

okaddress = (ImageView)view.findViewById(R.id.address_ok);
}

return view;
}

And when the result is 0 as you said you can change it like this in AddressControl

YourFragment.okaddress.setImageResource(R.drawable.clear);
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
0

I solved my problem by change source of ImageView from Fragment not from non-activity class, but until now need to know why we can not change ImageView by the previous way ( from normal class)?

Samah Ahmed
  • 419
  • 8
  • 24