I've got a ListView that displays an array that is generated dynamically by the user from within the activity. Basically, the program is a timer and the user can choose a time they would like an alarm to go off and also provide a description of what they should be doing when that alarm goes off.
I have the ListView setup using a SimpleAdapter which calls a alarm_list_item.xml, which contains two TextView's and an icon that I want displayed for each item in the list.
adapter = new AlarmListAdapter(this, stringArray, R.layout.alarm_list_item, new String[] {"name", "alert"}, new int[] {R.id.ListViewItem, R.id.ListViewItemSub});
When the user chooses not to add a custom dialog for the alarm I would like to set the visibility of the TextView in alarm_list_item.xml to GONE so it doesn't show as a blank space on the list. When I try to set this parameter from within the activity I get a NullPointerException because I am assuming that the xml file isn't actually an object but more or less just used as a reference for the ListView. Is there a way to accomplish this?
So far I am getting around the problem by setting the text to "No Custom Notification Set" but i'd like a cleaner option and just remove the line all together when its not in use.
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>
Snippet of the activity where the ListView resides:
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);
}
}
}
Adapter Code:
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 Error:
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 1开发者_如何学Go2: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)
Figured it out.
SimpleAdapter getView Override Code:
@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);
}
The problem was with the return statement. I needed to return the super values.
You will need to over ride the Adapter's bindView or getView methods: in there you'll be able to set the visibility on the desired View. This is a poorly-formatted (but technically correct) example.
EDIT: try something like 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;
}
Try something like this:
@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;
}
精彩评论