0

我想在不同的行上用不同的 XML 模板填充一个列表视图。我失去了一个教程,现在我被卡住了。我得到所有行的相同模板。所以我所做的是,我创建了一个适配器和一个包装器:

列表视图适配器:

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ListViewAdapter extends ArrayAdapter<ListItem> {



    private Activity activity = null;

    private List<ListItem> items = null;

    private int templateId = 0;

    public static final int TPL_TITLE = 1;

    public static final int TPL_PAYMENT = 2;

    public static final int TPL_NOTE = 3;



    public int getTemplateId() {

        return templateId;

    }



    public Activity getActivity() {

        return activity;

    }



    public void setActivity(Activity activity) {

        this.activity = activity;

    }



    public List<ListItem> getItems() {

        return items;

    }



    public void setItems(List<ListItem> items) {

        this.items = items;

    }



    public ListViewAdapter(Activity activity, List<ListItem> items,

            int templateId) {

        super(activity, android.R.layout.simple_list_item_1, items);

        this.items = items;

       this.activity = activity;

        this.templateId = templateId;

    }



    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        ListViewWrapper wrapper = null;

        if (row == null) {

            LayoutInflater inflater = activity.getLayoutInflater();

            row = inflater.inflate(R.layout.listviewtemplates, null);

            wrapper = new ListViewWrapper(row);

            row.setTag(wrapper);

        } else {

            wrapper = (ListViewWrapper) row.getTag();
        }

        wrapper.setTemplateId(templateId);

        wrapper.populateFrom(items.get(position));

        return (row);

    }



}

列表视图包装器:

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ListViewWrapper {

    private TextView title = null; //txt

    private TextView description = null; //subtext

    private TextView amount = null;

    private TextView date = null; 

    private ImageView vink = null; 

    private ImageView label = null; //icon

    private TableRow listitem = null;

    private View row = null;

    private int templateId = 0;



    //Set the right template view to be visible

    private void setTemplate() {

        row.findViewById(R.id.templatetitle).setVisibility(View.GONE);

        row.findViewById(R.id.templatepayment).setVisibility(View.GONE);

        row.findViewById(R.id.templatenote).setVisibility(View.GONE);


        switch (templateId) {

        case ListViewAdapter.TPL_TITLE:

            row.findViewById(R.id.templatetitle).setVisibility(View.VISIBLE);

            break;

        case ListViewAdapter.TPL_PAYMENT:

            row.findViewById(R.id.templatepayment).setVisibility(View.VISIBLE);

            break;

        case ListViewAdapter.TPL_NOTE:

            row.findViewById(R.id.templatenote).setVisibility(View.VISIBLE);

            break;

        }

    }



    public ListViewWrapper(View row) {

        this.row = row;

    }



    //Fill the template with the right values

    public void populateFrom(ListItem r) {

        getText().setText(r.getTitle());


        if (templateId == ListViewAdapter.TPL_TITLE) {
             //Do Nothing... Title is enough


//            getIcon().setImageResource(

  //                  ImageUtilities.getDrawableById(r.getType()));

        }

        if (templateId == ListViewAdapter.TPL_PAYMENT) {
            //SUBTXT, AMOUNT, DATE

             getSubtext().setText(r.getDescription());
             getAmount().setText(r.getAmount());
             getDate().setText(r.getDate());
                     }

        if (templateId == ListViewAdapter.TPL_NOTE) {
               getLabel().setImageResource(r.getLabel());
               getBackground().setBackgroundResource(r.getBackground());
            if (r.getVinkOn()==1){getVink().setImageResource(R.drawable.vink_on);}else{getVink().setImageResource(R.drawable.vink_off);}
            }

        setTemplate();

    }



    public int getTemplateId() {

        return templateId;

    }



    public void setTemplateId(int templateId) {

        this.templateId = templateId;

        switch (templateId) {

        case ListViewAdapter.TPL_TITLE:

            break;

        case ListViewAdapter.TPL_PAYMENT:

            break;

        case ListViewAdapter.TPL_NOTE:

            break;

        }

    }


//SET MAIN TITLE ID
    TextView getText() {

        if (title == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_TITLE:

                title = (TextView) row.findViewById(R.id.titletitle);

                break;

            case ListViewAdapter.TPL_PAYMENT:

             title = (TextView) row.findViewById(R.id.titlepayment);

                break;

            case ListViewAdapter.TPL_NOTE:

                title = (TextView) row.findViewById(R.id.notetxt);

                break;

            }

        }

        return (title);

    }



    //Alternative text (all except template 2)

    TextView getSubtext() {

        if (description == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                description = (TextView) row.findViewById(R.id.descriptionpayment);

                break;

            }

        }

        return (description);

    }


    TextView getAmount() {

        if (amount == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                amount = (TextView) row.findViewById(R.id.groupamount);

                break;

            }

        }

        return (amount);

    }

    TextView getDate() {

        if (date == null) {

            switch (templateId) {

            case ListViewAdapter.TPL_PAYMENT:

                date = (TextView) row.findViewById(R.id.date_payment);

                break;

        }


        }

        return (date);

    }



    ImageView getLabel() {

          if (label == null) {

              switch (templateId) {

          case ListViewAdapter.TPL_PAYMENT:

                  label = (ImageView) row.findViewById(R.id.labelpayment);

                  break;

          case ListViewAdapter.TPL_NOTE:

              label = (ImageView) row.findViewById(R.id.label_note);

              break;

      }
          }

        return (label);

    }

    ImageView getVink() {

      if (vink == null) {

            switch (templateId) {


        case ListViewAdapter.TPL_NOTE:

            vink = (ImageView) row.findViewById(R.id.vink);

            break;

    }
        }

      return (vink);

  }

    LinearLayout getBackground(){

      if (listitem == null) {

          switch (templateId) {

        case ListViewAdapter.TPL_PAYMENT:

              listitem = (TableRow) row.findViewById(R.id.templatepayment);

              break;

        case ListViewAdapter.TPL_NOTE:

            listitem = (TableRow) row.findViewById(R.id.templatenote);

            break;

        case ListViewAdapter.TPL_TITLE:

            listitem = (TableRow) row.findViewById(R.id.templatetitle);

            break;

  }
      }

    return (listitem);

    }

}

并创建了一个 ListItem 类(我需要这样做,还是可以使用 Note.class 和 Payment.class?)

public class ListItem {

    private long id;
    private String title;
    private String description;
    private String amount;
    private String date;

    private int label;
    private int background;

    private int type;
    private int vinkon;


    public ListItem(long id, String title, String description, String amount, String date, int type, int vinkon){

        this.id=id;

        this.title = title;

        this.description = description;

        this.amount=amount;

        this.date=date;

        this.type = type;

        this.label=getLabel();

        this.background=getBackground();

        this.vinkon=vinkon;

    }

    public long getId() {
        return id;
      }

      public void setId(long id) {
        this.id = id;
      }


     public String getTitle() {
        return title;
      }

      public void setTitle(String title) {
        this.title = title;
      }

      public String getDescription() {
            return description;
          }

      public void setDescription(String description) {
            this.description = description;
          }

      public String getAmount() {
            return amount;
          }

      public void setAmount(String amount) {
            this.amount = amount;
          }
      public String getDate() {
            return date;
          } 

      public void setDate(String date) {
            this.date = date;
          }

      public int getType() {
                return type;
                      }

public void setType(int type) {
                this.type = type;
                      }

public int getVinkOn() {
    return vinkon;
          }

public void setVinkOn(int vinkon) {
    this.vinkon = vinkon;
          }

public int getLabel() {
    label=R.drawable.c_yellow;
    if (type==11){label=R.drawable.c_azul;}
    if (type==12){label=R.drawable.c_blue;}
    if (type==13){label=R.drawable.c_green;}
    if (type==14){label=R.drawable.c_orange;}
    if (type==15){label=R.drawable.c_purple;}
    if (type==16){label=R.drawable.c_red;}
    if (type==17){label=R.drawable.c_yellow;}
    return label;
          }

public void setLabel(int label) {
    this.label = label;
          }

public int getBackground() {
    background=R.drawable.c_yellow_bg;
    if (type==11){background=R.drawable.c_azul_bg;}
    if (type==12){background=R.drawable.c_blue_bg;}
    if (type==13){background=R.drawable.c_green_bg;}
    if (type==14){background=R.drawable.c_orange_bg;}
    if (type==15){background=R.drawable.c_purple_bg;}
    if (type==16){background=R.drawable.c_red_bg;}
    if (type==17){background=R.drawable.c_yellow_bg;}
    return background;
          }

public void setBackground(int background) {
    this.background = background;
          }


}

在我的活动中,我有以下内容:

public void setList(){
         items = new ArrayList();


                title=getString(R.string.allpayments);
                items.add(new ListItem(id, title, description, amount, date, type, vinkon));    

            List<Payment> allpayments=paymentdatasource.getAllPayments();

                for (int i = 0;i<allpayments.size(); i++) {
                    Payment listpayment = allpayments.get(i);
                    id=listpayment.getId();

                 title=listpayment.getTitle();                     
                     description=listpayment.getDescription();
                     amount=listpayment.getGroupAmount();
                     date=listpayment.getDate();

                     items.add(new ListItem(id, title, description, amount, date, type, vinkon));

                }



        //Set adapter basing on template 1

        adapter = new ListViewAdapter(this, items, ListViewAdapter.TPL_PAYMENT);


        ListView mainMenu = (ListView) findViewById(R.id.listview);

        mainMenu.setAdapter(adapter);


        mainMenu.setTextFilterEnabled(true);

    }

抱歉,我丢失了教程,现在我不知道如何将不同的行布局放入列表视图中,希望它可以理解并且有人可以帮助我更进一步。谢谢!

4

2 回答 2

1

通过创建 BaseAdapter( tutorial ) 的子类来创建自定义列表适配器。在列表适配器的 getView() 方法中,根据您的选择扩展视图。我用过简单的开关,你可以用任何逻辑来选择你的布局。

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    TextView textView;
    View rv = null;
    if (convertView == null) { // if it's not recycled, initialize some
                                                Display display = getWindowManager().getDefaultDisplay();

            rv = convertView;

            if (rv == null) {

        // Inflate from XML
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                switch(id)
                {
                case 1:                 rv = li.inflate(R.layout.listitemlayout1, null);
                    break;
                case 2:                 rv = li.inflate(R.layout.listitemlayout2, null);

                break;
                case 3:                 rv = li.inflate(R.layout.listitemlayout3, null);

                break;
                }

}




        } else {
            rv = convertView;
        }

        return rv;
    }
于 2013-01-17T06:47:52.850 回答
0

您应该使用对您有帮助的包含标签:

http://www.coboltforge.com/2012/05/tech-stuff-layout/

您可以使用它在不同的原始文件中使用不同的布局。

于 2013-01-17T05:06:14.817 回答