Reputation: 21
I am trying to populate data from an sqlLite Database into a list view.
Below is the code where I am trying to fetch the data into a listview using cursoradapter.
public class TransactionActivity extends AppCompatActivity {
DBOperations dbOperations;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaction);
lv=(ListView)findViewById(R.id.listView);
dbOperations=new DBOperations(TransactionActivity.this);
Cursor tablecursor = dbOperations.getTableRecords(DBOperations.TABLE_TRANSACTIONS);
Myadapter sca = new Myadapter(TransactionActivity.this,tablecursor,true);
lv.setAdapter(sca);
}
public class Myadapter extends CursorAdapter{
public Myadapter(Context context,Cursor tablecursor, boolean autoRequery){
super(context,tablecursor,autoRequery);}
@Override
public View newView(Context context, Cursor tablecursor, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listitem, viewGroup, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
cursor.moveToFirst();
TextView Accnameview = (TextView)findViewById(R.id.textView);
TextView Incomevalueview=(TextView)findViewById(R.id.textView2);
String accname = cursor.getString(cursor.getColumnIndex(DBOperations.COL_ACC_NAME));
String incomev =cursor.getString(cursor.getColumnIndex(DBOperations.COL_DEBIT));
Accnameview.setText(accname);
Incomevalueview.setText(incomev);
}
}
}
The listview.xml that is inflated is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/darker_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2"
android:layout_gravity="right" />
I am getting the below error when trying to do so.
09-21 09:13:04.715 10142-10142/com.example.joshigir.finance D/OpenGLRenderer: Enabling debug mode 0 09-21 09:13:45.007 10142-10142/com.example.joshigir.finance W/EGL_genymotion: eglSurfaceAttrib not implemented 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance D/AndroidRuntime: Shutting down VM 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4c96b20) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: FATAL EXCEPTION: main 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: Process: com.example.joshigir.finance, PID: 10142 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: java.lang.NullPointerException 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at com.example.joshigir.finance.TransactionActivity$Myadapter.bindView(TransactionActivity.java:69) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.CursorAdapter.getView(CursorAdapter.java:254) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2255) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.ListView.measureHeightOfChildren(ListView.java:1263) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.ListView.onMeasure(ListView.java:1175) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime: at android.view.View.measure(View.java:16497)
Kindly help in finding out the error.
Upvotes: 0
Views: 72
Reputation: 30985
@Override
public void bindView(View view, Context context, Cursor cursor) {
// don't call this here; the cursor is already positioned on the
// the correct record when this method is called
// cursor.moveToFirst();
// you need to call findViewById on the view parameter
// TextView Accnameview = (TextView)findViewById(R.id.textView);
// TextView Incomevalueview=(TextView)findViewById(R.id.textView2);
TextView Accnameview = (TextView) view.findViewById(R.id.textView);
TextView Incomevalueview=(TextView) view.findViewById(R.id.textView2);
String accname = cursor.getString(cursor.getColumnIndex(DBOperations.COL_ACC_NAME));
String incomev =cursor.getString(cursor.getColumnIndex(DBOperations.COL_DEBIT));
Accnameview.setText(accname);
Incomevalueview.setText(incomev);
}
Upvotes: 0
Reputation: 1145
In your bindView(View, Context, Cursor)
method from the Myadapter
class:
TextView Accnameview = (TextView)findViewById(R.id.textView);
TextView Incomevalueview=(TextView)findViewById(R.id.textView2);
You need to call findViewById(int)
on the view
attribute of the method, like this :
TextView Accnameview = (TextView) view.findViewById(R.id.textView);
TextView Incomevalueview = (TextView) view.findViewById(R.id.textView2);
Otherwise you are using the findViewById(int)
method from the TransactionActivity
enclosing class, which looks for your TextViews inside the Activity view hierarchy. Since the view from the adapter isn't attached to the Activity view yet, it doesn't find them and returns null. Therefore, you are calling setText(String)
on null TextViews at the end of your bindView(View, Context, Cursor)
method.
See here for a CursorAdapter example.
Upvotes: 1