我有一个带有我自己的适配器的expandiblelistview,在父行中我有3个图像隐形和3个textview。
在活动中,我有一个包含数据的String [] []。 根据数据,我想将图像设置为可见。
我试着去做:
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parentView) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parentView);
} else {
v = convertView;
}
ponIconos();
return v;
}
private void ponIconos() {
ImageView drCandado = (ImageView) findViewById(R.id.icono_candado);
ImageView drOjos = (ImageView) findViewById(R.id.icono_ojo);
ImageView drUrgente = (ImageView) findViewById(R.id.icono_urgente);
for(int i =0; i<ArrayServicios.size();i++){
if (sIconos[i][0] != "null")
drUrgente.setVisibility(View.VISIBLE);
if (sIconos[i][1] != "null")
drCandado.setVisibility(View.VISIBLE);
if (sIconos[i][2] != "null")
drOjos.setVisibility(View.VISIBLE);
}
}
但它崩溃了nullpo .. logcat的:
06-06 08:44:01.066: E/AndroidRuntime(879): FATAL EXCEPTION: main
06-06 08:44:01.066: E/AndroidRuntime(879): java.lang.NullPointerException
06-06 08:44:01.066: E/AndroidRuntime(879): at com.reparalia.movilidad.ListadoServicios$adaptadorLista.ponIconos(ListadoServicios.java:532)
06-06 08:44:01.066: E/AndroidRuntime(879): at com.reparalia.movilidad.ListadoServicios$adaptadorLista.getGroupView(ListadoServicios.java:520)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ListView.makeAndAddView(ListView.java:1772)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ListView.fillDown(ListView.java:672)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ListView.fillFromTop(ListView.java:732)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ListView.layoutChildren(ListView.java:1625)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1617)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.onLayout(LinearLayout.java:1401)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.ScrollView.onLayout(ScrollView.java:1405)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.View.layout(View.java:11278)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.os.Looper.loop(Looper.java:137)
06-06 08:44:01.066: E/AndroidRuntime(879): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 08:44:01.066: E/AndroidRuntime(879): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 08:44:01.066: E/AndroidRuntime(879): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 08:44:01.066: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 08:44:01.066: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 08:44:01.066: E/AndroidRuntime(879): at dalvik.system.NativeStart.main(Native Method)
还有其他方式吗?
解决方案
/************************************************************************/
/********************* Metodo que GENERA LA VISTA DE LOS PADRES ****************/
@Override
public View getGroupView(final int groupPosition,
final boolean isExpanded, final View convertView,
final ViewGroup parent) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parent);
} else **{
v = convertView;
}
llenaPadre(v, padreDesde.get(groupPosition), padreDesde,
padreHasta, groupPosition);
return v;
}
/********************* Metodo que rellena las filas padres ****************/
private void llenaPadre(View v, Map<String, ?> map,
List<? extends Map<String, ?>> datosPadres, int[] padreHasta2,
int groupPosition) {
ImageView drCandado = (ImageView) v
.findViewById(R.id.icono_candado);
ImageView drOjos = (ImageView) v.findViewById(R.id.icono_ojo);
ImageView drUrgente = (ImageView) v
.findViewById(R.id.icono_urgente);
int len = padreHasta2.length - 1;
for (int i = 0; i < len; i++) {
TextView tv = (TextView) v.findViewById(padreHasta2[i]);
if (v != null) {
String texto = (String) map.get(cabecerasPadres[i]);
tv.setText(texto);
if (sIconos[0][groupPosition] != null)
if (drUrgente != null)
drUrgente.setVisibility(View.VISIBLE);
if (sIconos[1][groupPosition] != null)
if (drCandado != null)
drCandado.setVisibility(View.VISIBLE);
if (sIconos[2][groupPosition] != null)
if (drOjos != null)
drOjos.setVisibility(View.VISIBLE);
}
}**
}
答案 0 :(得分:0)
我认为问题是,你在这里缺少视图参考。按Id执行查找视图时,在适配器中,您必须根据已膨胀的视图引用该项目。在你的情况下“convertView”。尝试这样,通过ponIcons()传递convertView objetc并在那里引用你的小部件。
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parentView) {
View v;
if (convertView == null) {
v = newGroupView(isExpanded, parentView);
} else {
v = convertView;
}
ponIconos(convertView);
return v;
}
private void ponIconos(View convertView) {
ImageView drCandado = (ImageView)convertView.findViewById(R.id.icono_candado);
ImageView drOjos = (ImageView)convertView. findViewById(R.id.icono_ojo);
ImageView drUrgente = (ImageView)convertView. findViewById(R.id.icono_urgente);
for(int i =0; i<ArrayServicios.size();i++){
if (sIconos[i][0] != "null")
if(drUrgente!=null)
drUrgente.setVisibility(View.VISIBLE);
if (sIconos[i][1] != "null")
if(drCandado!=null)
drCandado.setVisibility(View.VISIBLE);
if (sIconos[i][2] != "null")
if(drOjos!=null)
drOjos.setVisibility(View.VISIBLE);
}
}