Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5932067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:46:59+00:00 2026-05-22T14:46:59+00:00

I have an a ListView which fetches the data from JSON and the app

  • 0

I have an a ListView which fetches the data from JSON and the app saves the downloaded images and texts from the web to the external storage though an ImageLoader class.

I need to make the listview accessible without an internet connection, the app already succesfully saves the cache data into the SD Card.

I found a way to do it by using SQLite. When the app runs for the first time with internet connection, it downloads the JSON data and save them to the sqlite database. When there’s no internet connection, instead of downloading the data from the web, the app calls the data from the sqlite database.

But I’m still a newbie and I have difficulties in implementing the SQLite with the codes I have.

Here are my codes:

public class DBAdapter {

       public static String KEY_PROJECTTITLE = "project title";
       public static String KEY_ORGANIZATIONTITLE = "organization title";
       public static String KEY_KEYWORD = "keyword";
       public static String KEY_SHORTCODE = "short code";
       public static String KEY_PROJECTDESCRIPTION = "description";
       public static String KEY_SMALLIMAGE = "smallImageUrl";
       public static String KEY_BIGIMAGE = "bigImageUrl";
       public static String KEY_PRICE= "price";
       public static String KEY_COUNTRY= "country";


       private static final String DATABASE_NAME = "applicationdata";
       private static final String DATABASE_TABLE_PROJECT = "Project";

       private static final int DATABASE_VERSION = 17;


       private static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE_PROJECT+" (" +
        KEY_PROJECTTITLE +      " TEXT NOT NULL, " +
        KEY_ORGANIZATIONTITLE +         " TEXT NOT NULL, "+
        KEY_KEYWORD +   " TEXT NOT NULL PRIMARY KEY, " +
        KEY_SHORTCODE +         " TEXT NOT NULL, "+
        KEY_PROJECTDESCRIPTION +    " TEXT NOT NULL, "+
        KEY_SMALLIMAGE +            " TEXT NOT NULL, "+
        KEY_BIGIMAGE +      " TEXT NOT NULL, "+
        KEY_PRICE +         " TEXT NOT NULL, "+
        KEY_COUNTRY +       " TEXT NOT NULL)";

       private final Context context; 

        private DatabaseHelper dbhelper;
        private SQLiteDatabase db;

        public DBAdapter(Context ctx) 
        {
            this.context = ctx;
            dbhelper = new DatabaseHelper(context);
        }

        public static class DatabaseHelper extends SQLiteOpenHelper {


            public DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL(DATABASE_CREATE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_PROJECT);
                onCreate(db);

            }
        }
        //---opens the database---
        public DBAdapter open() throws SQLException 
        {
            db = dbhelper.getWritableDatabase();
            return this;
        }

        //---closes the database---    
        public void close() 
        {
            dbhelper.close();
        }

        public long insertProject(String project_title, String organization_title, String keyword, String short_code, String project_description,String smallImageUrl,String bigImageUrl,String price, String country) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_PROJECTTITLE, project_title);
            initialValues.put(KEY_ORGANIZATIONTITLE, organization_title);
            initialValues.put(KEY_KEYWORD, keyword);
            initialValues.put(KEY_SHORTCODE, short_code);
            initialValues.put(KEY_PROJECTDESCRIPTION, project_description);
            initialValues.put(KEY_SMALLIMAGE, smallImageUrl);
            initialValues.put(KEY_BIGIMAGE, bigImageUrl);
            initialValues.put(KEY_PRICE, price);
            initialValues.put(KEY_COUNTRY, country);
            return this.db.insert(DATABASE_TABLE_PROJECT, null, initialValues);
        }
}

ListView Adapter:

public class ProjectAdapter extends ArrayAdapter<Project> {

    int resource;
    String response;
    Context context;
    List<Project> items;
    private ImageLoaderCache imageLoader;
    LayoutInflater mInflater;
    Activity activity;

    private DBAdapter mDBHelper;
    // Initialize adapter
    public ProjectAdapter(Context context, int resource, List<Project> items,
            Activity activity) {
        super(context, resource, items);
        this.resource = resource;
        imageLoader = new ImageLoaderCache(context);
        this.items = items;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        // Inflate the view
        if (convertView == null) {

            convertView = mInflater.inflate(resource, null);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.textTitle = (TextView) convertView
                    .findViewById(R.id.txt_title);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Project project = items.get(position);

        holder.textTitle.setText(project.project_title);

        String imageurl = project.smallImageUrl;
        holder.image.setTag(imageurl);
        imageLoader.displayImage(imageurl, activity, holder.image);
        return convertView;

    }

    static class ViewHolder {

        TextView textTitle;

        ImageView image;
    }
}

the class where the JSON data is populated to the ListView

public class ProjectsList extends Activity {
    /** Called when the activity is first created. */
    //ListView that will hold our items references back to main.xml
    ListView lstTest;

    //Array Adapter that will hold our ArrayList and display the items on the ListView
    ProjectAdapter arrayAdapter;


    //List that will  host our items and allow us to modify that array adapter
    ArrayList<Project> prjcts=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projects_list);

        //Initialize ListView
        lstTest= (ListView)findViewById(R.id.lstText);

         //Initialize our ArrayList
        prjcts = new ArrayList<Project>();
        //Initialize our array adapter notice how it references the listitems.xml layout

        arrayAdapter = new ProjectAdapter(ProjectsList.this, R.layout.listitems,prjcts,ProjectsList.this);

        //Set the above adapter as the adapter of choice for our list
            lstTest.setAdapter(arrayAdapter);
            if (isOnline())
            {
        //Instantiate the Web Service Class with he URL of the web service not that you must pass
        WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json");


        //Pass the parameters if needed , if not then pass dummy one as follows
        Map<String, String> params = new HashMap<String, String>();
        params.put("var", "");

        //Get JSON response from server the "" are where the method name would normally go if needed example
        // webService.webGet("getMoreAllerts", params);
        String response = webService.webGet("", params);

        try
        {
            //Parse Response into our object
            Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType();

            //JSON expects an list so can't use our ArrayList from the lstart
            List<Project> lst= new Gson().fromJson(response, collectionType);


            //Now that we have that list lets add it to the ArrayList which will hold our items.
            for(Project l : lst)
            {
                prjcts.add(l);
                ConstantData.projectsList.add(l);
            }

            //Since we've modified the arrayList we now need to notify the adapter that
            //its data has changed so that it updates the UI
            arrayAdapter.notifyDataSetChanged();
        }
        catch(Exception e)
        {
            Log.d("Error: ", e.getMessage());
        }
       }


        lstTest.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              

                Intent care = new Intent(ProjectsList.this, ProjectDetail.class);
                care.putExtra("spendino.de.ProjectDetail.position",position);
                startActivity(care);
            }
        });

    }


    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        } else {
             AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
             alertbox.setTitle("spendino Helfomat");
             alertbox.setMessage ("Please check your internet connection");
             alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                      //Main.this.finish();
                 }
             });
             alertbox.show();
            return false;
        }
    }   
}

I’m open to any kind of solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:47:00+00:00Added an answer on May 22, 2026 at 2:47 pm

    asuming that

    public static String KEY_PROJECTTITLE = "project title";
    

    is a Column name in db it should be without spaces ot within []

    EDIT:
    and for FSM’s sake if you’re using SQLite dont use ArrayAdapter use (Simple)CursorAdapter instead

    to avoid your next question C:\android\android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\view\List7.java <- (Simple)CursorAdapter sample

    2nd EDIT:

    here is working sample of your program it takes 2h to made it … and yeah i am new in android too (1 month or so)

    LooserSample.zip

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m new to android,I have an activity class which fetches Json string from url
I have a following problem. I have a ListView which returns data from SQL
I have ListView which shows images from an ImageList. Now wanted to get index
I have a listview which loads its data from sqlite database. Each row in
Possible Duplicate: nullPointerException in multi column list I have following app which fetches data
I have a class A which is responsible for fetching data from web services
I have a WPF ListView which repeats the data vertically. I cannot figure out
This is a very common scenario: displaying images in a ListView which have to
I have an app with a large ListView which is terribly slow so I'm
I have a listview which is populated by a SimpleCursorAdapter that is binding data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.