通过基本适配器创建列表,但是抛出数组索引越界异常

发布于 2024-11-14 12:10:32 字数 4072 浏览 0 评论 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

                }

 }

I want to show items in a ListView using BaseAdapter. I got the items in an array using a cursor and a content provider class. I saved the data in an array and want to use the BaseAdapter for displaying it in a ListView but it is throwing an ArrayIndexOutOfBoundException. I am totally confused about this problem.

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

                }

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-11-21 12:10:32

仅当单击按钮时才插入用户。因此第一次运行时将没有用户显示。

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

提示:适配器仅保存数据,它不应该实现 OnClickListener。

You only insert a user when you clicked a button. So the first time it will run without an user to display.

You should check if position isn't bigger than the array length.

Tip: An Adapter just holds data, it shouldn't implement OnClickListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文