3

I am uploading data to server, and if data successfully uploaded to server then i am showing "Saved", like you can see "Uploaded" for image.

But the issue is, i have stored data for the first item row, whereas getting "Saved" text in different row item

holder.textDataStatus.setText(ImageList.get(position).getData());

protected void onPostExecute(String file_url) {
  // dismiss the dialog once product deleted
  pDialog.dismiss();

  try {

       // Prepare Save Data
       if(strStatusId.equals("0"))
       {
              Toast.makeText(UploadImagesActivity.this, "Unable to upload", Toast.LENGTH_SHORT).show();
                }
                else if (strStatusId.equals("1"))
                {
                    Toast.makeText(UploadImagesActivity.this, "Data Uploaded Successfully", Toast.LENGTH_SHORT).show();
                    ImageList.get(position).setData("Saved");               
                    Log.d("strData:", fileName); // getting correct tapped item
                    mAdapter.notifyDataSetChanged();
                }
                else
                {
                    Toast.makeText(UploadImagesActivity.this, "Unable to upload", Toast.LENGTH_SHORT).show();
                }

                } catch (Exception e) {
                    // TODO: handle exception
                }


           if (file_url != null){
                Toast.makeText(UploadImagesActivity.this, file_url, Toast.LENGTH_LONG).show();
           }
Sun
  • 6,768
  • 25
  • 76
  • 131

3 Answers3

3

The problem is the your adapter doesn't know that data for which row was uploaded to server. You need to tell that to your adapter. As for the question "How to tell that to adapter?", you already have a list ImageList. We just need to edit it.

Now, add another bool to your MyData class like: boolean uploaded = false; and create getter setter for it.

Add following lines to your getView():

if(ImageList.get(position).isUploaded()){
    holder.btnUpload.setText("Save");
}else{
    holder.btnUpload.setText("Upload");
}

Now, we need to set this value to true, after the upload is done. We should only do that from UploadData class. For that, we need to send position to UploadData class. We can do that by constructor like following:

class UploadData extends AsyncTask<String, String, String> {
    private ProgressDialog pDialog;

    int position;

    //constructor to pass position of row, on which button was clicked to class
    public UploadData(int position){
        this.position=position;
    }

     /**
    * Before starting background thread Show Progress Dialog
    * */

    .
    .
    .
    .

   protected void onPostExecute(String file_url) {
       // dismiss the dialog once product deleted
       pDialog.dismiss();

       //after upload is done, set value to true
       ImageList.get(position).setUploaded(true);
       //now we need to notify our adapter so that it can reflect changes
       mAdapter.notifyDataSetChanged();
    .
    .

Now, according to your current code, I think passing value of position to UploadData is really going to be tough for you through constructor. So, you can try by setting a value in global variable in class.

EDIT 1:

Pass position to global variable in your holder.btnData.SetOnClickListener like following:

holder.btnData.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                //pass position into activity's global variable
                pos = position/*position from adapter*/;
                strPath = ImageList.get(position).getImages().toString();
                fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());                                      

                showDialog(DIALOG_DATA);

            }
        });

Please leave a comment if you need any explanation.

DroidDev
  • 1,527
  • 4
  • 20
  • 42
  • still getting same issue – Sun Apr 07 '15 at 12:26
  • @Sun how did you pass position to your `UploadData` class? – DroidDev Apr 07 '15 at 13:50
  • i have updated my code as per your suggestion check above, and now i am using something like this: new UploadData(position).execute(); i have declared int position globally in my class... – Sun Apr 08 '15 at 04:51
  • as per my code, I used: position = position; but getting Multiple markers at this line - The final local variable position cannot be assigned, since it is defined in an enclosing type - The assignment to variable position has no effect – Sun Apr 08 '15 at 12:32
  • @Sun you need to do something like `YourActivityName.position=position;` or you need to change name of your global variable. I think problem is because your variable names are same. – DroidDev Apr 08 '15 at 17:43
  • i have just changed the name of global variable and i am done ! – Sun Apr 09 '15 at 04:21
1

Try this modified getView(...), check the //added codes. The point is how to get item position when click button:

@Override
public View getView(int position, View convertView,
            ViewGroup parent) {
        // Avoid unneccessary calls to findViewById() on each row, which is
        // expensive!

        holder = null;

        if (convertView == null) {

            ... ...             


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

         //added codes
         holder.btnUpload.setTag(new Integer(position));    
         holder.btnData.setTag(new Integer(position));


        ... ...            

        // btnUpload
        holder.btnUpload.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Upload     

                //if you need position here, get it by:
                //added codes
                //Button clickBtn = (Button)v;
                //int position = ((Integer)clickBtn.getTag()).intValue();          
                  ..........
            }
        });


        holder.btnData.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //added codes
                Button clickBtn = (Button)v;
                int position = ((Integer)clickBtn.getTag()).intValue();

                strPath = ImageList.get(position).getImages().toString();
                fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());                                      

                showDialog(DIALOG_DATA);

            }
        });

        if(ImageList.get(position).isUploaded()){
            holder.textDataStatus.setText("Saved");
        }else{
            holder.textDataStatus.setText("");
        }


        return convertView;

    }

Hope this help!

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
  • 1
    Using position as a tag is much a better way than using a global position variable, since it will always refer to the correct position. Something like this I would have typed up but Xchinegn beat me to it :) – Ashton Engberg Apr 09 '15 at 01:04
  • @AshtonEngberg I agree with you. Using global variable is the last thing I wanted to do, but, in OP's case, they needed to pass position of row through multiple methods. So, either there was a lot of change in that mechanism(showing dialog box etc (which part is now deleted from question)) or global variables. – DroidDev Apr 21 '15 at 12:42
0

try to include

    public View getView(final int position, View convertView,
                    ViewGroup parent) {
                // Avoid unneccessary calls to findViewById() on each row, which is
                // expensive!

                holder = null;
     int crntposition = position;

                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(
                            R.layout.adapter_upload_images, null);
                    holder = new ViewHolder(convertView);

                    // Create a ViewHolder and store references to the children
                    // views
                    holder.textName = (TextView) convertView
                            .findViewById(R.id.textName);
                    holder.thumbnail = (ImageView) convertView
                            .findViewById(R.id.thumbnail);                
                    holder.textStatus = (TextView) convertView
                            .findViewById(R.id.textStatus);
                    holder.textDataStatus = (TextView) convertView
                            .findViewById(R.id.textDataStatus);
                    holder.btnUpload = (Button) convertView
                            .findViewById(R.id.btnUpload);
                    holder.btnData = (Button) convertView
                            .findViewById(R.id.btnData);

                    // The tag can be any Object, this just happens to be the
                    // ViewHolder
                   convertView.setTag(holder);
            convertView.setTag(R.id.textDataStatus, holder.textDataStatus);

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

                holder.btnUpload.setEnabled(ImageList.get(position)
                        .isStatusEnable());            
                holder.textStatus.setText(ImageList.get(position).getMessage());
                holder.textDataStatus.setText(ImageList.get(Integer.parseInt(holder.textDataStatus.getTag().toString())).getData());
                strPath = ImageList.get(crntposition)).getImages().toString();
 Log.e("IMAGE CLICK",strPath);


                // Get File Name
                fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());            

                holder.btnData.setOnClickListener(new OnClickListener() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        strPath = ImageList.get(position).getImages().toString();
                        fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());                                      

                        showDialog(DIALOG_DATA);

                    }
                });


                return convertView;

            }
        }

..after initializing the holder.textDataStatus inside getView method of your adapter class

Karthika PB
  • 1,373
  • 9
  • 16
  • @Sun please post the log what was the error please post the error line here – Karthika PB Apr 01 '15 at 09:16
  • getting NPE, where you have made changes – Sun Apr 01 '15 at 09:20
  • @sun please be sure that your array is not null it have sufficient values check Log.e("IMAGE CLICK",strPath); this in your log while running the code pls post the complete error log you get here and the line at which you get NPE – Karthika PB Apr 01 '15 at 09:34