程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-顯示更新成功了,但我在查詢數據時按選擇按鈕並沒有更新

編程-顯示更新成功了,但我在查詢數據時按選擇按鈕並沒有更新

編輯:編程解疑
顯示更新成功了,但我在查詢數據時按選擇按鈕並沒有更新

圖片說明
圖片說明
MainActivity.java

 package com.example.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    private static final String TAG="Add"; 
    private static final ListAdapter listAdapter = null; 
    private EditText ecode,ename,ebirth; 
    private Button badd,bdel,bupdate,bsele; 
    private SQLiteDatabase db=null; 
    private TextView tedatashow; 
    private ListView datashow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ecode=(EditText)findViewById(R.id.ecode); 
        ename=(EditText)findViewById(R.id.ename); 
        ebirth=(EditText)findViewById(R.id.ebirth); 
        badd=(Button)findViewById(R.id.badd); 
        bdel=(Button)findViewById(R.id.bdel); 
        bupdate=(Button)findViewById(R.id.bupdate); 
        bsele=(Button)findViewById(R.id.bsele); 
        tedatashow=(TextView)findViewById(R.id.tedatashow); 
        datashow=(ListView)findViewById(R.id.datashow); 
        badd.setOnClickListener((android.view.View.OnClickListener) this); 
        bdel.setOnClickListener((android.view.View.OnClickListener) this); 
        bsele.setOnClickListener((android.view.View.OnClickListener) this); 
        bupdate.setOnClickListener((android.view.View.OnClickListener) this);
    }

    public void onClick(View v) 
    { 
        // TODO Auto-generated method stub 
        MyDBHelper helper=new MyDBHelper(this); 
        String code=ecode.getText().toString().trim();
        String name=ename.getText().toString().trim();
        String birth=ebirth.getText().toString().trim();
        if(v==badd)
        {
            if(code.length()!=0 && name.length()!=0 && birth.length()!=0)
            {
                try
                {
                    db=helper.getWritableDatabase(); 
                    String sql="INSERT INTO user(ecode,ename,ebirth)" +"VALUES('"+ecode.getText()+"','" +ename.getText()+"','" +ebirth.getText()+"')"; 
                    db.execSQL(sql); 
                    Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show(); 
                    ecode.setText(""); 
                    ename.setText(""); 
                    ebirth.setText(""); 
                }
                catch(Exception e)
                {
                    Toast.makeText(this, "出錯了!"+ e.getMessage(),Toast.LENGTH_LONG).show(); 
                } 
            }
            else
                Toast.makeText(this, "學號和姓名出生日期不能為空!", Toast.LENGTH_LONG).show(); 
        } 
        if(v==bdel)
        {
            if(code.length()!=0)
            {
                try
                {
                    db=helper.getWritableDatabase(); 
                    String sql="delete from user where ecode='"+ecode.getText()+"'"; 
                    db.execSQL(sql); 
                    Toast.makeText(this, "成功刪除!", Toast.LENGTH_LONG).show(); 
                    ecode.setText(""); 
                }
                catch(Exception e)
                {
                    Toast.makeText(this, "出錯了!", Toast.LENGTH_LONG).show(); 
                }
            }
        }
                if(v==bupdate)
                {
                    db=helper.getWritableDatabase();
                    if(code.length()!=0 && name.length()!=0 && birth.length()!=0)
                    {
                        try
                        {
                            String sql="update user set ecode='"+ecode.getText() +"'where ename='"+ename.getText()+"'and ebirth='" +ebirth.getText()+"'and ecode='" +ecode.getText()+"'"; 
                            db.execSQL(sql); 
                            Toast.makeText(this, "成功更新!", Toast.LENGTH_LONG).show(); 
                            ecode.setText(""); 
                            ename.setText(""); 
                            ebirth.setText(""); 
                        }
                        catch(Exception e)
                        {
                            Toast.makeText(this, "出錯了!"+e.getMessage(),Toast.LENGTH_LONG).show(); 
                        } 
                    }
                    else
                        Toast.makeText(this, "學號姓名出生日期不能為空!", Toast.LENGTH_LONG).show(); 
                }
                if(v==bsele)
                {
                    if(code.length()!=0)
                    {
                    try
                    {
                        ArrayList<String> all=new ArrayList<String>(); 
                        String sql="select * from user where ecode =? or ename =? or ebirth =? "; 
                        Cursor result=helper.getReadableDatabase().rawQuery(sql,new String[]{code,code,code}); 
                        while(result.moveToNext())
                        {
                            all.add("["+result.getString(0)+"]"+""+result.getString(1)+","+result.getString(2)); 
                        }
                        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,all); 
                        datashow.setAdapter(arrayAdapter);
                    }
                    catch(Exception f)
                    {
                        Toast.makeText(this, "顯示不了", Toast.LENGTH_LONG).show(); 
                    }
                    }
                }
                db.close(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MyDBHelper.java

 package com.example.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper{
    public MyDBHelper(Context context)
    {
        super(context,"mvdb.db",null,2);
    }

    public void onCreate(SQLiteDatabase db)
    {
        String sql="create table user(ecode text,ename text,ebirth text);";
        db.execSQL(sql);
    }

    public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
    {
        String sql="create table user(ecode text,ename text,ebirth text);";
        db.execSQL(sql);
        this.onCreate(db);
    }

}

顯示更新成功了,但我在查詢數據時按選擇按鈕並沒有更新

最佳回答:


arrayAdapter.notifydatasetchanged();
更新完數據調用這個就可以了,不行再問我,qq441648051

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved