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 8947653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:49:11+00:00 2026-06-15T12:49:11+00:00

I’m trying to get the position of a list view in android development. I

  • 0

I’m trying to get the position of a list view in android development. I am wanting to get the position value and save it into a pref file. The code I have is

public class Browse extends Activity {

private ListView mainListView;  
private ArrayAdapter<Card> listAdapter; 
public static final String userPrefs = "UserPrefs";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);

    mainListView = (ListView) findViewById( R.id.mainListView);  

    DatabaseHandler dh = new DatabaseHandler(this);

    // Remember to drop existing table if it exists
    dh.removeAll();

    // Insert 4 Lecturers into the database
    Log.d("Database: ", "Inserting values..");
    dh.addCard(new Card(1, "Katniss Evergreen", "11", "33", "55", "44",     R.drawable.katniss));
    dh.addCard(new Card(2, "Peeta Melark", "49", "44", "11", "65", R.drawable.peeta));
    dh.addCard(new Card(3, "Gale Hawthrone", "87", "32", "98", "50", R.drawable.gale));
    dh.addCard(new Card(4, "Haymitch", "30", "32", "45", "31", R.drawable.haymitch));
    dh.addCard(new Card(5, "Effie Trinket", "65", "54", "21", "34", R.drawable.effie));
    dh.addCard(new Card(6, "President Snow", "23", "45", "67", "21", R.drawable.snow));

    List<Card> list = dh.getAll();  

    // Create ArrayAdapter using the list of lecturers
    listAdapter = new ArrayAdapter<Card>(this, R.layout.simplerow, list);

    // Set the ArrayAdapter as the ListView's adapter.  
    mainListView.setAdapter( listAdapter );  

    mainListView.setOnItemClickListener(new OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {
        // TODO Auto-generated method stub
        String value = mainListView.getItemAtPosition(position).toString();
        SharedPreferences sp;
        sp = getSharedPreferences(userPrefs, Activity.MODE_PRIVATE); 
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name", value);
        editor.commit();
    }
    }); 



}



}

This works but it saves the actual name, so if I click Katniss Evergreen it will save the name “Katniss Evergreen”. I don’t want this I want the position value so like 1 for Katniss, 2 for Peeta etc. Any one know how I can do this? I have been working all weekend on this and just can’t get my head around it 🙁 i’m fairly new to this.

  • 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-15T12:49:13+00:00Added an answer on June 15, 2026 at 12:49 pm

    Do you want get position from list or value from Card object ?

    If from list than you had position parameter passed to onClick.

    Otherwise, try get Card object and get value from it.

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {
        Card card  = (Card) mainListView.getItemAtPosition(position);
        int value = card.getId(); // Or other method
    
        SharedPreferences sp;
        sp = getSharedPreferences(userPrefs, Activity.MODE_PRIVATE); 
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("name", value);
        editor.commit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.