如何在android中使用类库使用getter和setter

时间:2016-04-24 07:54:54

标签: java android adapter android-recyclerview

我已使用RecyclerView显示记录列表。

列表记录是1到100个数字现在我想显示联系人类的名称列表。

所以我想创建一个Contact类,其中包含变量Name(String)并具有getter和setter方法。

现在如何在课堂上设置名称以及如何从COnatcts类中获取名称?

我不知道如何实现这个。

现在我有一个字符串类型的ArrayList,在这里添加了1到100的数字,并在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>

主要活动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 Code:

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

1 个答案:

答案 0 :(得分:0)

首先创建一个classe Contacts。要快速创建getter和setter方法(如果您有许多变量例如),只需右键单击字符串 - &gt;生成 - &gt;吸气者和二传手

public class Contacts {
    String name;

  public String getName() {
    return name;
  }

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

}

您可以随时创建List<Contacts>并填充

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