Content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
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>
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"/>
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无结果显示,请问怎么解决?
database=openHelper.getReadableDatabase();
应该这样写
database=openHelper..getWritableDatabase()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。