2012-05-01 65 views
0

我的问题是,我的listView重复我的布局的所有项目。 我需要在这个listView的底部有编辑文本和按钮。 但是这里的编辑文本和按钮重复listView的每一行,我不知道为什么。 如果有人可以帮助我 活动:Android:在listView中重复按钮和编辑文本

public class MessActivity extends ListActivity 
{ 
    public Channel chan; 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setListAdapter(Network.getInstance().Messages); 
     Bundle b = getIntent().getExtras(); 
     this.chan = (Channel) b.get("chanSelected"); 
     //add all message of selected chan to the messAdapter 
     for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) { 
      Message mess = this.chan.getListMessage().get(i); 
      Network.getInstance().addMessage(mess); 
     } 
     setContentView(R.layout.mess_list); 
    } 
} 

适配器:

public class MessageAdapter extends ArrayAdapter<Message> 
{ 
    private Context context; 
    private int textViewResourceId; 

    public MessageAdapter(Context context, int textViewResourceId) 
    { 
     super(context, textViewResourceId); 
     this.context = context; 
     this.textViewResourceId = textViewResourceId; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View row = convertView; 
     MessHolder holder = null; 
     //row null 
     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(textViewResourceId, parent, false); 

      holder = new MessHolder(); 
      holder.texte = (TextView)row.findViewById(R.id.listMess); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (MessHolder)row.getTag(); 
     } 

     Message mess = getItem(position); 
     holder.texte.setText(mess.getText()); 

     return row; 
    } 

    static class MessHolder 
    { 
     TextView texte; 
    } 
} 

布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@android:id/list"> 
    </ListView> 

      <TextView 

      android:id="@+id/listMess" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:padding="10dp" 
      android:textSize="16sp" > 

     </TextView> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="bottom" > 


     //edittext 
     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="text" /> 




     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Envoyer" /> 

    </LinearLayout> 

</LinearLayout> 

谢谢您的帮助。

回答

1

使用ListView方法addFooter(View v)

您可以为该视图定义XML布局,它充气:

ListView lv = getListView(); 
LayoutInflater inflater = getLayoutInflater(); 
View footer = (View)inflater.inflate(R.layout.footer, lv, false); 
lv.addFooterView(footer, null, false); 
+0

好吧,我会尝试。但我认为是在footFooterView中的页眉而不是头? – user1364017

+0

当然,对于那个错误感到抱歉。 – skywall

+0

谢谢你的帮助。我试过了,但是页脚并没有出现在列表视图中。 – user1364017

0

首先这个问题的原因是列表视图中重用时滚动其观点,因此,我们应该在我们获取视图逻辑来管理

if(convertview == null) 
{ 
//code here 
}else { 
//code here 
}  

,但如果你不想重复使用,那么你可以使用下列内容:

@Override 
    public int getItemViewType(int position) { 
    return position; 
} 

@Override 
public int getViewTypeCount() { 
    return 500; 
}