2017-02-17 15 views
0

タイトルが示唆しているように、カスタマイズを可能にするために、リストビューにカスタム行を追加しようとしています。私は現在、1つのカスタム行をリストビューに設定しています。これは、情報の入力量に応じて連続して繰り返されることがわかります。今では、リストビューを拡張して複数のカスタム行を表示したいのですが、私は自分のコードを調整することに悩まされています。追加のカスタム行をリストビューに追加するにはどうすればよいですか?

私は現在の設定について説明しました。

まず、情報を設定するために中間で動作するビューホルダー自体があります。

public class ViewHolderWine { 

public ImageView wineImage; 
public TextView wineName; 
public TextView wineDes; 
public TextView winePrice; 


public void setDataIntoViewHolder (List_String listString){ 

    wineImage.setImageResource(listString.getIcon_wine()); 
    wineName.setText(listString.getWineName()); 
    wineDes.setText(listString.getWinedes()); 
    winePrice.setText(listString.getWinePrices()); 

} 

次に、getterとsetterが作成されたList_Stringがあります。

public class List_String { 

int icon_wine; 
String wineName; 
String winedes; 
String winePrices; 


public List_String(int icon_wine, String wineName, String winedes, String winePrices) { 
    this.icon_wine = icon_wine; 
    this.wineName = wineName; 
    this.winedes = winedes; 
    this.winePrices = winePrices; 

} 

public String getWinePrices() { 
    return winePrices; 
} 

public void setWinePrices(String winePrices) { 
    this.winePrices = winePrices; 
} 

etc...... 

我々は次のListFragment

public class List extends ListFragment { 

java.util.List<List_String> lst; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    listData(); 
    // Adapter custom; 


    Adapter myAdapter = new Adapter(getActivity(), R.layout.custom_list, lst); 

    setListAdapter(myAdapter); 

    return super.onCreateView(inflater, container, savedInstanceState); 

} 


public void listData() { 

    lst = new ArrayList<List_String>(); 

    lst.add(new List_String(R.drawable.thumb, etc.... 

は最後に、私は、アダプタを使用して一緒にすべてのピースを持ってきました。

public class Adapter extends ArrayAdapter<List_String> { 

List<List_String> lstWine; 
LayoutInflater inflater; 

public Adapter(Context context, int resource, java.util.List<List_String> objects) { 
    super(context, resource, objects); 


    lstWine = objects; 
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 


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

    View view = convertView; 

    ViewHolderWine viewholder; 


    if (view == null){ 

     viewholder = new ViewHolderWine(); 


       view = inflater.inflate(R.layout.custom_list, null); 

       viewholder.wineImage = (ImageView) view.findViewById(R.id.wineImage); 
       viewholder.wineName = (TextView) view.findViewById(R.id.wineName); 
       viewholder.wineDes = (TextView) view.findViewById(R.id.wineDes); 
       viewholder.winePrice = (TextView) view.findViewById(R.id.winePrice); 



     view.setTag(viewholder); 

    }else{ 

     viewholder = (ViewHolderWine) view.getTag(); 

    } 

    List_String list = lstWine.get(position); 
    viewholder.setDataIntoViewHolder(list); 


    return view; 

} 
+0

のようなものですか?または、いくつかの下の行を別の外観にしたいだけですか? – kampangala

+0

基本的には、上のものとは異なる見た目を持っているいくつかの行があり、私は混合してマッチすることができます。 – Steven

+0

これらは同じ種類のデータを含んでいますか? – kampangala

答えて

0

リストビューをホストする活動のためのレイアウトを使用すると、拡張可能なリストを作りたいんこの

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="1"> 

    <ListView 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </ListView> 
    <ListView 
      android:id="@+id/list2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    </ListView> 
</LinearLayout> 
あなたの活動のクラスで

//populate your first list 
    myList.add(0, new Model("Full name 1", "Good")); 
    myList.add(1, new Model("Full name 2", "Bad")); 
    myList.add(2, new Model("Full name 3", "Worse")); 
    myList.add(3, new Model("Full name 4", "Mediocre")); 

//populate your second list 
    myList2.add(0, new Model2("Full name 5", true)); 
    myList2.add(1, new Model2("Full name 6", true)); 
    myList2.add(2, new Model2("Full name 7", false)); 

//initialize adapters 
    adapter = new mAdapter(StartActivity.this,myList); 
    adapter2 = new mAdapter2(StartActivity.this,myList2); 

//retrieve the list views 
    lv= (ListView) findViewById(R.id.list); 
    lv2= (ListView) findViewById(R.id.list2); 

//set the adapters 
    lv.setAdapter(adapter); 
    lv2.setAdapter(adapter2); 
関連する問題