如何使用原始查询从数据库中显示listview中的数据

时间:2013-03-15 21:50:06

标签: android listview

在我的项目中有2项活动;在一个Activity中,用户输入2个症状并按下一个按钮,这将打开下一个Activity。我希望下一个活动以列表视图格式显示症状的所有可能疾病的名称。谁能告诉我怎么做?我试过了,但它显示了nullpointer异常。我正在使用Sqlite数据库。

public class LstView extends Activity {
private Database db;
private Auto ato;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listname);
        ListView lstv =(ListView) findViewById(R.id.listView1);
        db=new Database(LstView.this);
        db.openDB();
        String a=ato.selected;
        String b=ato.selected1;
       // String[] lst=db.getName(a, b);
        String[] from=new String[]{db.COLUMN_1_NAME};
       int[] to= new int[]{R.id.text};
       Cursor csr=db.getName(a, b); 
      // ArrayAdapter<String> adt= new ArrayAdapter<String>(getBaseContext(),R.layout.list_view,lst);
       SimpleCursorAdapter adt =new SimpleCursorAdapter(this, R.layout.list_view, csr, from, to);
      // SimpleCursorAdapter adt = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, db.getName(a,b), new String[] { "Name" }, new int[] { android.R.id.text1 });
        lstv.setAdapter(adt);
 }


}

String[] Sympt= db.getSymptom();



    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_view,Sympt);
    textView.setAdapter(adapter);
    textView.setThreshold(1);
    textView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView arg0, View arg1, int arg2,
                long arg3) {

             int    idn=arg0.getId();
              selected = arg0.getItemAtPosition(arg2).toString();
                System.out.println("newww "+idn);
                System.out.println("newww1 "+selected);
                String[] Sympt1=db.getSymptom1(selected);
                for(int i=0;i<Sympt1.length;i++)
                {
                    System.out.println("value array "+Sympt1[i]);
                }
                ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getBaseContext(), R.layout.list_view,Sympt1);
                textView1.setAdapter(adapter1);
                textView1.setThreshold(1);
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_SHORT).show();
        }
    });
textView1.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        int idn1=arg0.getId();
        selected1=arg0.getItemAtPosition(arg2).toString();
        System.out.println("neww2"+idn1);
        System.out.println("neww3"+selected1);

    }
});
search.setOnClickListener(new OnClickListener(){

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent i = new Intent(getApplicationContext(),LstView.class);
        startActivity(i);
    }

});



}

here is the which im geting after clicking on button

03-16 09:49:33.993:W / dalvikvm(331):threadid = 1:线程退出时未捕获异常(组= 0x40015560) 03-16 09:49:34.003:E / AndroidRuntime(331):致命异常:主要 03-16 09:49:34.003:E / AndroidRuntime(331):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.autocmplt / com.example.autocmplt.LstView}:java.lang.NullPointerException 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.os.Handler.dispatchMessage(Handler.java:99) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.os.Looper.loop(Looper.java:123) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread.main(ActivityThread.java:3683) 03-16 09:49:34.003:E / AndroidRuntime(331):at java.lang.reflect.Method.invokeNative(Native Method) 03-16 09:49:34.003:E / AndroidRuntime(331):at java.lang.reflect.Method.invoke(Method.java:507) 03-16 09:49:34.003:E / AndroidRuntime(331):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 09:49:34.003:E / AndroidRuntime(331):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-16 09:49:34.003:E / AndroidRuntime(331):at dalvik.system.NativeStart.main(Native Method) 03-16 09:49:34.003:E / AndroidRuntime(331):引起:java.lang.NullPointerException 03-16 09:49:34.003:E / AndroidRuntime(331):at com.example.autocmplt.LstView.onCreate(LstView.java:22) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-16 09:49:34.003:E / AndroidRuntime(331):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-16 09:49:34.003:E / AndroidRuntime(331):... 11更多 03-16 09:49:37.643:I / Process(331):发送信号。 PID:331 SIG:9

1 个答案:

答案 0 :(得分:0)

什么是数据库?它没有安卓类,所以如果你使用的是SQLite,那么你使用的不是Android平台提供的版本。

每当您收到异常时,必须发布logCat。按照此列表的大多数人都会按时间进行操作,因此我们无法花一小时搜索您的代码来查找可能导致问题的内容。相反,logCat会立即识别导致错误的语句。

您还应该考虑在代码中添加注释,并为类,方法和变量提供更多的助记符名称。从头开始阅读代码很难做到,当我们不得不查看代码来确定应该是什么时,它会变得更糟。