android列表视图未在4.1.2版本上显示

时间:2013-07-03 19:24:34

标签: android

我在ViewSwitcher中有一个列表视图,在Android 2.2版本中工作正常,但它没有显示在我的设备中,它有android 4.1.2版本。

我也调试它并且没有看到任何错误,并检查我传递给适配器的列表是否为空,并且看到它不是空的。

我有另一个列表视图,它位于正确显示的TabHost中。

此致

编辑:

这是我的代码:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewswitcher"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical"
    android:layout_gravity="center|top"
     >

    <TextView
        android:id="@+id/dateLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/lblChooseaDate" />

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btnChooseDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/startdayexpensebtn" />
</LinearLayout>
<LinearLayout
      android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical"
     >
    <TextView
        android:id="@+id/descLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:text="@string/lbldaydescription" />

    <EditText
        android:id="@+id/dayDesctiptionText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:singleLine="false"
        android:minLines="1"
        android:maxLines="3"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/addnewItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:enabled="false"
        android:text="@string/btnAddnewItem" />

    <LinearLayout
        android:id="@+id/listcontaioner"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scrollbars="vertical"
        >
    <ListView
        android:id="@+id/android:list"
        android:divider="#bababa"
        android:dividerHeight="1dp"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp" >
    </ListView>
</LinearLayout>

    <LinearLayout
        android:id="@+id/btnlinearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:paddingRight="10dp"
        android:paddingLeft="10dp" >

        <Button
            android:id="@+id/cancelbtn"
            android:layout_weight="2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btncancel" />
         <Button
            android:id="@+id/savebtn"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnAdd" />
    </LinearLayout>

</LinearLayout>
</ViewSwitcher>

也为每一行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        <ImageView
            android:id="@+id/expenselisitem_imgaetype"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_marginLeft="5dp"
            android:layout_marginBottom="35dp"
            android:src="@drawable/fail" />

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >



            <TextView
                android:id="@+id/expenselisitem_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/expenselisitem_category"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/expenselisitem_desc"
                android:paddingBottom="6dp"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/expenselisitem_amount"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignBaseline="@+id/expenselisitem_category"
                android:layout_alignBottom="@+id/expenselisitem_category"
                android:layout_alignParentRight="true"
                android:layout_marginRight="14dp"
                android:paddingBottom="6dp"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall" />

        </RelativeLayout>

    </LinearLayout>

这是适配器:

    public class CustomAddExpenseItemListAdapter extends BaseAdapter {

    private ArrayList<ExpenseItem> _list=new ArrayList<ExpenseItem>();


    private final Activity _context;
    private static LayoutInflater inflater=null;

    public CustomAddExpenseItemListAdapter(Activity activity,ArrayList<ExpenseItem> data){


        _list  = data;
        _context=activity;
    }

    public long getItemId(int position) {
        return position;
    }

    public int getCount() {
        return _list.size();
    }

    public Object getItem(int position) {
        return position;
    }

    static class ViewHolder {
        protected TextView _cexpenseDesctiption;
        protected TextView _cAmount;
        protected ImageView _cType;
        protected TextView _cCategory;

    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=null;

        if(convertView==null)
        {
            inflater = _context.getLayoutInflater();
            vi = inflater.inflate(R.layout.expense_item_list_row, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder._cexpenseDesctiption= (TextView) vi.findViewById(R.id.expenselisitem_desc);
            viewHolder._cAmount = (TextView) vi.findViewById(R.id.expenselisitem_amount);
            viewHolder._cType=(ImageView)vi.findViewById(R.id.expenselisitem_imgaetype);
            viewHolder._cCategory=(TextView)vi.findViewById(R.id.expenselisitem_category);

            vi.setTag(viewHolder);
        }
        else
        {
            vi = convertView;
        }

        ViewHolder holder = (ViewHolder) vi.getTag();


        ExpenseItem item;
        item = _list.get(position);




        holder._cexpenseDesctiption.setText(item.get_description());
        holder._cAmount.setText(String.valueOf(item.get_amount()));

        if(item.get_type()==0)
        {
            holder._cCategory.setText(item.get_category().get_title());
            holder._cType.setImageResource(R.drawable.downarrow);
        }
        else
        {
            holder._cCategory.setText("-");
            holder._cType.setImageResource(R.drawable.uparrow);
        }

        return vi;
    }

}

1 个答案:

答案 0 :(得分:1)

通常这可以通过将ListView包装在LineaLayout或RelativeLayout中来解决,但是根据您反驳的问题,解决方案可能会完全不同。

以下是ViewSwitcher的一个工作示例,它在ListView和TextView之间切换。

从以下XML文本中,您可以看到ListView已经包含在RelativeLayout中,同时还包含一个Button,可以在视图(布局)之间切换。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <ViewSwitcher
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id = "@+id/viewswitcher"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

    >
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
    >
        <Button
                android:id="@+id/btn_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="Show Next" />
        <ListView android:id="@+id/listview" android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/btn_next"></ListView>

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

        <TextView
            android:id="@+id/text"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="HEllO WORLD">

            </TextView>

        <Button
                android:id="@+id/btn_previous"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Show Previous" />
     </LinearLayout>

        </ViewSwitcher>

</RelativeLayout>

这是提供魔力的Java代码:

public class MainActivity extends Activity {

    ViewSwitcher mViewSwitcher;

    ListView listview;
    String[] values = new String[] { "Hello1", "Hello2", "Hello3",
        "Hello4", "Hello5", "Hello6", "Hello7", "Hello8",
        "Hello9", "Hello10", "Hello11", "Hello12", "Hello13", "Hello14",
        "Hello15"};

    Button shownext;
    Button showprevious;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewSwitcher = (ViewSwitcher)findViewById(R.id.viewswitcher);
        listview = (ListView) findViewById(R.id.listview);
        shownext = (Button)findViewById(R.id.btn_next);
        showprevious = (Button)findViewById(R.id.btn_previous);

        shownext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewSwitcher.showNext();
            }
        });

        showprevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewSwitcher.showPrevious();
            }
        });

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
            list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(this,
                android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);




    }

    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

已经在Android 4.1.2上成功测试过。

希望有所帮助!