我正在尝试使用表格布局从sqllite动态获取数据。我使用getLayoutInflater()
进行了膨胀,但是无法正常工作,也无法获取表格行中的数据。
代码:
MainActivity.java
package com.example.datab;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView user_tv,pass_tv; View view;
TableLayout tb;int counter=0;
TableRow tr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutt);
tb=(TableLayout)findViewById(R.id.tablelayout);
//tr=(TableRow)findViewById(R.id.tableRow1);
SQLiteDatabase md=openOrCreateDatabase("databa",MODE_PRIVATE,null);
md.execSQL("CREATE TABLE IF NOT EXISTS data (Username varchar(20),Password varchar(20))");
md.execSQL("INSERT INTO data VALUES('nobal','Sharma')");
Cursor cr=md.rawQuery("select * from data", null);
cr.moveToFirst();
//String username=cr.getString(cr.getColumnIndex("Username"));
if (cr.moveToFirst()){
do{
String username = cr.getString(cr.getColumnIndex("Username"));
String passwd=cr.getString(cr.getColumnIndex("Password"));
TableRow view=(TableRow)getLayoutInflater().inflate(R.layout.layoutt,tb, false);//here NPE
user_tv=(TextView)view.findViewById(R.id.textView1);
pass_tv=(TextView)view.findViewById(R.id.textView2);
user_tv .setText(username );
pass_tv.setText(passwd);
tb.addView(view);//and NPE here
}while(cr.moveToNext());
}cr.close();
}
}
以下是layoutt.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textSize="35dp"
android:paddingLeft="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="@drawable/cell_shape"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cell_shape"
android:text="Medium Text"
android:textSize="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="20dp"/>
</TableRow>
</TableLayout>
logcat现在没有显示,但是我测试了,代码在代码中的注释行上给出了空指针异常
这是Logcat:
07-30 01:47:35.219: D/AndroidRuntime(973): Shutting down VM
07-30 01:47:35.219: W/dalvikvm(973): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-30 01:47:35.238: E/AndroidRuntime(973): FATAL EXCEPTION: main
07-30 01:47:35.238: E/AndroidRuntime(973): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datab/com.example.datab.MainActivity}: java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.TableRow
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.os.Looper.loop(Looper.java:137)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-30 01:47:35.238: E/AndroidRuntime(973): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 01:47:35.238: E/AndroidRuntime(973): at java.lang.reflect.Method.invoke(Method.java:525)
07-30 01:47:35.238: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-30 01:47:35.238: E/AndroidRuntime(973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-30 01:47:35.238: E/AndroidRuntime(973): at dalvik.system.NativeStart.main(Native Method)
07-30 01:47:35.238: E/AndroidRuntime(973): Caused by: java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.TableRow
07-30 01:47:35.238: E/AndroidRuntime(973): at com.example.datab.MainActivity.onCreate(MainActivity.java:42)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.Activity.performCreate(Activity.java:5133)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-30 01:47:35.238: E/AndroidRuntime(973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-30 01:47:35.238: E/AndroidRuntime(973): ... 11 more
答案 0 :(得分:0)
我觉得你采取的方法可以做得更好。 你完全不能使用tablelayout,仍然可以使用表格布局。
一个简单的方法,你的tablerow只不过是一个简单的行,类似的行做了一个布局,最终给你一个tablelayout的感觉。 为什么不在其中创建一个带有两个或三个文本视图的自定义线性布局,并添加了分隔视图。
您可以在扩展BaseAdapter的customadapter类中扩充此布局,并且在getView方法中只需对布局进行充气,并使用可以填充适配器的viewholder类。