Android ListView:Null时不显示TextView

时间:2011-07-11 03:03:19

标签: java android listview textview

我有一个ListView,它显示一个由用户在活动中动态生成的数组。基本上,该程序是一个计时器,用户可以选择他们希望闹钟响起的时间,并提供他们在闹钟响起时应该做什么的描述。

我使用SimpleAdapter进行ListView设置,该SimpleAdapter调用alarm_list_item.xml,其中包含两个TextView和一个我希望为列表中的每个项目显示的图标。

adapter = new AlarmListAdapter(this, stringArray, R.layout.alarm_list_item, new String[] {"name", "alert"}, new int[] {R.id.ListViewItem, R.id.ListViewItemSub});

当用户选择不为警报添加自定义对话框时,我想将alarm_list_item.xml中TextView的可见性设置为GONE,因此它不会在列表中显示为空白区域。当我尝试从活动中设置此参数时,我得到一个NullPointerException,因为我假设xml文件实际上不是一个对象,但或多或​​少只是用作ListView的引用。有没有办法实现这个目标?

到目前为止,我通过将文本设置为“无自定义通知集”来解决问题,但我想要一个更干净的选项,只需在不使用时将所有行一起删除。

alarm_list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:src="@drawable/alarm_icon"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp">
</ImageView>

<LinearLayout
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <TextView
        android:id="@+id/ListViewItem"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textSize="16sp">
    </TextView>
    <TextView
        android:id="@+id/ListViewItemSub"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textSize="10sp">
    </TextView>
</LinearLayout>

ListView所在活动的片段:

        stringArray = new ArrayList<HashMap<String, String>>();

    list = (ListView)findViewById(R.id.AlarmListView);
    adapter = new AlarmListAdapter(this, stringArray, R.layout.alarm_list_item, new String[] {"name", "alert"}, new int[] {R.id.ListViewItem, R.id.ListViewItemSub});
    list.setAdapter(adapter);
    registerForContextMenu(list);

}

private void updateListArray(){
    stringArray.clear();
    if(!arrayList.isEmpty()){
    for(int i=0; i<arrayList.size(); i++){

        String name = arrayList.get(i).getName();
        String alert = arrayList.get(i).getAlertDialog();

        if(alert == ""){
            alert = "No Custom Notification Set";
        }

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", name);
        map.put("alert", alert);
        stringArray.add(map);
    }
    }
}

适配器代码:

    public AlarmListAdapter(Context context, ArrayList<HashMap<String, String>> list, int textViewResourceId, String[] fields, int[] textViewId) {
    super(context, list, textViewResourceId, fields, textViewId);
    this.list = list;
    this.context = context;

}

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

View v = convertView;
if (v == null) {
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.alarm_list_item, null);
    Log.i("getView = ", "" + v);
    Log.i("list alert = ", "" + list.get(position).get("alert"));
}


if(list.get(position).get("alert") == null){    
    v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
}

return super.getView(position, convertView, parent);

}

public void forceReload(){
    notifyDataSetChanged();
}

LogCat错误:

07-11 12:16:23.543: ERROR/AndroidRuntime(190): Uncaught handler: thread main exiting due to uncaught exception
07-11 12:16:23.573: ERROR/AndroidRuntime(190): java.lang.NullPointerException
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.AbsListView.obtainView(AbsListView.java:1275)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.ListView.onMeasure(ListView.java:1060)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.View.measure(View.java:7703)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.os.Looper.loop(Looper.java:123)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at android.app.ActivityThread.main(ActivityThread.java:4203)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at java.lang.reflect.Method.invoke(Method.java:521)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-11 12:16:23.573: ERROR/AndroidRuntime(190):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

想出来。

SimpleAdapter getView覆盖代码:

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

View v = convertView;
if (v == null) {
    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.alarm_list_item, null);
}


if(list.get(position).get("alert") == null){    
    v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
}

return super.getView(position, convertView, parent);

}

问题在于return语句。我需要返回超值。

答案 1 :(得分:0)

您需要超越适配器的bindViewgetView方法:在那里,您将能够设置所需视图的可见性。 This是一个格式不正确(但技术上正确)的例子。

编辑:尝试这样的事情:

@Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
View v = super.getView(position, convertView, parent);
                        if(list.get(position).get("alert") == ""){      
        v.findViewById(R.id.ListViewItem).setVisibility(View.GONE);
        v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
    }
return convertView;

}

答案 2 :(得分:0)

尝试这样的事情:

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

View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.alarm_list_item, null);
}


if(list.get(position).get("alert") == ""){    
    v.findViewById(R.id.ListViewItemSub).setVisibility(View.GONE);
}

return convertView;

}