如何在Android中从适配器为布局分配数据

时间:2015-02-28 20:54:02

标签: android android-layout

当我尝试在适配器的布局中显示某些数据时,我遇到了问题。

我有一个Class,我有一些数据和listView,但是当我尝试从ListView适配器更改布局信息时,数据没有显示在屏幕上,我只看到默认布局,但不是真实的数据。

我在我的适配器中有这个:

holderExpanded = new SwipeExpandedHolder();
holderExpanded.number = (TextView) firstLayout.findViewById(R.id.tv_NumberExpanded);
holderExpanded.businessName = (TextView) firstLayout.findViewById(R.id.tv_BusinessNameExpanded);
holderExpanded.number.setText(arrayBusinesses.get(0).getNumber());
holderExpanded.businessName.setText(arrayBusinesses.get(0).getBusinessName());

我班上的电话是:

wipeLayoutListAdapter = new SwipeLayoutListAdapter(this, R.layout.show_multi_ticket_first_item, R.layout.show_multi_ticket_item,  listView, array);

你怎么看,我传递2个布局,因为我有一个带有数据的LinearLayout,第二个是ListView的项目布局。

我调试应用程序,我可以在"表达式"中看到正确的数据,但不能在布局中看到。

编辑:我的适配器

    public class SwipeLayoutListAdapter extends BaseSwipeAdapter implements OnTouchListener, OnItemClickListener
{
    private Context             context;
    private SwipeExpandedHolder holderExpanded;
    private SwipeListViewHolder holderListView;
    private ListView            listView;
    private int                 firstLayout, layout;
    private LayoutInflater      inflater;
    private ArrayList<MultiTicketBusiness> arrayBusinesses;

....

@Override
    public View generateView(int position, ViewGroup parent) {
        View v;
        SwipeLayout swipeLayout;
        if(arrayBusinesses.get(position).isEmpty()){
            v = inflater.inflate(R.layout.show_multi_ticket_empty_item, null);
            //swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
        } else {
            v = inflater.inflate(layout, null);
            swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
            swipeLayout.addSwipeListener(new SimpleSwipeListener() {
                @Override
                public void onOpen(SwipeLayout layout) {
                    YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
                }
            });
        }

return v;
}

@Override
    public void fillValues(final int position, View convertView) {

        if(arrayBusinesses.get(position).isEmpty()){
            convertView = inflater.inflate(R.layout.show_multi_ticket_empty_item, null);
            initializeListViewHolder(convertView, position, 0);

        } else {
            //Hide the first element in the list
            //if(arrayBusinesses.get(position).isHide()){
            if(position == 0){
                hideItem(convertView, true);

            } else {
                View firstView = inflater.inflate(firstLayout, null);
                hideItem(convertView, false);
                initializeListViewHolder(convertView, position, 1);
                initializeExpandedHolder(convertView, firstView);
            }
        }
    }

public void initializeExpandedHolder(View convertView, View firstLayout){

        firstLayout = inflater.inflate(R.layout.show_multi_ticket_first_item, null);

        holderExpanded = new SwipeExpandedHolder();
        holderExpanded.number_       = (TextView)    firstLayout.findViewById(R.id.tv_NumberExpanded);
        holderExpanded.businessName_  = (TextView)   firstLayout.findViewById(R.id.tv_BusinessNameExpanded);
        holderExpanded.queueName_    = (TextView)    firstLayout.findViewById(R.id.tv_QueueNameExpanded);
        holderExpanded.peopleInFront_ = (TextView)   firstLayout.findViewById(R.id.tv_PeopleInFrontExpanded);
        holderExpanded.estimateTime_     = (TextView)    firstLayout.findViewById(R.id.tv_EstimateTimeExpanded);
        holderExpanded.removeTurn    = (ImageButton) firstLayout.findViewById(R.id.ib_TrashExpanded);

        holderExpanded.number_.setText(arrayBusinesses.get(0).getNumber());
        holderExpanded.businessName_.setText(arrayBusinesses.get(0).getBusinessName());
        holderExpanded.queueName_.setText(arrayBusinesses.get(0).getQueueName());
        holderExpanded.peopleInFront_.setText(arrayBusinesses.get(0).getPeopleInFront());
        holderExpanded.estimateTime_.setText(arrayBusinesses.get(0).getEstimateTime());

    }

    public void initializeListViewHolder(View convertView, final int position, int opc){
        holderListView = new SwipeListViewHolder();

        if(opc == 0){   //When the row doesn't have turn assigned yet.
            holderListView.layoutAddTurn = (LinearLayout)convertView.findViewById(R.id.layout_AddTurn);
        } else {        //The row show a Turn
            holderListView.number        = (TextView)    convertView.findViewById(R.id.tv_NumberItem);
            holderListView.businessName  = (TextView)    convertView.findViewById(R.id.tv_BusinessNameItem);
            holderListView.queueName     = (TextView)    convertView.findViewById(R.id.tv_QueueNameItem);
            holderListView.peopleInFront = (TextView)    convertView.findViewById(R.id.tv_PeopleInFrontItem);
            holderListView.estimateTime  = (TextView)    convertView.findViewById(R.id.tv_EstimateTimeItem);
            holderListView.removeTurn    = (ImageButton) convertView.findViewById(R.id.ib_TrashItem);

            holderListView.number.setText(arrayBusinesses.get(position).getNumber());
            holderListView.businessName.setText(arrayBusinesses.get(position).getBusinessName());
            holderListView.queueName.setText(arrayBusinesses.get(position).getQueueName());
            holderListView.peopleInFront.setText(arrayBusinesses.get(position).getPeopleInFront());
            holderListView.estimateTime.setText(arrayBusinesses.get(position).getEstimateTime());

            //Remove turn of the item clicked
            holderListView.removeTurn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ////Open the remove layout when click on the imageButton
                    ((SwipeLayout)(listView.getChildAt(position - listView.getFirstVisiblePosition()))).open(true);
                }
            });
        }
        convertView.setTag(holderListView);
    }

问题是什么?

提前致谢; - )

1 个答案:

答案 0 :(得分:0)

感谢https://stackoverflow.com/a/21146009/3737032

,我得到了解决方案
View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);

然后:

TextView businessName  = (TextView)    rootView.findViewById(R.id.tv_BusinessNameExpanded);
TextView queueName     = (TextView)    rootView.findViewById(R.id.tv_QueueNameExpanded);
TextView number        = (TextView)    rootView.findViewById(R.id.tv_NumberExpanded);
TextView peopleInFront = (TextView)    rootView.findViewById(R.id.tv_PeopleInFrontExpanded);
TextView estimateTime  = (TextView)    rootView.findViewById(R.id.tv_EstimateTimeExpanded);
ImageButton removeTurn = (ImageButton) rootView.findViewById(R.id.ib_TrashExpanded);