2 freedom wings5 freedom_wings5 于 2016.03.01 09:21 提问

安卓,SQLite,执行插入,查询操作,listview无结果显示,怎么解决?

Content_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/insert"
        android:text="按下这里执行和查询操作"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"></ListView>
</LinearLayout>

listview的布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tx1listview"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tx2listview"/>
</LinearLayout>

继承SQLiteOpenHelper的Helper类:

 public class Helper extends SQLiteOpenHelper
{
    String create="create table mytest (id varchar(255)," +
            "number varchar(255))";
    Helper(Context context,String name,int version)
    {
        super(context,name,null,version);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(create);
    }
    @Override
    public  void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
    {

    }

}

MainActivity:

 package com.example.dell_pc.sqlitetest;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button but;
    TextView tx;
    SQLiteOpenHelper openHelper;
    SQLiteDatabase database;
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        but=(Button)super.findViewById(R.id.insert);
        tx=(TextView)super.findViewById(R.id.textview);
        openHelper=new Helper(this,"mytesthelper.db3",1);
        listview=(ListView)super.findViewById(R.id.listview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClick(View  v)
    {
        database=openHelper.getReadableDatabase();
        database.execSQL("insert into mytest (id,number) values ('test1',1)");
        database.execSQL("insert into mytest(id,number) values('test2',2)");
        Cursor cursor=database.rawQuery("select * from mytest where id='test1'",null);
        SimpleCursorAdapter s=new SimpleCursorAdapter(this,R.layout.listview,cursor,new String[]{"id","number"},new int[]{R.id.tx1listview,R.id.tx2listview}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listview.setAdapter(s);
    }
}

按下按钮执行插入和查询操作,listview无结果显示,请问怎么解决?

2个回答

days_of_together
days_of_together   2016.03.01 09:24

database=openHelper.getReadableDatabase();
应该这样写
database=openHelper..getWritableDatabase()

luochoudan
luochoudan   Ds   Rxr 2016.03.01 10:17

一切不贴log的问题都是耍流氓。数据库的打开没错,你确定下面的语句确实插入数据了???
insert into mytest (id,number) values ('test1',1)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!