Android:ListView单元格似乎绘制不正确

时间:2012-03-21 17:51:56

标签: android android-listview cell

我确信我做的事情很愚蠢,但我真的对此感到困惑......

这些代码大部分来自StackOverflow上的一些很棒的答案....

在下面的代码中 - 我得到了警报字符串顶部显示的故障字符串,但是 单元格定义将警报字符串置于顶部。

我的手机是alarm_list_row

<?xml version="1.0" encoding="utf-8"?>                       
<LinearLayout                                                 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"                       
    android:layout_height="wrap_content"                      
    android:padding="10dp"                                    
    android:orientation="vertical"                            
    >                                                         

    <TextView                                                 
        android:id="@+id/ar_alarm_text"                       
        android:layout_width="wrap_content"                   
        android:layout_height="wrap_content"                  
        android:text="@+strings/alarm_dummy" />               

    <TextView                                                 
        android:id="@+id/ar_trouble_text"                     
        android:layout_width="wrap_content"                   
        android:layout_height="wrap_content"                  
        android:text="@+strings/trouble_dummy" />             

</LinearLayout>  

我正在使用从ArrayAdapter扩展的适配器

public class ComfortListAlarmArrayAdapter extends ArrayAdapter<StringPair> {         
    private final Context context;                                                     
    private final ArrayList<StringPair> items;                                         

    //Constructors                                                                     
    public ComfortListAlarmArrayAdapter(Context context, ArrayList<StringPair> items) {
        super(context, R.layout.comfort_alarm_cell);                                     
        this.context = context;                                                          
        this.items = items;                                                              
    }                                                                                  

    private class AlarmViewHolder {                                                    
        public TextView alarm;                                                           
        public TextView trouble;                                                         
    }                                                                                  

    private AlarmViewHolder alarmHolder;                                               


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

        if(rowView == null){                                                             
            LayoutInflater inflater = (LayoutInflater) context                             
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);                        
            rowView = inflater.inflate(R.layout.alarm_list_row, parent, false);            
            alarmHolder = new AlarmViewHolder();                                           
            alarmHolder.alarm = (TextView) rowView.findViewById(R.id.ar_alarm_text);       
            alarmHolder.trouble = (TextView) rowView.findViewById(R.id.ar_trouble_text);   
            rowView.setTag(alarmHolder);                                                   

            //          LayoutInflater inflater = context.getLayoutInflater();                 
//          rowView = inflater.inflate(R.layout.comfort_list_row, null, true);           
        } else alarmHolder = (AlarmViewHolder) rowView.getTag();                         


        StringPair alarm = items.get(position);                                          
        if (alarm != null) {                                                             
            alarmHolder.alarm.setText(alarm.getAlarm());                                   
            if (alarm.getTrouble().trim().length() == 0) {                                 
                alarmHolder.trouble.setText(" ");                                            
                //alarmHolder.trouble.setVisibility(android.view.View.INVISIBLE);            
            } else {                                                                       
                alarmHolder.trouble.setText(alarm.getTrouble());                             
                //alarmHolder.trouble.setVisibility(android.view.View.VISIBLE);              
            }                                                                              
            Log.v("Alarm View", alarm.getAlarm()+"/"+alarm.getTrouble());                  
        }                                                                                
        rowView.setBackgroundColor(Color.BLACK);                                         
        return rowView;                                                                  
    }                                                                                  


    /* (non-Javadoc)                                                                   
     * @see android.widget.ArrayAdapter#getCount()                                     
     */                                                                                
    @Override                                                                          
    public int getCount() {                                                            
        return items.size();                                                             
        //return super.getCount();                                                       
    }                                                                                  

}                                                                                    

它正在用于AlarmActivity

public class AlarmActivity extends Activity implements MessageThumper{                                                   

    private ComfortListAlarmArrayAdapter alarmListAdapter;                                                                 
    private final int AA_NEW_MESSAGE = 1;                                                                                  

    CommunicationsController getComms()                                                                                    
    {                                                                                                                      
        ComfortApp main = (ComfortApp) getApplicationContext();                                                              
        return main.Comms;                                                                                                   
    }                                                                                                                      

    /** Called when the activity is first created. */                                                                      
    @Override                                                                                                              
    public void onCreate(Bundle savedInstanceState) {                                                                      
        super.onCreate(savedInstanceState);                                                                                  
        setContentView(R.layout.alarms);                                                                                 
        final ListView lv_1  = (ListView) findViewById(R.id.alarm_list);                                                     

        alarmListAdapter = new ComfortListAlarmArrayAdapter(AlarmActivity.this, getComms().am_alarmStuff);                   
        lv_1.setAdapter(alarmListAdapter);                                                                                   
    }                                                                                                                      

    Handler alarmUpdateHandler = new Handler(){                                                                            

            @SuppressWarnings("unchecked")                                                                                     
            @Override                                                                                                          
            public void handleMessage(Message msg) {                                                                           
                if(msg.what == AA_NEW_MESSAGE){                                                                                  
                    //update all adapters                                                                                          
                    ((ComfortListAlarmArrayAdapter)((ListView) findViewById(R.id.alarm_list)).getAdapter()).notifyDataSetChanged();
                }                                                                                                                
                super.handleMessage(msg);                                                                                        
            }                                                                                                                                                                                      
    };                                                                                                                                                                                                                                                 

    public void passMessage(String msg, String p1, String p2) {                                                            
        if (msg.equals("NEW_ALARM"))                                                                                         
        {                                                                                                                    
            Message M = new Message();                                                                                         
            M.what = AA_NEW_MESSAGE;                                                                                           
            this.alarmUpdateHandler.sendMessage(M);                                                                            
        }                                                                                                                    
    }                                                                                                                      
}      

如果我的logCat显示

03-22 01:25:42.112: I/CommsController:setAlarm(26655): System Armed,Code 240

the cell shows
    ------------
    Code 240
    System Armed
    ------------

有人可以指出我在忽视的东西吗?

我很抱歉这个额外的复杂性 - 来自通信线程等的消息留在了,但是他的日志消息应该显示字符串在正确的位置....

1 个答案:

答案 0 :(得分:0)

我猜这个问题是LogCat显示System Armed然后代码显示代码然后显示System Armed的事实。如果这是问题,那么您需要查看enter code here alarm_list_row布局,并确保已恰当地命名TextView