如何使用listview显示sqlite数据库中的数据 [英] How to show data from sqlite database using listview

查看:373
本文介绍了如何使用listview显示sqlite数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用CustomCursor Adapter从android中的sqlite数据库填充listView。问题是我运行程序时listView没有显示任何内容。显示空列表视图。



我尝试过:



 package com.pass; 


import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;


/ **
*一个简单的{@link Fragment}子类。
* /
公共类SiteFragment扩展Fragment {
ListView allSites;
ProgressDialog progressDialogue;
光标光标;

public SiteFragment(){
//必需的空公共构造函数
}


@Override
public查看onCreateView( LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){

//为此片段扩充布局
查看视图= inflater.inflate(R.layout.fragment_site,container,false );
allSites =(ListView)view.findViewById(R.id.LIST_SITES);
SiteCursorAdapter adapter = new SiteCursorAdapter(getActivity(),cursor);
allSites.setAdapter(adapter);
返回视图;
}
@Override
public void onStart(){
super.onStart();
new FetchSitesAsynch()。execute();
}
私有类FetchSitesAsynch扩展AsyncTask< Void,Void,Void> {

@Override
protected void onPreExecute(){
super.onPreExecute( );
progressDialogue = new ProgressDialog(getActivity());
progressDialogue.setTitle(检索网站信息);
progressDialogue.setMessage(Please Wait ...);
progressDialogue.setCancelable(true);
progressDialogue.setIndeterminate(true);

progressDialogue.show();
}

@Override
protected Void doInBackground(Void ... params){
数据库数据库=新数据库(getActivity());
//database.getAll();
cursor = database.getAllSitesInfo();
Log.d(网站提取,注意);
返回null;
}

@Override
protected void onProgressUpdate(Void ... values){
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void aVoid){
super.onPostExecute(aVoid);
if(progressDialogue!= null)
progressDialogue.dismiss(); //关闭对话框

}
}
}

解决方案

你好,你可以请参阅我的YouTube视频 [Android studio] 24。 SQLite GetImage TakePicture - YouTube [ ^ ]

Am trying to populate a listView from sqlite database in android using CustomCursor Adapter. The problem is that my listView shows nothing when I run the program. Just empty listview is displayed.

What I have tried:

package com.pass;


import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SiteFragment extends Fragment {
    ListView allSites;
    ProgressDialog progressDialogue;
    Cursor cursor;

    public SiteFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_site, container, false);
        allSites = (ListView) view.findViewById(R.id.LIST_SITES);
        SiteCursorAdapter adapter = new SiteCursorAdapter(getActivity(), cursor);
        allSites.setAdapter(adapter);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        new FetchSitesAsynch().execute();
    }
    private class FetchSitesAsynch extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialogue = new ProgressDialog(getActivity());
            progressDialogue.setTitle("Retrieving Sites Info");
            progressDialogue.setMessage("Please Wait...");
            progressDialogue.setCancelable(true);
            progressDialogue.setIndeterminate(true);

            progressDialogue.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            Database database = new Database(getActivity());
            //database.getAll();
            cursor = database.getAllSitesInfo();
            Log.d("Site Fetched", "Note");
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (progressDialogue != null)
                progressDialogue.dismiss(); // close dialog

        }
    }
}

解决方案

Hi you can see in my youtube video [Android studio]24. SQLite GetImage TakePicture - YouTube[^]


这篇关于如何使用listview显示sqlite数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆