C# listview 控件中,以列表呈现,如何获取选中的某一项的名称

C# listview 控件中,以列表呈现,如何获取选中的某一项的名称,第1张

ListView listView1 = new ListView();

listView1Bounds = new Rectangle(new Point(10,10), new Size(300,200));

// Set the view to show details

listView1View = ViewDetails;

// Allow the user to edit item text

listView1LabelEdit = true;

// Allow the user to rearrange columns

listView1AllowColumnReorder = true;

// Display check boxes

listView1CheckBoxes = true;

// Select the item and subitems when selection is made

listView1FullRowSelect = true;

// Display grid lines

listView1GridLines = true;

// Sort the items in the list in ascending order

listView1Sorting = SortOrderAscending;

// Create three items and three sets of subitems for each item

ListViewItem item1 = new ListViewItem("item1",0);

// Place a check mark next to the item

item1Checked = true;

item1SubItemsAdd("1");

item1SubItemsAdd("2");

item1SubItemsAdd("3");

ListViewItem item2 = new ListViewItem("item2",1);

item2SubItemsAdd("4");

item2SubItemsAdd("5");

item2SubItemsAdd("6");

ListViewItem item3 = new ListViewItem("item3",0);

// Place a check mark next to the item

item3Checked = true;

item3SubItemsAdd("7");

item3SubItemsAdd("8");

item3SubItemsAdd("9");

// Create columns for the items and subitems

listView1ColumnsAdd("Item Column", -2, HorizontalAlignmentLeft);

listView1ColumnsAdd("Column 2", -2, HorizontalAlignmentLeft);

listView1ColumnsAdd("Column 3", -2, HorizontalAlignmentLeft);

listView1ColumnsAdd("Column 4", -2, HorizontalAlignmentCenter);

您好:

package comexmythandroid;

public class ListEditorAdapter extends BaseAdapter {

private LayoutInflater mInflater;

private List<Map<String, Object>> mData;// 存储的EditText值

public Map<String, String> editorValue = new HashMap<String, String>();//

public ListEditorAdapter(Context context, List<Map<String, Object>> data) {

mData = data;

mInflater = LayoutInflaterfrom(context);

init();

}

// 初始化

private void init() {

editorValueclear();

}

@Override

public int getCount() {

return mDatasize();

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

private Integer index = -1;

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

// convertView为null的时候初始化convertView。

if (convertView == null) {

holder = new ViewHolder();

convertView = mInflaterinflate(Rlayoutlistview_item, null);

holdername = (TextView) convertView

findViewById(Ridlist_item_name);

holdertitle = (TextView) convertView

findViewById(Ridlist_item_title);

holdervalue = (EditText) convertView

findViewById(Ridlist_item_inputvalue);

holdervaluesetTag(position);

holderuserkey = (TextView) convertViewfindViewById(Riduser_key);

holdervaluesetOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (eventgetAction() == MotionEventACTION_UP) {

index = (Integer) vgetTag();

}

return false;

}

});

class MyTextWatcher implements TextWatcher {

public MyTextWatcher(ViewHolder holder) {

mHolder = holder;

}

private ViewHolder mHolder;

@Override

public void onTextChanged(CharSequence s, int start,

int before, int count) {

}

@Override

public void beforeTextChanged(CharSequence s, int start,

int count, int after) {

}

@Override

public void afterTextChanged(Editable s) {

if (s != null && !""equals(stoString())) {

int position = (Integer) mHoldervaluegetTag();

mDataget(position)put("list_item_inputvalue",

stoString());// 当EditText数据发生改变的时候存到data变量中

}

}

}

holdervalueaddTextChangedListener(new MyTextWatcher(holder));

convertViewsetTag(holder);

} else {

holder = (ViewHolder) convertViewgetTag();

holdervaluesetTag(position);

}

Object value = mDataget(position)get("list_item_name");

if (value != null) {

holdernamesetText((String) value);

}

value = mDataget(position)get("list_item_title");

if (value != null) {

holdertitlesetText(valuetoString());

}

value = mDataget(position)get("user_key");

if (value != null) {

holderuserkeysetText(valuetoString());

} else {

holderuserkeysetText("-1");

}

value = mDataget(position)get("list_item_inputvalue");

if (value != null && !""equals(value)) {

holdervaluesetText(valuetoString());

} else {

String key = mDataget(position)get("user_key")toString();

String inputValue = editorValueget(key);

holdervaluesetText(inputValue);

}

holdervalueclearFocus();

if (index != -1 && index == position) {

holdervaluerequestFocus();

}

return convertView;

}

public final class ViewHolder {

public TextView name;

public TextView title;

public EditText value;// ListView中的输入

public TextView userkey;// 用来定义的标志性主键,可不用关心

}

}

使用ListView的MouseDoubleClick事件,当你选中一行时,会得到ListViewSelectedItem,这个SelectedItem就是ListView数据源集合的一项,这样你就可以想显示什么就显示什么。

比如:ListView的DataContext绑定的数据源是List<Person>,Person类有三个属性(Name,Age,Sex),那么ListViewSelectedItem的值就是Person的一个对象,你想获得Text数据(假设是Name),只要使用var person = ListViewSelectedItem as Person;personName即可

在ListVIew下面加一个lable 然后剖析ImageList中路径的字符串取得文件名,再将取得的文件名用字符串函数 *** 作,替换或者删除文件名中的数字,将经过剖析和加工后的文件名赋给lable的text属性

以上就是关于C# listview 控件中,以列表呈现,如何获取选中的某一项的名称全部的内容,包括:C# listview 控件中,以列表呈现,如何获取选中的某一项的名称、android开发中怎么获取listview中多个edittext的值、wpf中listview如何通过双击方式获取某一行的信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9461455.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存