我试图将数据从sqlLite数据库填充到列表视图中。

以下是我尝试使用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);

    }
}



}


膨胀的listview.xml是

  <?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" />




尝试这样做时出现以下错误。


09-21 09:13:04.715 10142-10142 / com.example.joshigir.finance
D / OpenGLRenderer:启用调试模式0 09-21 09:13:45.007
10142-10142 / com.example.joshigir.finance W / EGL_genymotion:
eglSurfaceAttrib未实现09-21 09:13:46.507
10142-10142 / com.example.joshigir.finance D / AndroidRuntime:正在关闭
VM 09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
W / dalvikvm:线程id = 1:线程退出,未捕获的异常
(group = 0xa4c96b20)09-21 09:13:46.507
10142-10142 / com.example.joshigir.finance E / Android运行时:致命
例外:主要09-21 09:13:46.507
10142-10142 / com.example.joshigir.finance E / AndroidRuntime:流程:
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:在
com.example.joshigir.finance.TransactionActivity $ Myadapter.bindView(TransactionActivity.java:69)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.CursorAdapter.getView(CursorAdapter.java:254)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.AbsListView.obtainView(AbsListView.java:2255)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.ListView.onMeasure(ListView.java:1175)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:719)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.FrameLayout.onMeasure(FrameLayout.java:310)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.FrameLayout.onMeasure(FrameLayout.java:310)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.LinearLayout.onMeasure(LinearLayout.java:588)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于android.view.View.measure(View.java:16497)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-21 09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
android.widget.FrameLayout.onMeasure(FrameLayout.java:310)09-21
09:13:46.507 10142-10142 / com.example.joshigir.finance
E / AndroidRuntime:位于
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:位于android.view.View.measure(View.java:16497)


请帮助找出错误。

最佳答案

@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);

}

08-18 20:12