我想从我的sqlite
中将数据放入客户适配器中这是我的代码自定义适配器 我想使用带有sqlite的BaseAdapter来显示信息
public class Myadapter extends BaseAdapter{
Context context;
ArrayList<String>mylist;
private static LayoutInflater inflater=null;
public Myadapter(Context context, int element,ArrayList<String>mylist) {
// TODO Auto-generated constructor stub
this.mylist=mylist;
this.context=context;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return result.length;
return mylist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mylist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView t1;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
SQLiteDatabase sq;
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.element, parent,false);
final TextView t1=(TextView) rowView.findViewById(R.id.t1);
//Viwe.info.get(position);
return rowView;
}
知道我想在cutome布局中显示结果这是viwe活动
public class Viwe extends AppCompatActivity {
public static SQLiteDatabase db;
public ListView l1;
public static ArrayList<String> information;
// public String[] salfi={"moh","mas"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viwe);
l1 = (ListView) findViewById(R.id.l1);
db = openOrCreateDatabase("info", MODE_PRIVATE, null);
// ArrayAdapter<String>data=new ArrayAdapter<String>(Viwe.this,android.R.layout.simple_list_item_1,salfi);
Myadapter myadapter = new Myadapter(Viwe.this, R.layout.element, information);
//Myadapter myadapter=new Myadapter(Viwe.this,R.layout.element,info);
try {
Viwe.getall();
} catch (Exception e) {
e.printStackTrace();
Log.d("tage", e.getMessage());
}
l1.setAdapter(myadapter);
myadapter.notifyDataSetChanged();
}
public static void getall()
{
Cursor c = db.rawQuery("select * from information", null);
while (c.moveToNext())
{
information.add(c.getString(0));
}
c.close();
}
我的logcat
05-18 00:18:27.516 27670-27670/com.example.dv_loper.db E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dv_loper.db, PID: 27670
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dv_loper.db/com.example.dv_loper.db.Viwe}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.example.dv_loper.db.Myadapter.getCount(Myadapter.java:36)
at android.widget.ListView.setAdapter(ListView.java:487)
at com.example.dv_loper.db.Viwe.onCreate(Viwe.java:45)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
答案 0 :(得分:1)
你有一个简单的问题,当你第一次初始化适配器时,你传递给它一个空的ArrayList ....因为try-catch块在适配器初始化之后出现并且信息ArrayList为null,所以简单地把数组填充后的初始化如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viwe);
l1 = (ListView) findViewById(R.id.l1);
db = openOrCreateDatabase("info", MODE_PRIVATE, null);
try {
Viwe.getall();
// information arraylist got filled here
} catch (Exception e) {
e.printStackTrace();
Log.d("tage", e.getMessage());
}
Myadapter myadapter = new Myadapter(Viwe.this, R.layout.element,information);
//the information is not null here as before in your code
l1.setAdapter(myadapter);
myadapter.notifyDataSetChanged();
}
这就是它:)
修改强>
当我看到你的代码时,我以为你在getall()方法中初始化了ArrayList,一切都很好,但那不对,你根本就没有初始化它,这就是getall方法应该是这样的: / p>
public static void getall()
{
// this is the missing initialization
information = new ArrayList<String>();
Cursor c = db.rawQuery("select * from information", null);
while (c.moveToNext())
{
information.add(c.getString(0));
}
c.close();
}