Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Unclicking Listview Item

Options
  • 09-02-2016 4:28pm
    #1
    Closed Accounts Posts: 789 ✭✭✭


    Hey guys,

    I am creating a listview containing books that a users can tick off as they read. I'm trying to make it so that they can unclick on books that they have clicked on as "read".

    When they click on an item on the listview it changes the alpha of that row to 0.2f and shows a toast saying they read the book. I have created an if else loop that returns the alpha to 1f but the way I've done it makes every first click from the time the listview is opened a 0.2f and every second click a 1f.

    Sorry if I'm not explaining myself well but here is my code:

    public class Books extends AppCompatActivity {

    boolean listRow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_books);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

    final ArrayList<String> topBooks = new ArrayList<String>(asList("1984", "To Kill a Mockingbird", "Pride and Prejudice",
    "Harry Potter and the Sorcerer's Stone", "The Great Gatsby","Jane Eyre","Wuthering Heights","The Catcher in the Rye",
    "The Hobbit","Brave New World","The Lord of the Rings: The Fellowship of the Ring","Don Quixote",
    "Catch-22","The Count of Monte Cristo","Harry Potter and the Goblet of Fire","The Grapes of Wrath","The Adventures of Huckleberry Finn",
    "The Diary of a Young Girl","Gone with the Wind","Harry Potter and the Deathly Hallows","Moby Dick","Harry Potter and the Half-Blood Prince","War and Peace",
    "Animal Farm","Anna Karenina","Ulysses","Lord of the Flies","The Divine Comedy","One Hundred Years of Solitude","Frankenstein","The Perks of Being a Wallflower",
    "The Fault in Our Stars","Good to Great","Harry Potter and the Chamber of Secrets","Harry Potter and the Order of the Phoenix","Harry Potter and the Prisoner of Azkaban",
    "The Amazing Adventures of Kavalier & Clay","The Hunger Games","The Lord of the Rings: The Two Towers","The Lord of the Rings: The Return of the King", "The Maze Runner",
    "Looking for Alaska","Fahrenheit 451", "Hamlet","Gullivers Travels","The Canterbury Tales","Rebecca","The Brothers Karamazov","Lover Awakened","At Grave's End"));

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topBooks);

    booksListView.setAdapter(arrayAdapter);

    final MediaPlayer mPlayer = MediaPlayer.create(this,R.raw.pindrop);

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

    if(listRow) {

    Toast.makeText(getApplicationContext(), "You Read " + topBooks.get(position), Toast.LENGTH_LONG).show();
    view.animate().alpha(0.2f);
    mPlayer.start();
    listRow = false;
    }
    else {

    view.animate().alpha(1f);
    mPlayer.start();
    listRow = true;

    }
    }
    });
    }

    }

    Any help would be greatly appreciated.


Comments

  • Registered Users Posts: 419 ✭✭Mort5000


    Suggest you try StackOverflow.


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    You should use the ViewHolder pattern if you are using listView. Or better still, use the RecyclerView instead. ListView is more or less deprecated and the RecyclerView already enforces the ViewHolder pattern for you.


  • Registered Users Posts: 7,860 ✭✭✭The_B_Man


    Just on your logic, if have this:
    boolean listRow = true
    
    ....
    .....
    
    onItemClicl(...) {
       if(listRow) {
            ...
            view.animate().alpha(0.2f);
            listRow = false;
       } else {
            ...
            view.animate().alpha(1f);
            listRow = true;
       }
    }
    

    So no matter what happens, every odd time u click an item, alpha will be 0.2f, and every even time it will be 1f.

    Is that what you want, or are you looking to change items to 0.2f only if they're read? If so, you're missing some logic.




    EDIT: Also, wrap your code in
    tags.
    
    For example
    [code]
    public class Books extends AppCompatActivity {
    
        boolean listRow = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_books);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            ListView booksListView = (ListView) findViewById(R.id.booksListView);
    
            final ArrayList<String> topBooks = new ArrayList<String>(Arrays.asList("1984", "To Kill a Mockingbird", "Pride and Prejudice",
                    "Harry Potter and the Sorcerer's Stone", "The Great Gatsby", "Jane Eyre", "Wuthering Heights", "The Catcher in the Rye",
                    "The Hobbit", "Brave New World", "The Lord of the Rings: The Fellowship of the Ring", "Don Quixote",
                    "Catch-22", "The Count of Monte Cristo", "Harry Potter and the Goblet of Fire", "The Grapes of Wrath", "The Adventures of Huckleberry Finn",
                    "The Diary of a Young Girl", "Gone with the Wind", "Harry Potter and the Deathly Hallows", "Moby Dick", "Harry Potter and the Half-Blood Prince", "War and Peace",
                    "Animal Farm", "Anna Karenina", "Ulysses", "Lord of the Flies", "The Divine Comedy", "One Hundred Years of Solitude", "Frankenstein", "The Perks of Being a Wallflower",
                    "The Fault in Our Stars", "Good to Great", "Harry Potter and the Chamber of Secrets", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Prisoner of Azkaban",
                    "The Amazing Adventures of Kavalier & Clay", "The Hunger Games", "The Lord of the Rings: The Two Towers", "The Lord of the Rings: The Return of the King", "The Maze Runner",
                    "Looking for Alaska", "Fahrenheit 451", "Hamlet", "Gullivers Travels", "The Canterbury Tales", "Rebecca", "The Brothers Karamazov", "Lover Awakened", "At Grave's End"));
    
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topBooks);
    
            booksListView.setAdapter(arrayAdapter);
    
            final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.pindrop);
    
            booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (listRow) {
                        Toast.makeText(getApplicationContext(), "You Read " + topBooks.get(position), Toast.LENGTH_LONG).show();
                        view.animate().alpha(0.2f);
                        mPlayer.start();
                        listRow = false;
                    } else {
                        view.animate().alpha(1f);
                        mPlayer.start();
                        listRow = true;
                    }
                }
            });
        }
    }
    


  • Closed Accounts Posts: 789 ✭✭✭Fakman87


    Thanks lads, I got it working now. The only problem is that when I exit the activity the alpha in all rows of the listview resets to 1f. Any ideas how I can stop the selections resetting?

    This is my code:
    
    public class Books extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_books);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    ListView booksListView = (ListView)findViewById(R.id.booksListView);
    
    final ArrayList<String> topBooks = new ArrayList<String>(asList("1984", "To Kill a Mockingbird", "Pride and Prejudice",
    "Harry Potter and the Sorcerer's Stone", "The Great Gatsby","Jane Eyre","Wuthering Heights","The Catcher in the Rye",
    "The Hobbit","Brave New World","The Lord of the Rings: The Fellowship of the Ring","Don Quixote",
    "Catch-22","The Count of Monte Cristo","Harry Potter and the Goblet of Fire","The Grapes of Wrath","The Adventures of Huckleberry Finn",
    "The Diary of a Young Girl","Gone with the Wind","Harry Potter and the Deathly Hallows","Moby Dick","Harry Potter and the Half-Blood Prince","War and Peace",
    "Animal Farm","Anna Karenina","Ulysses","Lord of the Flies","The Divine Comedy","One Hundred Years of Solitude","Frankenstein","The Perks of Being a Wallflower",
    "The Fault in Our Stars","Good to Great","Harry Potter and the Chamber of Secrets","Harry Potter and the Order of the Phoenix","Harry Potter and the Prisoner of Azkaban",
    "The Amazing Adventures of Kavalier & Clay","The Hunger Games","The Lord of the Rings: The Two Towers","The Lord of the Rings: The Return of the King", "The Maze Runner",
    "Looking for Alaska","Fahrenheit 451", "Hamlet","Gullivers Travels","The Canterbury Tales","Rebecca","The Brothers Karamazov","Lover Awakened","At Grave's End"));
    
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topBooks);
    
    booksListView.setAdapter(arrayAdapter);
    
    final MediaPlayer mPlayer = MediaPlayer.create(this,R.raw.pindrop);
    
    booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    if(view.alpha==1f) {
    
    Toast.makeText(getApplicationContext(), "You Read " + topBooks.get(position), Toast.LENGTH_LONG).show();
    view.animate().alpha(0.2f);
    mPlayer.start();
    
    }
    else {
    
    view.animate().alpha(1f);
    
    }
    }
    });
    }
    
    }
    
    


  • Registered Users Posts: 7,860 ✭✭✭The_B_Man


    You'll have to just store which ones are selected somewhere, and when you create/resume the activity, change the alpha to what you want.

    I always write a wrapper for SharedPreferences and use that to store any info I need.


  • Advertisement
Advertisement