2013-01-17 57 views
0

我想用不同行上的不同XML模板填充一個列表視圖。我失去了一個教程,現在我卡住了。我爲所有行獲得相同的模板。所以,我做什麼,是的,我創建了一個適配器和包裝器:在列表視圖中獲取更多XML佈局

ListViewAdapter:

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); 

    } 



} 

ListViewWrapper:

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); 

    } 

對不起,我失去了教程,現在我不知道如何讓不同的行佈局到ListView中,希望這是可以理解的,有人能幫助我更進了一步。謝謝!

回答

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; 
    } 
0

您應該使用包括標籤,這將成爲ü幫助:

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

您可以使用使用此不同的原糖不同的佈局。

+0

但似乎很難處理例如,當項目點擊,如果我有20個listitems,我有很多佈局包括在內。我所教的教程非常簡單,我認爲它在適配器的某個地方我錯過了一些東西。它應該增加列表視圖中添加的每個項目的不同rowlayout,但我找不到。 – Diego