如何将SQLite表数据显示到listview中?

时间:2014-10-10 15:15:12

标签: android sqlite listview

我有活动 1 GT;主要活动 它创建SQLite表并将存储数据从edittext添加到mysql表

2 - ; viewdatabse活动 它从SQLite表中检索数据并在listview中显示。

问题是多次显示相同的DATA。 恩。 如果我在edittext1中写一些东西,listview将只显示edittext1的多次数据。

这是mainactivity的代码

package com.example.androiddatabase;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    EditText name,password,date,other;
    Button save,show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        name=(EditText)findViewById(R.id.nametext);
        password=(EditText)findViewById(R.id.password);
        date=(EditText)findViewById(R.id.time);
        other=(EditText)findViewById(R.id.other);
        save=(Button)findViewById(R.id.button1);
        show=(Button)findViewById(R.id.button2);
        save.setOnClickListener(this);
        show.setOnClickListener(this);

        SQLiteDatabase db=null;

        try{
        db=openOrCreateDatabase("MyIds",MODE_PRIVATE , null);
        db.execSQL("create table if not exists MyInfo(names VARCHAR(30),passwords VARCHAR(10),dates INTEGER,others VARCHAR(500))");
        }
        catch(Exception e){
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();

        }
        finally{
            if(db.isOpen()){
                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 void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==save)
        {

        String n= name.getText().toString();
        String p= password.getText().toString();
        String d= date.getText().toString();
        String o =other.getText().toString();

        SQLiteDatabase db=null;

                try{
                    db=openOrCreateDatabase("MyIds", MODE_PRIVATE, null);
                    db.execSQL("insert into MyInfo(names,passwords,dates,others)values('"+n+"','"+p+"','"+d+"','"+o+"')");
                }

                catch(Exception e)
                {
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
        finally
        {
            if(db.isOpen())
            {
                db.close();
            }

        }

        }
        else if (v==show)
        {
            Intent i = new Intent(this, viewdatabse.class);
            startActivity(i);
        }

    }

}

这是另一个代表listview的活动,它显示来自mysql的数据

package com.example.androiddatabase;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class viewdatabse extends Activity{

    ListView lv;
    ArrayList<HashMap<String, String>> aa;
    String name,password,date,other;
    HashMap<String, String> hh;
    String n,p,d,o;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdatabse);

        lv=(ListView)findViewById(R.id.listView1);
        aa = new ArrayList<HashMap<String,String>>();

        SQLiteDatabase db=null;

        try{
        db=openOrCreateDatabase("MyIds",MODE_PRIVATE , null);
        Cursor c = db.rawQuery("select * from MyInfo", null);


        while(c.moveToNext())
        {
            hh= new HashMap<String, String>();
            name=c.getString(c.getColumnIndex("names"));
            hh.put(n, name);
            password=c.getString(c.getColumnIndex("passwords"));
            hh.put(p, password);
            date=c.getString(c.getColumnIndex("dates"));
            hh.put(d, date);
            other=c.getString(c.getColumnIndex("others"));
            hh.put(o, other);




            aa.add(hh);

        }


        }
        catch(Exception e){
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();

        }
        finally{
            if(db.isOpen()){
                db.close();
            }
        }

        String s[]={n,p,d,o};


        int i[]={R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4};

        SimpleAdapter sa = new SimpleAdapter(this, aa, R.layout.customlist, s, i);

        lv.setAdapter(sa);
    }
}

这里是mainactivity xml文件,它有4个edittext来获取用户输入

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <EditText
        android:id="@+id/nametext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName">
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="time" />

    <EditText
        android:id="@+id/other"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >


    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show" />

</LinearLayout>

这里是视图数据库活动的xml,它有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="vertical" >

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

</LinearLayout>

这里是用于创建自定义列表视图的xml,它直接用于simpleadapter

<?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="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

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

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

</LinearLayout>

0 个答案:

没有答案