Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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;
    }
}
Posted
Updated 10-Mar-18 23:18pm
v2
Comments
Richard MacCutchan 11-Mar-18 4:37am    
Please format your code properly and show where the problem occurs.
Member 13390616 11-Mar-18 6:03am    
I created an object, trying to insert values in a listView in SQLite database.
Failed to insert the values from

//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);
to ownwards..

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900