如何使用扩展列表活动的类中的数组适配器动态添加按钮?

时间:2018-04-05 08:33:51

标签: android

以下是我的代码

    public class TicketAdapter extends ArrayAdapter<Ticket> {
        Context context;
        int layout;

        public TicketAdapter(Context context, int layoutFile, List<Ticket> tickets) {
            super(context, layoutFile, tickets);

            this.context = context;
            this.layout = layoutFile;

        }

        // overide a method which will be called for every ticket in ticket list

        public View getView(int position, View finalView, ViewGroup parent){
            Ticket ticket = getItem(position);
            ticketDataHolder holder;
            LayoutInflater layoutInflater;
            // 0th index - final view will not be ready
            if(finalView == null){

                holder = new ticketDataHolder();
                layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                finalView= layoutInflater.inflate(R.layout.ticket, null );
                holder.ticketName = (TextView)finalView.findViewById(R.id.btnticketType);
                finalView.setTag(holder);
                Log.d("Helllloooo", finalView.getTag()+"");
            }else{
               holder = (ticketDataHolder) finalView.getTag();
            }

            holder.ticketName.setText(ticket.getTicketType());

            return finalView;
        }

        class ticketDataHolder{
            TextView ticketName;
            HorizontalScrollView ticketView;

        }
    }

以下是我的列表活动类

    public class TicketActivity extends ListActivity {

        TicketAdapter ticketAdapter;
        String[] ticketNames ={"Adult", "Weekly","Mothly", "Yearly"};
        List<Ticket> ticketList;

        public void onCreate(Bundle data){
            super.onCreate(data);
            ticketList = new ArrayList<Ticket>();
            for (int i = 0; i< ticketNames.length; i++) {
                Ticket ticket = new Ticket();
                ticket.setTicketType(ticketNames[i]);
                ticketList.add(ticket);
            }
            ticketAdapter = new TicketAdapter(getApplicationContext(), R.layout.ticket, ticketList);
            setListAdapter(ticketAdapter);
        }
    }

我的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_view"
android:layout_width="200sp"
android:layout_height="300sp">

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:id="@+id/row"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btnticketType"
                android:layout_width="wrap_content"
                android:layout_height="100sp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:padding="10dp"
                android:textColor="#fff"
                android:textSize="20sp"
                android:background="@drawable/mybutton"
                android:text="Buttons Buttons Buttons" />

            <Button
                android:id="@+id/btnticketType1"
                android:layout_width="wrap_content"
                android:layout_height="100sp"
                android:layout_gravity="center"
                android:layout_margin="20dp"
                android:padding="10dp"
                android:textColor="#fff"
                android:textSize="20sp"
                android:background="@drawable/mybutton"
                android:text="Weekly PrePaid" />

        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>

我的按钮xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="oval"  >
            <corners android:radius="1dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="oval"  >
            <corners android:radius="1dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <solid android:color="#58857e"/>
        </shape>
    </item>
    <item >
        <shape android:shape="oval"  >
            <corners android:radius="1dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e"/>
        </shape>
    </item>
</selector>

我正在尝试使用arrayAdapter动态填充按钮。一切正常,除了按钮没有出现在水平滚动视图中。我正在使用andriod api 21级。

我的问题是按钮没有水平显示。

非常感谢您的帮助。

由于 拉万

0 个答案:

没有答案