Adapter 问题 我真的不是很明白!!!

shenxiaolinil 2009-11-06 02:59:39
11-06 05:58:03.239: ERROR/AndroidRuntime(778): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
package com.shen.sqlite;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class sqlite extends Activity {
/** Called when the activity is first created. */
public static final String bdName = "code.db";
public static final int Version = 1;
private Myhandle handle;
private SQLiteDatabase dataset;
private ArrayAdapter<String> adapt;
private Cursor cursor;
private ArrayList<String> infoList=new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handle = new Myhandle(this, bdName, null, Version);
dataset = handle.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Myhandle.COUNTRY, "china");
values.put(Myhandle.CODE, 86);
dataset.insert(Myhandle.TableName, Myhandle.ID, values);
dataset.insert(Myhandle.TableName, Myhandle.ID, null);
values.clear();
values.put(Myhandle.COUNTRY, "usa");
values.put(Myhandle.CODE, 88);
dataset.update(Myhandle.TableName, values, Myhandle.ID + "=2", null);
dataset.execSQL("INSERT INTO " + Myhandle.TableName + "("
+ Myhandle.COUNTRY + "," + Myhandle.CODE + ") VALUES"
+ "('JAP','89')");

values.put(Myhandle.COUNTRY, "china");
cursor = dataset.rawQuery("Select * from " + Myhandle.TableName, null);
// TextView text = new TextView(this);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String a = cursor.getString(1);
String b = cursor.getString(2);
infoList.add(a + b);
//
}
ListView sp = (ListView) findViewById(R.id.spinner);
adapt = new ArrayAdapter<String>(this, R.layout.main, infoList);
sp.setAdapter(adapt);
}

public void onDestroy() {
dataset.delete(Myhandle.TableName, null, null);
super.onDestroy();
}
}
...全文
589 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwanglyxwangly 2011-04-29
  • 打赏
  • 举报
回复
你的adapter的资源文件的顶级结点必须是TextView
kinghua_q 2009-11-16
  • 打赏
  • 举报
回复
R.layout.main

只能是一个TextView的layout
Jack_tianzhenhai 2009-11-13
  • 打赏
  • 举报
回复
R.layout.main要使用系统提供的显示layout,或者你就自己定义一个显示list的layout.
download2039 2009-11-13
  • 打赏
  • 举报
回复
你的main.xml要更改为:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="wrap_content" />

因为根节点必须是TextView,不然就会抛“ArrayAdapter requires the resource ID to be a TextView”
AngleProgrammer 2009-11-11
  • 打赏
  • 举报
回复
第2个参数应该是ListView中每个选择项的样式,可以使用系统自带的android.R.layout.xxx,也可以是自定义的,仅包含TextView。
AngleProgrammer 2009-11-11
  • 打赏
  • 举报
回复
adapt = new ArrayAdapter <String>(this, R.layout.main, infoList);
不正确吧
shenxiaolinil 2009-11-09
  • 打赏
  • 举报
回复
这是我main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView android:id="@+id/spinner" android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
zhangguifeng123 2009-11-06
  • 打赏
  • 举报
回复
抛出的异常已经写的很明白呢:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

adapt = new ArrayAdapter <String>(this, R.layout.main, infoList);
这句中布局main中的布局可能不是TextView对象。

adapt = new ArrayAdapter <String>(A1, A2, A3);
参数A2是一个“resource ID to be a TextView

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧