我有一个类(foo)扩展listactivity,它实例化了一个类(fooadap)扩展arrayadapter。在fooadap中,有getview()方法,我在其中填充我的listview。
在foo类中,我可以调用getListView().setDividerHight(0)并使分隔符消失。有办法从fooadap中的getview()访问该方法吗?
爪哇
public class foo extends ListActivity
{
...
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
...
ListView lv = getListView ();
lv.setDividerHeight (0);
fooAdap foo = new fooAdap (this,android.R.layout.simple_list_item_single_choice, mRowData);
...
}
爪哇
public class fooAdap extends ArrayAdapter
{
...
public View getView (int position, View convertView, ViewGroup parent)
{
...
switch (position)
{
case 1: // show divider for these rows in listview
case 2:
break;
case 3: // hide divider for this row in listview
break;
}
...
}
}
最佳答案
您需要将currentActivity对象传递给fooadap,在那里可以很容易地完成。
或者可以通过fooadap构造函数传递活动对象。
Activity currentActivity=foo.this;
fooAdap foo = new fooAdap (this,android.R.layout.simple_list_item_single_choice, mRowData);
foo.currentActivity=currentActivity;
public class fooAdap extends ArrayAdapter
{
Activity currentActivity;
public View getView (int position, View convertView, ViewGroup parent)
{
...
switch (position)
{
case 1: // show divider for these rows in listview
case 2:
break;
case 3: // hide divider for this row in listview
this.currentActivity.getListView().setDividerHeight(0);
break;
}
...
}
}
这对你一定有帮助。
谢谢