【问题标题】:display sqlite in listview在列表视图中显示 sqlite
【发布时间】:2014-01-25 18:48:32
【问题描述】:

我想将我的Sqlite 数据显示到我的ListView 中,但我做不到。

现在它可以显示Toast,但我怎样才能将它运行到我的ListView

谢谢。

public class fehrest extends Activity {
    public String fonts="BNazanin.ttf";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // DATABASE START
        DBAdapter db = new DBAdapter(this);
        try {
            String destPath = "/data/data/" + getPackageName() + "/databases";
            File f = new File(destPath);
            if (!f.exists()) {              
                f.mkdirs();
                f.createNewFile();

                //---copy the db from the assets folder into 
                // the databases folder---
                CopyDB(getBaseContext().getAssets().open("mydb"),
                    new FileOutputStream(destPath + "/MyDB"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //---get all contacts---
        db.open();
        Cursor c = db.getAllContacts();
        if (c.moveToFirst()) {
            do {
                DisplayContact(c);
            } while (c.moveToNext());
        }
        db.close();

        //DATABASE END

        setFace();
        //Tab2 contents
        }

        //DATABASE COPY FILES

        public void CopyDB(InputStream inputStream, 
                    OutputStream outputStream) throws IOException {
            //---copy 1K bytes at a time---
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            inputStream.close();
            outputStream.close();
        }

        // DATABASE COPY FILES END

        // DATABASE SHOW INFO
        public void DisplayContact(Cursor c) {

            Toast.makeText(this,
                "id: " + c.getString(0) + "\n" +
                "Name: " + c.getString(1) + "\n" +
                "Email:  " + c.getString(2),
                Toast.LENGTH_LONG).show();
            }
            // DATABASE SHOW INFO END

【问题讨论】:

  • 阅读安卓记事本教程
  • 您尝试过我帖子中的建议吗?有用吗??
  • 对不起,但它不起作用

标签: android sqlite android-listview


【解决方案1】:

您可以执行以下操作。您也可以使用 CustomAdapter。例如,我使用了SimpleAdapter

http://developer.android.com/reference/android/widget/SimpleAdapter.html

    ListView lv= (ListView)findViewById(R.id.listview);
    //listview in main.xml
    String[] from = new String[] {"id", "name", "email"};
    int[] to = new int[] { R.id.textView1, R.id.textView2, R.id.textView3};
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    db.open();
    Cursor c = db.getAllContacts();
     if(c.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();  
            String id =  c.getString(0);
            String name= c.getString(1);
            String email = c.getString(2);   
            map.put("id",id);
            map.put("name",name);
            map.put("email",email); 
            fillMaps.add(map); 
        } while (c.moveToNext());
    }
    db.close();

    } 

    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.mylayout, from, to);
    lv.setAdapter(adapter);

mylayout.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="50dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="23dp"
        android:text="TextView" />

</RelativeLayout>

您也可以使用游标适配器,在这种情况下可能更合适

  String[] from = new String[] {"id", "name", "email"}; // columns
  int[] to = new int[] { R.id.textView1, R.id.textView2, R.id.textView3}; 
  SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.mylayout, c, from, to);

注意:

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

此构造函数在 API 级别 11 中已弃用。不鼓励使用此选项,因为它会导致游标查询在应用程序的 UI 线程上执行,从而可能导致响应速度不佳甚至应用程序无响应错误。作为替代方案,使用 LoaderManagerCursorLoader

【讨论】:

  • 这不是 sqlitedb 数据的处理方式,这里 CursorAdapter(或者更确切地说是它的派生类之一)是显而易见的要使用的类
  • @pskink 是的,您可以使用光标适配器或自定义适配器。我只是用一个例子展示了它是如何完成的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 2016-01-21
相关资源
最近更新 更多