0

我的问题是我的 listView 重复了我布局的所有项目。我需要在这个 listView 的底部有编辑文本和按钮。但是这里对于 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>

谢谢您的帮助。

4

2 回答 2

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);
于 2012-05-01T10:36:29.360 回答
0

这个问题的第一个原因是 Listview 在滚动时重用它的视图,所以我们应该在我们的 get 视图中有一个逻辑来管理它

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

,但如果您不想重复使用,则可以使用以下内容:

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

@Override
public int getViewTypeCount() {
    return 500;
}
于 2014-04-16T08:54:37.217 回答