无法在sqlite数据库(android studio)的listview中插入更多值。 [英] Unable to insert more values in listview in sqlite database(android studio).

查看:96
本文介绍了无法在sqlite数据库(android studio)的listview中插入更多值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在android中的SQLite数据库中插入一些值。

之前插入的值,现在当我运行我的应用程序时,它没有显示我想要添加的值。

我的应用程序基本上具有操作栏中的搜索功能。

主要活动如下:



我尝试过:



I am trying to insert some values in SQLite Database in android.
The values were inserting before, now when I run my app it is not showing the values that I am trying to add.
My application has basically the searching functionality in action bar.
The main activity is attached below.

What I have tried:

package com.admin.search;
import android.annotation.TargetApi;
import android.app.SearchManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private CustomAdapter customAdapter;
    ListView listView;
    Cursor cursor;
    StationRepo stationRepo ;
    private final static String TAG= MainActivity.class.getName().toString();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.admin.fyp.R.layout.activity_main);
        stationRepo = new StationRepo(this);


        cursor=stationRepo.getStudentList();
        customAdapter = new CustomAdapter(MainActivity.this,  cursor, 0);
        listView = (ListView) findViewById(com.admin.fyp.R.id.lstStudent);
        listView.setAdapter(customAdapter);

        if(cursor==null) insertDummy();
    }

    private void insertDummy(){
        Station station= new Station();


        station.stop ="Bus Stop: Youhana Abaad\nDistance: 5.0 Km";
        station.name="Ataari Saroba";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Bhatti Chowk\nDistance: 4.1 Km";
        station.name="Azaadi Chowk";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Katchery\nDistance: 1.9 Km";
        station.name="Bhatti Chowk";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Qaddafi Stadium\nDistance:0.8 km";
        station.name="Canal";
        stationRepo.insert(station);


        stationRepo = new StationRepo(this);

        station.stop ="Kamahan\nDistance: 2.1 Km";
        station.name="Chungi Amar Sidhu";
        stationRepo.insert(station);


        stationRepo = new StationRepo(this);

        station.stop ="MAO College\nDistance: 1.7 Km";
        station.name="Civil Secretariat";
        stationRepo.insert(station);


        stationRepo = new StationRepo(this);
        station.stop ="Bus Stop: Gajjumata\nDistance: 0.9 Km";
        station.name="Dulu Khurd";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Dullu Khurd\nDistance: 0.9 Km";
        station.name="Gajjumata";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Chungi Amar Sidhu\nDistance: 2.5 Km";
        station.name="Ghazi Chowk";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Canal\nDistance: 1.6 Km";
        station.name="Ichra";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Qainchi\nDistance: 6.0 Km";
        station.name="Itefaq Hospital";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Qartabah Chowk\nDistance: 2.0 Km";
        station.name="Janazgah";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Model Town\nDistance: 4.3 Km";
        station.name="Kalma Chowk";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Ataari Saroba\nDistance: 2.9 Km";
        station.name="Kamahan";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Civil Secretariat\nDistance: 0.9 Km";
        station.name="Katchery";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Janazgah\nDistance: 2.0 Km";
        station.name="MAO College";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Naseerahbad\nDistance: 3.7 Km";
        station.name="Model Town";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Itefaq Hospital\nDistance: 4.1 Km";
        station.name="Naseerahbad";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);
        station.stop ="Bus Stop: Timber Market\nDistance: 0.8 Km";
        station.name="Niazi Chowk";
        stationRepo.insert(station);


        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Kalma Chowk\nDistance: 3.0 Km";
        station.name="Qaddafi Stadium";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Ghazi Chowk\nDistance: 1.1 Km";
        station.name="Qainchi";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Shama\nDistance: 1.2 Km";
        station.name="Qartaba Chowk";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Niazi Chowk\nDistance: 1.9 Km";
        station.name="Shahdra";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Ichra\nDistance: 0.7 Km";
        station.name="Shama";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Azaadi Chowk\nDistance: 2.2 Km";
        station.name="Timber Market";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop ="Bus Stop: Dulu Khurd\nDistance: 0.9 Km";
        station.name="Youhana Abaad";
        stationRepo.insert(station);

        //Feeder Routes.
        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Bhatti Chowk\nDistance: 0.9 Km";
        station.name="Railway Station(FR-01)";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Bhatti Chowk\nDistance: 0.9 Km";
        station.name="Samanabad Morr(FR-02)";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Shahdara\nDistance: 0.9 Km";
        station.name="Railway Station(FR-03)";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Chungi Amar Sidhu\nDistance: 0.9 Km";
        station.name="R.A Bazar(FR-04)";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Bhatti Chowk\nDistance: 0.9 Km";
        station.name="Shadbagh Underpass(FR-05)";
        stationRepo.insert(station);

        stationRepo = new StationRepo(this);

        station.stop="Bus Stop: Raj Ghar Chowk\nDistance: 0.9 Km";
        station.name="Babu Sabu(FR-06)";
        stationRepo.insert(station);


    }

    @Override
    public void onResume(){
        super.onResume();

    }

    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(com.admin.fyp.R.menu.options_menu, menu);


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView search = (SearchView) menu.findItem(com.admin.fyp.R.id.search).getActionView();
            search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

            search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String s) {
                    Log.d(TAG, "onQueryTextSubmit ");
                    cursor=stationRepo.getStudentListByKeyword(s);
                    if (cursor==null){
                        Toast.makeText(MainActivity.this,"No records found!",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(MainActivity.this, cursor.getCount() + " records found!",Toast.LENGTH_LONG).show();
                    }
                    customAdapter.swapCursor(cursor);

                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    Log.d(TAG, "onQueryTextChange ");
                    cursor=stationRepo.getStudentListByKeyword(s);
                    if (cursor!=null){
                        customAdapter.swapCursor(cursor);
                    }
                    return false;
                }

            });

        }

        return true;
    }
}

推荐答案

2件事。



首先,不要使用 ListView ,考虑 RecyclerView 。 Android文档可以告诉您如何执行此操作,使用RecyclerView创建列表Android开发者 [ ^ ]。 RecyclerView 执行最佳做法。



其次,考虑在Android中使用Room ORM for SQLite。 会议室持久性库| Android开发者 [ ^ ]将帮助您更好地最大化应用程序中的数据库使用。其次,由于它是一个ORM,您只需要处理您的业务逻辑,现在如何管理数据库。我想您将能够理解,哪种逻辑阻止您添加更多数据。
2 things.

First, don't use ListView, consider RecyclerView. Android documentation can tell you how to do that, Create a List with RecyclerView | Android Developers[^]. RecyclerView enforces the best practices.

Then secondly, consider using Room ORM for SQLite in Android. Room Persistence Library | Android Developers[^] will help you in better maximizing the database usage in the apps. Secondly, since it is an ORM, you only have to take care of your business logic, and now how to manage the database. I think you will be able to understand, which logic is preventing you from adding more data.


这篇关于无法在sqlite数据库(android studio)的listview中插入更多值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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