2016-09-21 60 views
0

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

下面是我尝试使用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" /> 

试图这样做时,我得到下面的错误。

9月9日至21日:13:04.715 10142-10142/com.example.joshigir.finance d/OpenGLRenderer:启用调试模式0 9月9日至21日:13:45.007 10142-10142/com。示例.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:以未捕获异常退出的线程 (group = 0xa4c96b20)09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime:致命 E XCEPTION:main 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:at android.widget.CursorAdapter。 getView(CursorAdapter.java:254)09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRunti我:在 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: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.measureChildHorizo​​ntal(R elativeLayout.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。金融 E/AndroidRuntime:在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310)09-21 09:13:46.507 10142-10142/com.example.joshigir.finance E/AndroidRuntime:在 机器人.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:在 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:在 android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 9月9日至21日: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:at 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) 9月9日至21日:13:46.507 10142-10142/com.example.joshigir.finance ë/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)

请帮助找出错误。

+0

请发表您的适配器类空指针异常是有意味着你试图访问是空的东西,也可能不会被初始化 –

+0

是吧'R.layout.listitem'或'R.layout.listview' - 只是一个错字?你可能可以假设'Cursor tablecursor = dbOperations.getTableRecords(DBOperations.TABLE_TRANSACTIONS);'返回null - 你从不对空格检查游标,可能在这里抛出:'cursor.moveToFirst();' –

回答

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

} 
1

Myadapter类的bindView(View, Context, Cursor)方法:

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 enclosing类,该类在Activity视图层次结构内查找TextView。由于来自适配器的视图尚未附加到活动视图,因此它没有找到它们并返回null。因此,您在bindView(View, Context, Cursor)方法末尾的空TextView上调用setText(String)

有关CursorAdapter示例,请参见here

+0

非常感谢。这有帮助! –