android - 使用基本适配器和 ListView 显示来自 SQLite 的变量数据

标签 android sqlite listview adapter

嗨,我正在开发应用程序,该应用程序包含来自解析的 json 通知,应用程序将其存储在 SQLite 中主要 Activity 是我的代码

public class Receiver extends ParsePushBroadcastReceiver {


 @Override
    public void onPushReceive(Context context, Intent intent) {
        super.onPushReceive(context, intent);
        Log.e("Push", "Clicked");
        Intent i = new Intent(context, splashscreen.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i); 
        
        
        Log.d("Alert: ", "Inserting from onReceive"); 
        Bundle extras = intent.getExtras();
        String jsonData = extras.getString("com.parse.Data");
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonData);
            String alert = jsonObject.getString("alert");
            Log.d("Insert: ",alert); 
            String Message = jsonObject.getString("Message");
            Log.d("Title: ",Message); 
            String Date = jsonObject.getString("Date");
            Log.d("Date: ",Date); 
             
        
            DatabaseHandler db=new DatabaseHandler(context);
          
            db.addnotification(new notification(alert,Message,Date));
            
            Log.d("Reading: ", "Reading all notifications.."); 
            List<notification> notifications = db.getAllnotifications();       
             
            for (notification cn : notifications) {
                String log = "Id: "+cn.getID()+" ,Title: " + cn.gettitle() + " ,Message: " + cn.getmessage()+ " ,Date: " + cn.gettimedate();
                // Writing notifications to log
                Log.d("Name: ", log);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
}

最佳答案

进行中,,,

  • 编写您的 Dao 类(在您的情况下为 DatabaseHandler 类)以从数据库中获取数据(或者您可以编写一个内容提供程序——实现这一点的好方法。)

  • 一个示例 dao 类如下,
    public class DocumentRepository {
    
                public static final String TABLE_DOCUMENT = "document";
                public static final String COLUMN_ID = BaseColumns._ID;
                public static final String RECORD_ID = "record_id";
                public static final String UUID_DOCUMENT_NAME = "unique_file_name";
    
                public static final String DOCUMENT_TABLE_CREATE = "create table if not exists "
                        + TABLE_DOCUMENT + "(" + COLUMN_ID
                        + " integer primary key autoincrement, " + RECORD_ID
                        + " integer , " + UUID_DOCUMENT_NAME
                        + " text );";
                private Context context;
    
                public DocumentRepository(Context context) {
                    this.context = context;
                }
    
    
                public ArrayList<Document> geDocuments(Integer customerId) {
                    ArrayList<Document> documents = new ArrayList<>();
                    String query = "SELECT * FROM " + TABLE_DOCUMENT + " WHERE " + RECORD_ID + " = '" + customerId + "'";
                    SQLiteDatabase database = ......// create a instance of your SqliteDatabase implementation 
                    Cursor cursor = database.rawQuery(query, null);
    
                    if (cursor.moveToFirst()) {
                        do {
                            Document document = new Document();
                            document.setId(cursor.getInt(0));
                            document.setUniqueIdentifier(cursor.getString(3));
                            documents.add(document);
                        } while (cursor.moveToNext());
                    }
                    return documents;
                }
            }
    
  • 获取要在 ListView 中显示的对象列表。

  • 在此示例中 geDocuments()会给你对象列表。
  • 将其注入(inject)您的 Adapter 并为 listView 引用设置 Adapter。
  • 关于android - 使用基本适配器和 ListView 显示来自 SQLite 的变量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406666/

    相关文章:

    Android异常: java. io.IOException:打开失败:EACCES(权限被拒绝)

    Android Studio - 没有互联网连接的 Android 模拟器 Wifi

    android - 调用 setAdapter 时 ViewPager 并不总是刷新,FragmentStatePagerAdapter

    Android: java.lang.OutOfMemoryError: 线程创建失败

    iphone - SQLite count(*) 如何得到结果?

    iphone - FMDB 存储 typedef 枚举值

    SQLite3 Group By 错误地仅返回 1 行

    android - 将用户导航到列表项上的新屏幕单击 React native

    android - getPositionForSection 和 getSectionForPosition 的区别

    java - Android - 适配器数据更改后,滚动到 ListFragment 中 ListView 中的位置