单击后如何将Listview设置为删除线项目? [英] How to setup Listview to strikethrough item after click?
问题描述
我有一个在创建时在listview中设置的字符串的列表:
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);
我的问题:
我想设置列表,以便在单击某个项目后就可以删除它.但是,我是android的新手,之前没有做过.
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.
我尝试失败的方法:
根据答案,我尝试了这种方法,但是它会划掉列表中不可见的项目.
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);
}
});
我已经读过,也许有一种扩展arrayadapter的方法,但是我从来没有做过.有什么想法吗?
I've read that there is maybe a way to extend the arrayadapter, but I have never done that before. Any ideas?
更新:由于我们遇到了一些问题,因此这里是整个代码.全部都在onCreate中:
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);
});
}
这是它的行为:
推荐答案
更新:
您将要使用自定义适配器.实例化您的列表视图,如下所示:
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();
}
现在,selectedStrings数组列表包含您选择的所有字符串.
Now the selectedStrings array list contains all of the strings you have selected.
尝试一下:
@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);
}
第一行获取行的视图(由于使用简单的列表项1,因此是文本视图).如果您使用的是自定义视图,请说一个包含图片和文本视图的布局,那么您只需添加:
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);
这篇关于单击后如何将Listview设置为删除线项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!