throwing array index out of bound..

public class CustomProviderDemo extends ListActivity {
private EditText mContactNameEditText;
private TextView mContactsText;
private Button mContactSaveButton;
static String id = null;
static String userName = null;
static String[] usr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_provider);

    mContactNameEditText = (EditText) findViewById(R.id.contactNameEditText);
    mContactSaveButton = (Button) findViewById(R.id.contactSaveButton);
    mContactsText = (TextView) findViewById(R.id.contactEntryText);

    mContactSaveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String name = mContactNameEditText.getText().toString();
            insertRecord(name);

            mContactsText.append("\n"+name);
        }
    });


          displayRecords();
         setListAdapter(new MyAdapter(this));
}

private void insertRecord(String userName) {
    ContentValues values = new ContentValues();
    values.put(MyUsers.User.USER_NAME, userName);
    getContentResolver().insert(MyUsers.User.CONTENT_URI, values);
}
  private void displayRecords() {
    // An array specifying which columns to return.
    String columns[] = new String[] { MyUsers.User._ID,
            MyUsers.User.USER_NAME };
    Uri myUri = MyUsers.User.CONTENT_URI;
    Cursor cur = managedQuery(myUri, columns, // Which columns to return
            null, // WHERE clause; which rows to return(all rows)
            null, // WHERE clause selection arguments (none)
            null // Order-by clause (ascending by name)
    );
    if (cur.moveToFirst()) {

        do {
            id = cur.getString(cur.getColumnIndex(MyUsers.User._ID));
            userName = cur.getString(cur
                    .getColumnIndex(MyUsers.User.USER_NAME));
            Log.i("fgvd",id);
            Log.i("fgvd",userName);
            usr = new String[id.length()];
             for(int i=0;i<id.length();i++){


                 usr[i] =userName; 
         }
            // setListAdapter(new MyAdapter(this));
            //String[] hegd =userName; 
            Toast.makeText(this, id + " " + userName,  Toast.LENGTH_LONG).show();
        }
        while (cur.moveToNext());

    }

}


class MyAdapter extends BaseAdapter implements OnClickListener
 {

                private LayoutInflater inflater;

                public MyAdapter(Context ctx) {
                    super();
                    this.inflater = (LayoutInflater)   ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }        

                @Override
                public int getCount() {
                    return CustomProviderDemo.userName.length();
                }

                /* Not implemented but not really needed */
                @Override
                public Object getItem(int position) {
                    return null;
                }

                /* Not implemented but not really needed */
                @Override
                public long getItemId(int position) { 
                    return 0;
                }

                @Override
                public View getView(int position, View ConvertView, ViewGroup parent) 
                {
                    View v = inflater.inflate(R.layout.listitem_layout, parent, false);

//array index out of bound exception is here

                    **String gallerynames = CustomProviderDemo.usr[position];**
                    Log.i("base adapter check..",gallerynames);
                  //String addresses = keywordsearch.array_address[position]; 
                    TextView tv = (TextView) v.findViewById(R.id.barrio);
                    tv.setText(gallerynames);
                    tv = (TextView) v.findViewById(R.id.ciudad);
                  //  tv.setText(addresses);
                // email= keywordsearch.array_email[position];

                    return v;
                }

               @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }

 }



            }
link|flag

44% accept rate
The question lacks in many ways - what is CustomProviderDemo? the activity class name? is usr declared as static? though I saw you last question, it is not clear to me. please make it clearer. – MByD 3 hours ago
static String id = null; static String userName = null; static String[] usr; – saurabh trivedi 2 hours ago
please edit the question, and don't add this as comments. – MByD 2 hours ago
@MBYD edited qus – saurabh trivedi 2 hours ago
@MByD where i am wrong?pls – saurabh trivedi 2 hours ago
show 3 more comments

Know someone who can answer? Share a link to this question via email, twitter, or facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.