【问题标题】:ListView with LinearLayout带有线性布局的 ListView
【发布时间】:2014-03-14 00:43:51
【问题描述】:

我对 android 编程有点陌生,我想以编程方式实现一个 ListView,其中项目是自定义的 LinearLayout。 我尝试过类似的东西

ListView lv = (ListView) findViewById(R.layout.list_view);
LinearLayout ll = (LinearLayout) findViewById(R.id.list_item);
lv.addView(ll);

但它崩溃了。 谁能给我一个应该如何做的例子? 谢谢!

【问题讨论】:

  • 我猜你正在寻找自定义适配器

标签: android android-layout listview android-linearlayout


【解决方案1】:
protected class MyArrayAdapter extends ArrayAdapter<E>
{
    //List of Events
    private ArrayList<E> myList;

    /*
     * Creates the Adapter for the list
     */
    public MyArrayAdapter (Context context,  int textViewResourceId, ArrayList<E> list)
    {
        super(context, textViewResourceId, list);
        myList= list;
    }

    /**
     * inflate view for each row
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {           
        View v = convertView;
        // if the given channel row view is not being updated
        if (v == null) 
        {
            // inflate you linear 
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.channel_list_row, null,false);
        }


        // edit view here, as shown below
        TextView topTextView = (TextView)v.findViewById(R.id.mytextview);
                    ...

        // return the view for the given position
        return v;           
    }
}

在主代码中

 // create the  array adapter
 myArrayAdapter = new MyChannelAdapter(Activity Cntext, Custom row layout, lList);
 // bind the array adapter to the list view
 ListView.setAdapter(myArrayAdapter);

【讨论】:

    【解决方案2】:

    您必须为您的 ListView 实现自定义 AdapterAdapter 就像列表中的一行。 请查看本教程:http://www.vogella.com/tutorials/AndroidListView/article.html 或类似的问题:Custom Adapter for List View

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多