Nikunj Patel
Nikunj Patel

Reputation: 22066

Stuck while call webservices

i need to display some records in listview but when i am calling services it get me error in adapter.so can you tell me how can i correct it?i got Arrayindexoutofbound in USERNAME. i have also check main activity but there are no problem at all.this code are previosly working good.but now it raise issue....

package com.RecordingAppDotnet;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.Sharedpreferance.GMailSender;

public class LogListAdapter extends BaseAdapter
{

    public String ScoreID[];
    public String Activityname[];
    public String UserName[];
    public String PlayedDate[];
    public String UserComment[];
    public String Score[];
    public int int_spnr_choose_item_first_or_sec;
    public Activity context;
    public LayoutInflater inflater;

    public LogListAdapter(Activity context,String[] ScoreID,String Activityname[],String UserName[],String PlayedDate[],String UserComment[],String Score[],int int_spnr_choose_item_first_or_sec) {
        super();

        this.context = context;
        this.ScoreID = ScoreID;
        this.Activityname = Activityname;
        this.UserName = UserName;
        this.PlayedDate = PlayedDate;
        this.UserComment= UserComment;
        this.Score = Score;
        this.int_spnr_choose_item_first_or_sec = int_spnr_choose_item_first_or_sec;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Activityname.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        TextView log_Activity,log_username,log_playdate,log_score;
        Button edit_comment;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.log_activity_row, null);
            holder.log_Activity = (TextView) convertView.findViewById(R.id.log_Activity_name);
            holder.log_username = (TextView) convertView.findViewById(R.id.log_Username);
            holder.log_playdate = (TextView) convertView.findViewById(R.id.log_PlayDate);
            holder.log_score = (TextView) convertView.findViewById(R.id.log_Score);
            holder.edit_comment = (Button)convertView.findViewById(R.id.log_comment_btn);




            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();
        holder.log_Activity.setText(Activityname[position]);
        holder.log_username.setText(UserName[position]);
        holder.log_playdate.setText(PlayedDate[position]);
        holder.log_score.setText(Score[position]);
        //final Intent editcommentIntent = new Intent(context,LogList.class);
        holder.edit_comment.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.comment);
                dialog.setTitle("Put your Comment");
                dialog.setCancelable(true);

                final EditText cmment_comment_edtx;
                Button cmt_btn_ok,cmt_cacel;
                cmment_comment_edtx =(EditText)dialog.findViewById(R.id.cmment_comment_edtx);
                //UserComment[position].replaceAll("\\%0A", "");
                UserComment[position] = URLDecoder.decode(UserComment[position].toString());
                cmment_comment_edtx.setText(UserComment[position].toString());
                cmt_btn_ok = (Button)dialog.findViewById(R.id.cmt_btn_ok);
                cmt_cacel = (Button)dialog.findViewById(R.id.cmt_cacel);
                cmment_comment_edtx.setOnEditorActionListener(new OnEditorActionListener(){ 

                    @Override
                    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                         return false;
                    }

                 });

                cmt_btn_ok.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                            try
                            {
                            String Comment = cmment_comment_edtx.getText().toString().trim();

                            String query = URLEncoder.encode(Comment, "utf-8");
                                try
                                {                                         //Webservices/Service.asmx/UpdateLogComment?ScoreId=string&MachineName=string&UserComment=string
                                    String sourceUrl1 = "URL/Service.asmx/UpdateLogComment?ScoreId="+ScoreID[position]+"&MachineName="+Machinelist.str_Machinename+"&UserComment="+query+"";
                                    URL sourceUrl = new URL(sourceUrl1);
                                    SAXParserFactory spf = SAXParserFactory.newInstance();
                                    SAXParser sp = spf.newSAXParser();
                                    XMLReader xr = sp.getXMLReader();
                                    commentHandler dataHandler = new commentHandler();
                                    xr.setContentHandler(dataHandler);
                                    xr.parse(new InputSource(sourceUrl.openStream()));
                                    commentDataset dataset = dataHandler.getParsedcomment_DataSet();

                                    String flag_sucess_send_commnt = dataset.getboolean1();
                                    if(flag_sucess_send_commnt.equalsIgnoreCase("true"))
                                    { 
                                        Toast.makeText(context, "Comment added Successfully.",Toast.LENGTH_LONG).show();
                                        dialog.cancel();
                                        LogList.lview.setVisibility(View.INVISIBLE);

                                    }
                                    else
                                    {
                                        Toast.makeText(context, "Comment not Update,Please Try Again", Toast.LENGTH_LONG).show();
                                    }
                                }catch (Exception e) {
                                    e.printStackTrace();
                                }

                    }catch (Exception e) {
                        Writer writer = new StringWriter();
                        PrintWriter printWriter = new PrintWriter(writer);
                        e.printStackTrace(printWriter);
                        String s = writer.toString();

                        } catch (Exception ex) {   

                        } 
                    }}
                });

                cmt_cacel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
                dialog.show();


            }
        });


        return convertView;
    } 

}

ERROR :

02-28 16:25:54.082: ERROR/AndroidRuntime(3374): FATAL EXCEPTION: main
02-28 16:25:54.082: ERROR/AndroidRuntime(3374): java.lang.ArrayIndexOutOfBoundsException
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.RecordingAppDotnet.LogListAdapter.getView(LogListAdapter.java:107)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.AbsListView.obtainView(AbsListView.java:1428)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.ListView.onMeasure(ListView.java:1128)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.View.measure(View.java:8553)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.os.Looper.loop(Looper.java:150)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at android.app.ActivityThread.main(ActivityThread.java:4385)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at java.lang.reflect.Method.invoke(Method.java:507)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
02-28 16:25:54.082: ERROR/AndroidRuntime(3374):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 105

Answers (2)

Amokrane Chentir
Amokrane Chentir

Reputation: 30385

It's telling you that you are trying to access an array out of its range.

Specifically, here: UserName[position]. As TeddyBearFr said, position here is bigger or equal than UserName.length.

You have to figure out, you are having this issue.

Also, you really need to clean your code a bit:

  • Pay attention to how you are naming your variables, this is not Java friendly. Take a look at some guidelines.
  • Create an object: Log that will encapsulates all these variables: scoreId, activityName, userName etc... And then, your adapter will be read only an array of Logs.
  • What you are doing when clicking on the button Ok should be done in a background thread, so try using AsyncTask for that.

Upvotes: 0

Sly
Sly

Reputation: 2101

Are you sure that Activityname and Username have the same sizes? It seems that Activityname.length > Username.length

EDIT: Add this in your constructor to check array sizes

    Log.d("TEST", "Activityname size = " + Activityname.length );
    Log.d("TEST", "UserName size     = " + UserName.length );
    Log.d("TEST", "PlayedDate size   = " + PlayedDate.length );
    Log.d("TEST", "UserComment size  = " + UserComment.length );
    Log.d("TEST", "Score size        = " + Score.length );

EDIT2: Don't be so sure!

According to Android documentation, a new array will be created by the collection.toArray(Object[]) method if the size of the collection is greater than the argument array. So, str_arr_ActivityName and str_arr_UserName may have a size different of ActivityName.size().

Upvotes: 1

Related Questions