How to setup Listview to strikethrough item after

2020-07-30 00:54发布

问题:

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:

回答1:

UPDATE:

You will want to use a custom adapter then. Instantiate your list view like this:

adapter = new CustomAdapter(yourArrayHere);
    listview.setAdapter(adapter);

Then you will create your custom adapter like so:

private class CustomAdapter extends BaseAdapter {

    private String[] strings;
    private ArrayList<String> selectedStrings;

    public CustomAdapter(String[] strings) {
        this.strings = strings;
        selectedStrings = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return strings.length;
    }

    @Override
    public String getItem(int i) {
        return strings[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
            holder.text = (TextView) view;
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.text.setText(getItem(i));
        if (selectedStrings.contains(getItem(i))) {
            holder.text.setPaintFlags(holder.text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            holder.text.setPaintFlags(0);
        }
        return view;
    }

    private class ViewHolder {
        TextView text;
    }

    public ArrayList<String> getSelectedStrings() {
        return selectedStrings;
    }
}

Then your new click will look like:

@Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            adapter.getSelectedStrings().add(list[i]);
            adapter.notifyDataSetChanged();
        }

Now the selectedStrings array list contains all of the strings you have selected.


Try this:

@Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            TextView text = (TextView) view;
            text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }

The first line is getting the row's view (which is a text view, due to using simple list item 1). If you were using a custom view, say a layout with images and a text view, then you would just need to add:

TextView text = (TextView)view.findViewById(R.id.my_text_view);