2017-12-19 6 views
0

私はアンドロイドを新しくしています。私はプロジェクトで働いており、質問があります。 ArrayList<the_class>からデータを取得してlistviewに入力するにはどうすればよいですか? 6つのテキストビューを含むxmlファイルがあり、クラス項目のコンテキストで埋めなければなりません。ありがとうございます複数のデータをリストビューに複数のテキストビューで取り込む方法は?

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="10dp"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:text="origin" 
      android:textSize="19dp" 
      android:id="@+id/origin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:text="destination" 
      android:textSize="19dp" 
      android:paddingLeft="15dp" 
      android:id="@+id/destination" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

* xmlファイルの一部です。

+0

何を試しましたか?何か? – DroiDev

+0

私はlistadapterを使用しました。それは動作しますが、それは本当に遅いです。別のより速い方法があるのだろうかと思います。 – SerKleanthis

答えて

1

実際はとても簡単です。

は私が次にモデル

public class Model { 

    String top, bottom; 

    public Model(String top, String bottom) { 
     this.top = top; 
     this.bottom = bottom; 
    } 

    public String getTop() { 
     return top; 
    } 

    public void setTop(String top) { 
     this.top = top; 
    } 

    public String getBottom() { 
     return bottom; 
    } 

    public void setBottom(String bottom) { 
     this.bottom = bottom; 
    } 
} 

を作成し、私はそれのためにリストビューのアダプタを作成する必要があります。私の主な活動で

public class ListAdapter extends ArrayAdapter<Model> { 


    private Context activityContext; 
    private List<Model> list; 
    public static final String TAG = "ListView"; 

    public ListAdapter(Context context, List<Model> list){ 
     super(context, R.layout.single_listview, list); 
     this.activityContext = context; 
     this.list = list; 
    } 


    @Override 
    public View getView(final int position, View view, ViewGroup viewGroup){ 

     final ViewHolder viewHolder; 

     if (view == null) { 
      view = LayoutInflater.from(activityContext).inflate(R.layout.single_listview, null); 
      viewHolder = new ViewHolder(); 

      viewHolder.top = (TextView) view.findViewById(R.id.top); 
      viewHolder.bottom = (TextView) view.findViewById(R.id.bottom); 

      viewHolder.top.setText(list.get(position).getTop()); 
      viewHolder.bottom.setText(list.get(position).getBottom()); 

      view.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) view.getTag(); 
     } 

     return view; 
    } 

    private static class ViewHolder { 

     TextView top; 
     TextView bottom; 
    } 


} 

、私はメインの活動のレイアウトXML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.fuhnatik.customlistview.MainActivity"> 

    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/listview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</android.support.constraint.ConstraintLayout> 

最後に、リストビューアダプタレイアウトXMLの内部

の内側に次のコード

public class MainActivity extends AppCompatActivity { 

    ListView listview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

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

     List<Model> list = new ArrayList<>(); 
     list.add(new Model("top-one", "bot-one")); 
     list.add(new Model("top-two", "bot-two")); 
     list.add(new Model("top-three", "bot-three")); 
     list.add(new Model("top-four", "bot-four")); 
     list.add(new Model("top-five", "bot-five")); 
     list.add(new Model("top-six", "bot-six")); 
     list.add(new Model("top-seven", "bot-seven")); 
     list.add(new Model("top-eight", "bot-eight")); 

     ListAdapter adapter = new ListAdapter(listview.getContext(), list); 
     listview.setAdapter(adapter); 
    } 
} 

を行います

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" 
    android:orientation="vertical"> 


    <TextView 
     android:id="@+id/top" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:text="TOP SECTION HERE" 
     android:textSize="16sp" /> 

    <TextView 
     android:id="@+id/bottom" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="BOTTOM SECTION HERE" 
     android:textSize="12sp" /> 



</LinearLayout> 

res最終:

enter image description here

+0

Woww ..本当に助かりました。どうもありがとうございました! – SerKleanthis

+0

あなたはそれを持っています。ハッピーコーディング – DroiDev

関連する問題