Reputation: 11
I want to put data in customer adapter from my sqlite
this is my code custom adapter I want to use BaseAdapter with sqlite to display information
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;
}
know i want to display result in cutome layout this is viwe activity
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();
}
my 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)
Upvotes: 0
Views: 386
Reputation: 458
You have a simple problem, when you first initialize the Adapter you pass to it an empty ArrayList .... because the try-catch block is coming after the initialization of the adapter and the information ArrayList is null, so simply put the initialization after your Array get filled like this :
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();
}
and that's pretty much it :)
Edit:
when I saw your code I thought you were initializing the ArrayList in the getall() method and everything was fine, but that was not right, you are not initializing it at all , this is what getall method should look like :
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();
}
Upvotes: 1