在 ExpandableListView android 的 child 视图中编辑 TextView
edit TextView in child view in ExpandableListView android
在同一 child 视图中单击按钮后更改我的文本视图 txtChild.Text = "some Words...";
时,我有可扩展列表视图 android,最后 child 仅编辑所有 parent !!
我希望单击的 child 位置仅更改而不是最后一个 child?
怎么做到的?
这是我的 ExpandableListAdapter class
`
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;
}
}`
这是我的 activity
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();
};
}
}
i have Expandable List View android when change my Text view txtChild.Text = "some Words..."; after clicking button in the same child view ,, last child only editing in all parent !!
这是因为您将 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;
}
在同一 child 视图中单击按钮后更改我的文本视图 txtChild.Text = "some Words...";
时,我有可扩展列表视图 android,最后 child 仅编辑所有 parent !!
我希望单击的 child 位置仅更改而不是最后一个 child?
怎么做到的?
这是我的 ExpandableListAdapter class
` 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;
}
}`
这是我的 activity
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();
};
}
}
i have Expandable List View android when change my Text view txtChild.Text = "some Words..."; after clicking button in the same child view ,, last child only editing in all parent !!
这是因为您将 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;
}