在listview中动态添加项目

时间:2013-11-19 12:24:58

标签: android listview

我有一个线性布局,包括列表视图和列表视图下方的编辑文本。然后,我将它们全部放在滚动视图中。我想动态地为listview添加项目。如果只有listview,一切都很好。但是,当我在listview下面添加编辑文本时,虽然我使用了adapter.notifyDataSetChanged()函数,但listview的高度不会改变。我想当我将项目放在列表视图中时,编辑文本也会被按下。我能解决什么问题?感谢

我的布局很简单,就像这样

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"/>

 <ScrollView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

   <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
   </ListView>

   <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="example"> 

   </EditText>
 </ScrollView>
</LinearLayout>

更新我的细节设计 enter image description here

5 个答案:

答案 0 :(得分:0)

您是否尝试过使用相对布局?然后你可以在xml中添加以下编辑文本:android:layout_below =“@ + id / your_listview_id”

修改

适配器

public class ListAdapter extends BaseAdapter{

    List<String> itemList = new ArrayList<String>();
    Context context;

    void addItem(String item){
        itemList.add(item);
    }

    public ListAdapter(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View list;
        LayoutInflater inflater = getLayoutInflater();
        list = inflater.inflate(R.layout.list_item, null);
        TextView text = (TextView) list.findViewById(R.id.text_view);
        text.setText(itemList.get(position));

        if (itemList.get(position).toString().trim().equals("edit")){
            // Add a Edit text dynamically
            text.setText("Edit text here");
        }

        return list;
    }

}

onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uga);
    ListView list = (ListView) findViewById(R.id.list_view);
    List<String> list_content = new ArrayList<String>();
    listAdapter = new ListAdapter(getApplicationContext());
    list_content.add("1");
    list_content.add("2");
    list_content.add("3");
    list_content.add("4");
    list_content.add("5");
    list_content.add("6");
    list_content.add("7");
    list_content.add("8");
    list_content.add("9");
    list_content.add("10");
    list_content.add("edit");

    list.setAdapter(listAdapter);

    for (String text : list_content){
        listAdapter.addItem(text);
    }

}

答案 1 :(得分:0)

1)不要将listview放入scrollview,listview是可滚动的。

2)如果你想在listview下面修改你的编辑文本并且一直在屏幕上,你可以在复合linearlayout和relativelayout时得到它,像这样:

<LinearLayout>
   <RelativeLayout weight='1' height='wrap_content'>
       <YouListView />
   </RelativeLayout>
   <EditText />
</LineadLayout>

在这种情况下,RelativeLayout将填充除edittext区域之外的所有可能区域。

3)如果你想让这个edittext成为listview的一部分,请改用listview中的页脚。

4)您可以为列表中的每一行使用不同的布局,但在这种情况下,您必须更准确地管理此视图以避免性能问题。

答案 2 :(得分:0)

试试这个

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"/>

   <ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
   </ListView>

   <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="example"> 

   </EditText>
</LinearLayout>

您在代码中犯了错误

1)在Scrollview中包含Listview是非常糟糕的做法。

2)当您处理LinearLayout时,wight属性会播放keyrole。而你还没有使用那个属性

如果此解决方案不起作用,请提供您想要设计的布局的虚拟屏幕截图。我会帮助你的。

答案 3 :(得分:0)

经过一番努力,我通过在没有测量列表高度的情况下将页眉和页脚添加到列表视图来解决了我的问题。 得到这个项目让我很困惑但不知何故,我明白了。

ListView listView = (ListView) getActivity().findViewById(
                    android.R.id.list);
            headerView = View.inflate(getActivity(), R.layout.fragment_dish_step_header, null);
            listView.addHeaderView(headerView);
            TextView mDishName = (TextView) headerView.findViewById(
                    R.id.tv_dish_name_in_step);
            mDishName.setText(mGlobal.getDishName());
            footerView = View.inflate(getActivity(), R.layout.fragment_dish_step_footer, null);
            listView.addFooterView(footerView);

答案 4 :(得分:-2)

我终于通过计算onDataSetChanged函数中listview的大小来解决我的问题

ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
    listView.setLayoutParams(params);