Android - 将数据填充到列表视图中

时间:2016-09-21 13:34:16

标签: java android

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

以下是我尝试使用cursoradapter将数据提取到listview中的代码。

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:threadid = 1:线程退出未捕获的异常   (组= 0xa4c96b20)09-21 09:13:46.507   10142-10142 / com.example.joshigir.finance E / AndroidRuntime:致命   例外:主要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: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:在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.measureChildHorizo​​ntal(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:在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:在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:在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:在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:在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:在android.view.View.measure(View.java:16497)

请帮助找出错误。

2 个答案:

答案 0 :(得分:1)

来自bindView(View, Context, Cursor)班级的Myadapter方法:

TextView Accnameview = (TextView)findViewById(R.id.textView);
TextView Incomevalueview=(TextView)findViewById(R.id.textView2);

您需要在方法的findViewById(int)属性上调用view,如下所示:

 TextView Accnameview = (TextView) view.findViewById(R.id.textView);
 TextView Incomevalueview = (TextView) view.findViewById(R.id.textView2);

否则,您正在使用findViewById(int)封闭类中的TransactionActivity方法,该类在“活动”视图层次结构中查找TextView。由于适配器的视图尚未附加到Activity视图,因此它找不到它们并返回null。因此,您在setText(String)方法结束时在null TextView上调用bindView(View, Context, Cursor)

有关CursorAdapter示例,请参阅here

答案 1 :(得分:0)

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

}