gpt4 book ai didi

android - 单击后如何将 Listview 设置为删除线项目?

转载 作者:行者123 更新时间:2023-11-30 02:13:18 24 4
gpt4 key购买 nike

我在创建时的 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);

}
});

我读到过可能有一种方法可以扩展数组适配器,但我以前从未这样做过。有什么想法吗?

更新:由于我们遇到了一些问题,这里是完整的代码。一切都在 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);

});
}

这是它的行为: enter image description here

最佳答案

更新:

然后您将需要使用自定义适配器。像这样实例化您的 ListView :

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

第一行是获取行的 View (这是一个 TextView ,因为使用了简单的列表项 1)。如果您使用的是自定义 View ,比如带有图像和 TextView 的布局,那么您只需要添加:

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

关于android - 单击后如何将 Listview 设置为删除线项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com