我想在ListView
using中显示项目BaseAdapter
。我使用游标和内容提供程序类获得了数组中的项目。我将数据保存在一个数组中,并希望使用BaseAdapter
它在 a 中显示它,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
}
}