我有可扩展列表查看android时,在同一子视图中单击按钮后更改我的文本视图txtChild.Text = "some Words...";
,最后一个孩子只在所有父级编辑!
我想点击孩子的位置只改变不是最后一个孩子?
怎么做?
这是我的ExpandableListAdapter类
` class ExpandableListAdapter:BaseExpandableListAdapter { TextView txtListChild;
private Activity _context;
private List<string> _listDataHeader; // header titles
// child data in format of header title, child title
private Dictionary<string, List<string>> _listDataChild;
public ExpandableListAdapter(Activity activity, List<string> listDataHeader, Dictionary<String, List<string>> listChildData)
{
this._context = activity;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
return _listDataChild[_listDataHeader[groupPosition]][childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
string childText = (string)GetChild(groupPosition, childPosition);
if (convertView == null)
{
convertView = _context.LayoutInflater.Inflate(Resource.Layout.ListItemCustomLayout, null);
txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
Button CollectAll = (Button)convertView.FindViewById(Resource.Id.button1);
CollectAll.Click += delegate
{
txtListChild.Text = "some words ..";
// i want child position clicked only text changed
};
}
txtListChild.Text = childText;
return convertView;
}
public override int GetChildrenCount(int groupPosition)
{
return _listDataChild[_listDataHeader[groupPosition]].Count;
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return _listDataHeader[groupPosition];
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
string headerTitle = (string)GetGroup(groupPosition);
convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.HeaderCustomLayout, null);
var lblListHeader = (TextView)convertView.FindViewById(Resource.Id.lListHeader);
lblListHeader.Text = headerTitle;
return convertView;
}
public override int GroupCount
{
get
{
return _listDataHeader.Count;
}
}
public override bool HasStableIds
{
get
{
return false;
}
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}`
这是我的活动
public class layout1 : Activity
{
Sqlitedb db = new Sqlitedb();
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<string> listDataHeader;
Dictionary<string, List<string>> listDataChild;
int previousGroup = -1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
// Create your application here
expListView = FindViewById<ExpandableListView>(Resource.Id.vExp);
//// Prepare list data
prepareListData();
//Bind list
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.SetAdapter(listAdapter);
FnClickEvents();
}
void prepareListData()
{
listDataHeader = new List<String>();
listDataChild = new Dictionary<String, List<String>>();
// Adding child data
listDataHeader.Add("Top 250");
listDataHeader.Add("Now Showing");
listDataHeader.Add("Coming Soon..");
// Adding child data
List<String> top250 = new List<String>();
top250.Add("The Shawshank Redemption");
top250.Add("The Godfather");
top250.Add("The Godfather: Part II");
top250.Add("Pulp Fiction");
top250.Add("The Good, the Bad and the Ugly");
top250.Add("The Dark Knight");
top250.Add("12 Angry Men");
List<String> nowShowing = new List<String>();
nowShowing.Add("The Conjuring");
nowShowing.Add("Despicable Me 2");
nowShowing.Add("Turbo");
nowShowing.Add("Grown Ups 2");
nowShowing.Add("Red 2");
nowShowing.Add("The Wolverine");
List<String> comingSoon = new List<String>();
comingSoon.Add("2 Guns");
comingSoon.Add("The Smurfs 2");
comingSoon.Add("The Spectacular Now");
comingSoon.Add("The Canyons");
comingSoon.Add("Europa Report");
// Header, Child data
listDataChild.Add(listDataHeader[0], top250);
listDataChild.Add(listDataHeader[1], nowShowing);
listDataChild.Add(listDataHeader[2], comingSoon);
//RunOnUiThread(() =>
//{
// listDataChild.Remove("Top 250");
// listAdapter.NotifyDataSetChanged();
//});
}
void FnClickEvents()
{
//Listening to child item selection
expListView.ChildClick += delegate(object sender, ExpandableListView.ChildClickEventArgs e)
{
// listDataChild.Visibility = Android.Views.ViewStates.Gone;
Toast.MakeText(this, "child", ToastLength.Short).Show();
};
//Listening to group expand
//modified so that on selection of one group other opened group has been closed
expListView.GroupExpand += delegate(object sender, ExpandableListView.GroupExpandEventArgs e)
{
if (e.GroupPosition != previousGroup)
expListView.CollapseGroup(previousGroup);
previousGroup = e.GroupPosition;
};
//Listening to group collapse
expListView.GroupCollapse += delegate(object sender, ExpandableListView.GroupCollapseEventArgs e)
{
Toast.MakeText(this, "group", ToastLength.Short).Show();
};
}
}
答案 0 :(得分:0)
我有可扩展列表查看android时更改我的文本视图txtChild.Text =&#34;有些单词...&#34 ;;在同一个子视图中点击按钮后,最后一个孩子只在所有父母中编辑!!
这是因为您在全球范围内存储了txtChild
,如下所示:
class ExpandableListAdapter : BaseExpandableListAdapter {
TextView txtListChild;
...
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
...
txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
...
}
txtListChild.Text = childText;
return convertView;
}
因此,当子视图得到渲染时。 GetChildView
多次被调用,txtListChild
指向最后一个子视图。
要解决此问题,您只需将声明TextView txtListChild
移至方法GetChildView
内,如下所示:
class ExpandableListAdapter : BaseExpandableListAdapter {
...
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
TextView txtListChild;
...
txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
...
txtListChild.Text = childText;
}
return convertView;
}