2011-06-09 47 views
0

我想使用BaseAdapterListView中显示项目。我使用游标和内容提供者类获取数组中的项目。我将数据保存在一个数组中,并希望使用BaseAdapter将其显示在ListView中,但它将丢弃ArrayIndexOutOfBoundException。我对这个问题感到十分困惑。通过基础适配器创建列表,但是抛出数组索引超出界限例外

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; 
static String gallerynames; 
@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); 


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


     } 
    }); 


     displayRecords(); 
     setListAdapter(new MyAdapter(this)); 
} 
    public 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); 

       usr = new String[id.length()]; 
       for(int i=0;i<id.length();i++){ 


        usr[i] =userName; 
        Log.i("fgvd",usr[i]); 
      } 

       //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); 

        //throwing array index out of bund exceotion here 
        gallerynames = CustomProviderDemo.usr[position]; 
        TextView tv = (TextView) v.findViewById(R.id.barrio); 
        tv.setText("gallerynames"); 



        return v; 
       } 

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

       } 

} 

回答

0

您只在单击按钮时插入用户。所以它第一次运行时没有用户显示。

您应该检查position是否不大于数组长度。

提示:一个适配器只是保存数据,它不应该实现OnClickListener。