我在list上的listview中设置了一个字符串列表:
//set Adapter
lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, chosenArray);
//add to listview
lw.setAdapter(lvAdapter);
我的问题:
我想设置列表,以便在点击某个项目后,它会获得一次删除。但是,我是Android的新手,并且以前没有做过。
我尝试过的尝试失败了:
根据回答,我尝试了这种方法,但它也会跨越列表中不可见的项目。
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的方法,但我以前从未这样做过。有什么想法吗?
更新:由于我们遇到了一些问题,所以这里是整个代码。它都在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);
});
}
这里有它的行为:
答案 0 :(得分:1)
<强>更新强>
然后,您将需要使用自定义适配器。像这样实例化你的列表视图:
adapter = new CustomAdapter(yourArrayHere);
listview.setAdapter(adapter);
然后您将创建自定义适配器,如下所示:
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;
}
}
然后您的新点击将如下所示:
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.getSelectedStrings().add(list[i]);
adapter.notifyDataSetChanged();
}
现在,selectedStrings数组列表包含您选择的所有字符串。
试试这个:
@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,因此是文本视图)。如果您使用的是自定义视图,请说出包含图片和文本视图的布局,那么您只需要添加:
TextView text = (TextView)view.findViewById(R.id.my_text_view);