-3

I'm really struggling to highlight items in my listview - I want them to be highlighted onClick and un-highlighted when they are clicked again. Can someone please help me achieve this for my code as I have looked at numerous bits of code online and can't get it to work - Thanks in advance!! Here is my code

    public class ConversationView extends Activity {
    TextView hello;

    Integer threadId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_view);

        Intent intent = getIntent();
        String thread = intent.getStringExtra("threadId");

        threadId = Integer.parseInt(thread);

        final ListView convoListView = (ListView) this.findViewById(R.id.conversationListView);

        ArrayList<ConversationItem> convoItems = this.GetItems(threadId);

        final ConversionAdapter convoAdapter = new ConversionAdapter(getApplicationContext(), convoItems);

        convoListView.setAdapter(convoAdapter);

 convoListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        convoListView.setItemChecked(1, true);
   }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_conversation_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public ArrayList<ConversationItem> GetItems(Integer threadId)
    {
        ArrayList<ConversationItem> convos = new ArrayList<>();
        convos.add(new ConversationItem(1, 1, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));
        convos.add(new ConversationItem(1, 2, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet,Donec lacinia nunc sed faucibus suscipit. Curabitur."));
        convos.add(new ConversationItem(1, 3, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, Curabitur."));
        convos.add(new ConversationItem(1, 4, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));

        ArrayList<ConversationItem> returnItems = new ArrayList<>();

        for(int i = 0; i < convos.size(); i++)
        {
            if(convos.get(i).ThreadId.equals(threadId))
            {
                returnItems.add(convos.get(i));
            }
        }

        return returnItems;
    }

//this is my adapter


    public class ConversionAdapter extends ArrayAdapter<ConversationItem>
    {

        private final Context context;
        private final ArrayList<ConversationItem> items;
        private int currentPage = 0;

        public ConversionAdapter(Context context, ArrayList<ConversationItem> convoItems) {
            super(context, 0, convoItems);
            this.context = context;
            this.items = convoItems;
        }



}
Sim
  • 231
  • 1
  • 3
  • 11
  • check this question and answer, it also same problem like you http://stackoverflow.com/questions/4644829/checkboxes-in-android-listview-having-problem – balaji koduri Feb 16 '15 at 12:09
  • try to use CheckedLinearLayout extends LinearLayout – KOTIOS Feb 16 '15 at 12:11
  • @balajikoduri I don't want to use checkboxes I want to highlight the whole row... – Sim Feb 16 '15 at 12:17
  • instead of checkbox you have to maintain one toggle variable here, – balaji koduri Feb 16 '15 at 12:22
  • 1
    your requirement also same here he maintain checkbox, but you are maintaining item background. – balaji koduri Feb 16 '15 at 12:23
  • @Apurva I'm getting an error saying ' Element Item must be declared' – Sim Feb 16 '15 at 12:30
  • use one property isChecked on ConversationItem and set it true or false on list view item click also change the clicked item background – Mourice Feb 16 '15 at 12:34
  • Thank you @Apurva - if you put it as an answer I'll mark it correct :) – Sim Feb 16 '15 at 12:35
  • @Apurva it worked great - if I wanted to delete all selected items how would I do this? – Sim Feb 16 '15 at 12:42
  • if you put it as an answer on this post i can mark it as correct so you have more reputations – Sim Feb 16 '15 at 12:42
  • Follow this tutorial http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/ And truthfully, I'm at work can't write an entire answer right now – Apurva Feb 16 '15 at 12:45

1 Answers1

-1

I'm just doing the same thing. This works for me.

I don't set the @drawable in listview, I have set it in format of listview i.e. in layout where you have put textViews and checkBoxes to be displayed in listview.

list_view_format.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_view_item">

</RelativeLayout>

/res/drawable/list_view_item.xml

<item android:state_pressed="true" >
    <shape>
        <solid android:color="@color/list_item_pressed"/>
    </shape>
</item>

<item android:state_activated="true" >
    <shape>
        <solid android:color="@color/list_item_activated"/>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/list_item_normal" />
    </shape>
</item>

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="list_item_normal">#96ffdcb5</color>
    <color name="list_item_activated">#ffaa66</color>
    <color name="list_item_pressed">#ffaa66</color>
</resources>
Apurva
  • 7,871
  • 7
  • 40
  • 59