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

  • SEARCH
  • Home
  • 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 9148969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:24:45+00:00 2026-06-17T11:24:45+00:00

I’m trying to create a list of keys from a SharedPreferences object. Since the

  • 0

I’m trying to create a list of keys from a SharedPreferences object. Since the user can add new pairs key-value, I need to do it dynamically.

Here is the onCreate function of my activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = this.getLayoutInflater();
        LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.activity_show_saved, null);

        this.add_here = (LinearLayout)layout.findViewById(R.id.saved_list);

        // Loading shared preferences
        SharedPreferences sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
        Map<String, ?> kv = sp.getAll();

        int i = 0;
        // Iterating through shared preferences
        for(Map.Entry<String, ?> entry : kv.entrySet()) {
            Toast.makeText(this, entry.getKey(), Toast.LENGTH_SHORT).show();
            TextView to_add = new TextView(this);
            to_add.setText(entry.getKey());
            to_add.setId(i++);
            this.add_here.addView(to_add, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }

        this.setContentView(layout);
    }

Where this.add_here is a class variable declared as private LinearLayout. And here is the xml file which is being inflated:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/background"
    tools:context=".ShowSaved" >

    <com.google.ads.AdView 
        android:id="@+id/adViewTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="*****************"
        ads:adSize="SMART_BANNER"
        ads:testDevices="TEST_EMULATOR"
        ads:loadAdOnCreate="true" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/saved_list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>

    </ScrollView>


</LinearLayout>

Unfortunately I get a blank screen. The problem is in adding the TextViews, since the Toast inside the while correctly shows saved keys as it’s expected to do.

  • 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-06-17T11:24:46+00:00Added an answer on June 17, 2026 at 11:24 am

    As someone have already stated in the comments, that was not the right way of doing it. Here is the result achieved using a ListView. Have a look.

    public class ShowSaved extends Activity implements OnItemClickListener {
    
        // Layout
        private ListView add_here;
        private TextView empty_view;
    
        // String Array
        private ArrayList<String> string = new ArrayList<String>();
    
        // SharedPreferences object and editor
        private SharedPreferences sp;
    
        // Array adapter for ListView add_here
        private ArrayAdapter<String> saved;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            this.setContentView(R.layout.activity_show_saved);
    
            this.add_here = (ListView)findViewById(R.id.saved_list);
            this.empty_view = (TextView)findViewById(R.id.empty_text);
    
            // Loading shared preferences and editor
            this.sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
    
            // Creating the Adapter with all strings in an array
            this.saved = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, string);
            this.saved.setNotifyOnChange(true);
    
            // Retrieving all pairs in SharedPreferences sp
            Map<String, ?> kv = sp.getAll();
    
            // Iterating through shared preferences
            for(Map.Entry<String, ?> entry : kv.entrySet()) {       
                saved.add(entry.getKey());
            }
    
            // Setting Adapter to ListView and empty View
            this.add_here.setEmptyView(empty_view);
            this.add_here.setAdapter(this.saved);
    
            // Setting Listener for ListView add_here
            this.add_here.setOnItemClickListener(this);
        }
    

    Note: this code won’t compile on its own since it lacks the onItemClick function required for the OnItemClickListener interface.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:

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.