I have a list of strings that I setup on the listview in on create:
//set Adapter
lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, chosenArray);
//add to listview
lw.setAdapter(lvAdapter);
My Question:
I want to setup the list so that once an item is clicked, it gets a strike through. However, I am new to android and haven't done that before.
What I've tried unsuccessfully:
Based on answer I tried this method, however it would cross off items that were not visible in the list as well.
lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//cross off
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
});
I've read that there is maybe a way to extend the arrayadapter, but I have never done that before. Any ideas?
UPDATE: Since we're running into some issues, here's the whole code. It's all within the onCreate:
public ArrayAdapter<String> lvAdapter;
public ArrayList<String> arrPlayers = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pool_sheet);
//Setup List view
ListView lw = (ListView) findViewById(R.id.lvBouts);
//adapter to use on ListView
String[] chosenArray = {""};
//convert
if (extras != null) {
arrPlayers = extras.getStringArrayList("arrPlayers");
}
//Based on array input size from main activity
//select pre defined string arrays
switch (arrPlayers.size()) {
case 6:
chosenArray = poolOfSix;
break;
case 7:
chosenArray = poolOfSeven;
break;
case 8:
chosenArray = poolOfEight;
break;
case 9:
chosenArray = poolOfNine;
break;
case 10:
chosenArray = poolOfTen;
break;
//no need for default since multiple choice
}
//set Adapter
lvAdapter = new ArrayAdapter<String>this,Android.R.layout.simple_list_item_1, chosenArray);
lw.setAdapter(lvAdapter);
//List view onClick listener
lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//cross off
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
});
}
Here's the behavior it has: