2016-04-24 3 views
1

私はRecyclerViewを使用してレコードのリストを表示しています。アンドロイドのクラスライブラリを使用してゲッターとセッターを使用するには

リストレコードは1〜100の数字です。これで、連絡先クラスの名前リストを表示したいと思います。

私は変数Name(String)があり、getterメソッドとsetterメソッドを持つContactクラスを作成したいと考えています。

今、クラスに名前を設定したり、名前をCOnatctsクラスから取得する方法を教えてください。

これを実装する方法はわかりません。

今私はこの中に1から100までの文字列型のArrayListを追加し、これらをRecyclerViewに挿入します。

今、名前を持つリストのオブジェクトを生成したいと思います。

どうすればいいですか?

アダプターコード:

public class AdapterData extends RecyclerView.Adapter<AdapterData.DummyHolder> { 

    private LayoutInflater layoutInflater; 
    private ArrayList<String> mItems = new ArrayList<>(); 
    public Context ThisContext; 

    public AdapterData(Context context) 
    { 
     layoutInflater = LayoutInflater.from(context); 
     mItems = generateValues(); 
     ThisContext = context; 
    } 

    public static ArrayList<String> generateValues(){ 
     ArrayList<String> Dummy = new ArrayList<>(); 
     for(int i=1; i<100; i++) 
     { 
      Dummy.add("Item"+i); 
      Log.d("MTS", String.valueOf(i)); 
     } 
     return Dummy; 
    } 

    @Override 
    public DummyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view= layoutInflater.inflate(R.layout.row_layout,parent,false); 
     DummyHolder holder=new DummyHolder(view); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(DummyHolder holder, int position) { 
     holder.txt_name.setText(mItems.get(position)); 
     Log.d("MAN=",mItems.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     return 100; 
    } 

    public static class DummyHolder extends RecyclerView.ViewHolder{ 

     TextView txt_name; 

     public DummyHolder(View itemView) { 
      super(itemView); 
      txt_name = (TextView) itemView.findViewById(R.id.tx_name); 
     } 
    } 
} 

行のコード:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_name" 
     android:hint="Hello"/> 

</LinearLayout> 

メインActiivity XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.hogwarts.harrypotter.recyclerdemo.MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rv_list"> 

    </android.support.v7.widget.RecyclerView> 
</RelativeLayout> 

MainActivityコード:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    recyclerView = (RecyclerView) findViewById(R.id.rv_list); 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(linearLayoutManager); 
    recyclerView.setAdapter(new AdapterData(this)); 
} 

答えて

0

まず、クラスContactsを作成します。 >生成 - - > GetterおよびSetter

public class Contacts { 
    String name; 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 

} 

したい時はいつでもあなたがList<Contacts>を作成し、それを移入することができ、文字列のちょうど右クリック(あなたがexempleのために多くの変数を持っている場合)迅速getterとsetterメソッドを作成するには

List<Contacts> data = new ArrayList<>(); 
for(int i = 0; i < 10; i++){ 
    Contacts contacts = new Contacts(); 
    contacts.setName("name_" + i); 

    data.add(contacts); 
} 

それは同じプロセス

// in case of a recyclerview you would have pass the List to your adapter in its constructor 

//in construcor just store the list into a variable 
this.data = data 

//in onBindViewHolder you retrieve the data from that list 

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    Contacts currentContact = data.get(position); 
    String name = current.get(title); 
} 
のデータを取得するには
関連する問題