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;
}
}