我使用本教程作为参考: http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
我使用数据库中的值
代替预定义的值Mainactivity.java
package com.example.listview1;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
ListView list;
datamanager dm;
String[] web;
int[] imageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dm=new datamanager(this);
list=(ListView)findViewById(R.id.list);
web=dm.getNames("Books");
imageId=getID();
CustomList adapter = new
CustomList(MainActivity.this, web, imageId);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
public int[] getID()
{
int[] id=null;
String[] str=dm.getID("Books");
for(int i=0;i<str.length;i++)
{
id[i]=getResources().getIdentifier(str[i] , "drawable", getPackageName());
}
return id;
}
}
datamanager.java
package com.example.listview1;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class datamanager {
SQLiteDatabase DB1;
private final String Db_NAME = "dbshopkart.db";
private final int DB_VERSION =3;
Context c1;
public datamanager(Context c) {
c1=c;
openingclass o=new openingclass(c);
DB1=o.getWritableDatabase();
}
public class openingclass extends SQLiteOpenHelper
{
public openingclass(Context c) {
super(c,Db_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
public String[] getNames(String category)
{
ArrayList<String> dataList = new ArrayList<String>();
Cursor cu=DB1.rawQuery("SELECT NAME from products where CATEGORY ='"+category+"'",null);
cu.moveToFirst();
if (!cu.isAfterLast())
{
do
{
dataList.add(cu.getString(0));
// dataList.add(cu.getString(1));
// dataList.add(cu.getString(2));
// dataList.add(cu.getString(3));
// dataList.add(cu.getString(4));
// dataList.add(cu.getString(5));
// dataList.add(cu.getString(6));
// dataList.add(cu.getString(7));
}
while (cu.moveToNext());
cu.close();
}
// return the ArrayList that holds the data collected from
// the database.
return dataList.toArray(new String[dataList.size()]);
}
public String[] getID(String category)
{
ArrayList<String> dataList = new ArrayList<String>();
Cursor cu=DB1.rawQuery("SELECT PID from products where CATEGORY ='"+category+"'",null);
cu.moveToFirst();
if (!cu.isAfterLast())
{
do
{
dataList.add(cu.getString(0));
// dataList.add(cu.getString(1));
// dataList.add(cu.getString(2));
// dataList.add(cu.getString(3));
// dataList.add(cu.getString(4));
// dataList.add(cu.getString(5));
// dataList.add(cu.getString(6));
// dataList.add(cu.getString(7));
}
while (cu.moveToNext());
cu.close();
}
// return the ArrayList that holds the data collected from
// the database.
String[] str=dataList.toArray(new String[dataList.size()]);
return str;
}
}
自定义适配器
package com.example.listview1;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final int[] imageId;
public CustomList(Activity context,
String[] web, int[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
我得到以下logcat
04-19 02:29:01.182: E/AndroidRuntime(9405): FATAL EXCEPTION: main
04-19 02:29:01.182: E/AndroidRuntime(9405): Process: com.example.listview1, PID: 9405
04-19 02:29:01.182: E/AndroidRuntime(9405): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview1/com.example.listview1.MainActivity}: java.lang.NullPointerException
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.os.Handler.dispatchMessage(Handler.java:102)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.os.Looper.loop(Looper.java:137)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-19 02:29:01.182: E/AndroidRuntime(9405): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 02:29:01.182: E/AndroidRuntime(9405): at java.lang.reflect.Method.invoke(Method.java:515)
04-19 02:29:01.182: E/AndroidRuntime(9405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-19 02:29:01.182: E/AndroidRuntime(9405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-19 02:29:01.182: E/AndroidRuntime(9405): at dalvik.system.NativeStart.main(Native Method)
04-19 02:29:01.182: E/AndroidRuntime(9405): Caused by: java.lang.NullPointerException
04-19 02:29:01.182: E/AndroidRuntime(9405): at com.example.listview1.MainActivity.getID(MainActivity.java:41)
04-19 02:29:01.182: E/AndroidRuntime(9405): at com.example.listview1.MainActivity.onCreate(MainActivity.java:22)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.Activity.performCreate(Activity.java:5243)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 02:29:01.182: E/AndroidRuntime(9405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
04-19 02:29:01.182: E/AndroidRuntime(9405): ... 11 more
帮助!
答案 0 :(得分:2)
你得到NPE:
public int[] getID()
{
int[] id=null; <--- you need initialize this
String[] str=dm.getID("Books");
for(int i=0;i<str.length;i++)
{
id[i]=getResources().getIdentifier(str[i] , "drawable", getPackageName()); <--- this line cause NPE
}
return id;
}
您未在代码中初始化id
如果您知道所需阵列的大小:
id = new int[size];
如果您不知道使用ArrayList
代替int[]
您的代码必须是:
public int[] getID()
{
String[] str=dm.getID("Books");
int[] id= new int[str.length]
for(int i=0;i<str.length;i++)
{
id[i]=getResources().getIdentifier(str[i] , "drawable", getPackageName());
}
return id;
}