0

I'm using koush/ion to upload a picture to a server. The picture uploads fine but the progress bar is not disappearing after completion. I've been checking the code over and over and everything seems to be fine. I tried the .progressBarparameter of Ion as well as doing it manually with progressBar.setVisibility parameter. Here's my code:

public class ChatRowsAdapter extends ArrayAdapter<ChatMessage> {

private TextView messageBody;
private ImageView messagePic;
private ProgressBar progressBar;
private List<ChatMessage> countries = new ArrayList<ChatMessage>();
private LinearLayout wrapper;
private static final String TAG = "ChatRows";
private boolean loading = false;

@Override
public void add(ChatMessage object) {
    countries.add(object);
    super.add(object);
}

public ChatRowsAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}

public int getCount() {
    return this.countries.size();
}

public ChatMessage getItem(int index) {
    return this.countries.get(index);
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    //if (row == null) {
    LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context
            .LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.row_chat, parent, false);
        //}
    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

    ChatMessage coment = getItem(position);

    messageBody = (TextView) row.findViewById(R.id.comment);
    messagePic = (ImageView) row.findViewById(R.id.imgPhotoSendVC);
    progressBar = (ProgressBar) row.findViewById(R.id.progressBarEnvioFoto);

    Log.d(TAG, "Picture path: " + coment.info.getPath());
    Log.d(TAG, "Text: " + coment.comment);
    if (coment.info != null && coment.info.getPath() != "") {
        messagePic.setVisibility(View.VISIBLE);
        messageBody.setVisibility(View.GONE);

        Picasso.with(parent.getContext()).load(new File(coment.info.getPath())).resize(120, 120).
                into(messagePic);
        if (!loading){
            uploadImage(parent.getContext(), coment.info);
        }             
        //else {
          //  progressBar.setVisibility(View.VISIBLE);
        //}
    } else {
        messagePic.setVisibility(View.GONE);
        messageBody.setVisibility(View.VISIBLE);
        Log.d(TAG, "Setting text..");
        messageBody.setText(coment.comment);
    }

    WindowManager temp = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = temp.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = (int) (size.x * 0.75);
    messageBody.setMaxWidth(width);


    if (coment.left) {
        messageBody.setTextColor(Color.rgb(104, 159, 56));
        messageBody.setBackgroundResource(R.drawable.white_bubble);
    } else {
        messageBody.setTextColor(Color.rgb(255, 255, 255));
        messageBody.setBackgroundResource(R.drawable.green_bubble);
    }
    wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

    return row;
}

public void uploadImage(Context context, EnvioFotoInfo info) {
        Log.d(TAG, "Loading picture..");
        loading = true;

        Ion.with(context)
                .load("POST", "https://portaldev.mediconecta.com/api/v1/WebMethods/fotochat")
                .addHeader("authorization", "Basic c2Z1c2VyZGV2Ojg2NDJFRjkyM0U1OTYxQjg3MzFDRjI0QTBCOTkzMQ==")
                .progressBar(progressBar)
                .setMultipartParameter("idQuote", info.getCitaId())
                .setMultipartFile("file", new File(info.getPath()))
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        if (result != null) {
                            Log.d(TAG, result.toString());
                        } else {
                            Log.e(TAG, "No result");
                        }
                        loading = false;
                        //progressBar.setVisibility(View.GONE);
                    }
                });
    }
}

layout/row_chat.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="wrap_content" >

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgPhotoSendVC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:paddingLeft="10dip"
            android:visibility="gone"/>
        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:paddingLeft="10dip"
            android:text="Mensaje Chat"
            android:textColor="@android:color/primary_text_light"
            android:textSize="15dp"
            android:textStyle="normal"
            />

        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBarEnvioFoto"
            android:visibility="gone"/>
    </LinearLayout>

</LinearLayout>
Mauricio
  • 839
  • 2
  • 13
  • 26
  • why "progressBar.setVisibility(View.GONE)" is commented on your code? – Stack Diego May 27 '16 at 15:12
  • Because I understand `Ion` is supposed to do that automatically. In any case, I tried to do it manually but it didn't work either, that's why it is commented. @StackDiego – Mauricio May 27 '16 at 15:51

0 Answers0