我试图在自定义 ListView 中添加复选框和一些 TextView ( TextView 从数据库获取值),但它给出了 NullPointerException 错误。我不知道为什么以及我的代码有什么问题。下面是我的代码。
我的 MainActivity 类:
public class Classes extends Activity {
ImageView imageViewNewClass;
ListView mListView;
String[] stg1;
List<String[]> names2 = null;
DataManipulatorClass dataManipulator;
CustomAdapter customAdapter;
public Classes classes = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.classes);
imageViewNewClass = (ImageView) findViewById(R.id.newclass);
mListView = (ListView) findViewById(R.id.displaydata);
imageViewNewClass.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Classes.this, Class_Create.class);
startActivity(intent);
}
});
Resources res =getResources();
classes = this;
dataManipulator = new DataManipulatorClass(this);
names2 = dataManipulator.selectAll();
stg1 = new String[names2.size()];
int x = 0;
String stg = null;
for (String[] name : names2) {
stg = "Class Name : " + name[1];
stg1[x] = stg;
x++;
}
customAdapter= new CustomAdapter( classes, stg1,res );
mListView.setAdapter( customAdapter );
customAdapter.notifyDataSetChanged();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Listview item clicked", Toast.LENGTH_LONG).show();
}
});
}
}
自定义适配器类.java
public class CustomAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public Resources res;
int i = 0;
String[] stg1;
List<String[]> names2 = null;
DataManipulatorClass dataManipulator;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, String[] stg1, Resources resLocal) {
/********** Take passed values **********/
activity = a;
data = stg1;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if (data.length <= 0)
return 1;
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public CheckBox checkBox;
public TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.check, null);
holder = new ViewHolder();
holder.checkBox = (CheckBox) vi.findViewById(R.id.checkBox1);
holder.textView = (TextView) vi.findViewById(R.id.selection);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
if (data.length <= 0) {
holder.textView.setText("No Data");
} else {
dataManipulator = new DataManipulatorClass(this);
names2 = dataManipulator.selectAll();
stg1 = new String[names2.size()];
int x = 0;
String stg = null;
for (String[] name : names2) {
stg = "Class Name : " + name[1];
stg1[x] = stg;
x++;
}
holder.textView.setText(stg1[x]);
}
return vi;
}
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
}
DataManipulator.Java
public class DataManipulatorClass {
private static final String DATABASE_NAME = "mydatabaseclass.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_NAME = "newtableclass";
private static Context context;
static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME
+ "(classname) values (?)";
public DataManipulatorClass(Context context) {
DataManipulatorClass.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context);
DataManipulatorClass.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT);
}
public DataManipulatorClass(CustomAdapter customAdapter) {
OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context);
DataManipulatorClass.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT);
}
public long insert(String classname) {
this.insertStmt.bindString(1, classname);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}
public List<String[]> selectAll() {
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME,
new String[] { "id", "classname" }, null, null, null, null,
"classname asc");
int x = 0;
if (cursor.moveToFirst()) {
do {
String[] b1 = new String[] { cursor.getString(0),
cursor.getString(1) };
list.add(b1);
x = x + 1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public void delete(int rowId) {
db.delete(TABLE_NAME, null, null);
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ " (id INTEGER PRIMARY KEY, classname TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
在类类中,我有一个 ListView ,我想在其中显示数据,我的 logcat 在这里 http://i.share.pho.to/9739fdca_o.png
mListView.setAdapter( customAdapter ) 出现错误;将 ListView 中的适配器设置为自定义适配器时的这一行...
提前致谢...
最佳答案
public class Classes extends Activity {
ImageView imageViewNewClass;
ListView mListView;
String[] stg1; // Never Initialized in code before passing to Adapter
//stg1,这个变量你从未在你的 Activity 中初始化过,你将它传递给你的自定义适配器,这就是为什么listview在访问数据计数时抛出NPE vai适配器。
public int getCount() { // Will always be Null, as data is null
if (data.length <= 0)
return 1;
return data.length;
}
只需在 Activity 中传递一个非空变量实例,您的问题就会得到解决。
关于java - Android 中的自定义 ListView 在更新 Android 数据库中的数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20322933/