I'm trying to make a simple recyclerView
with a cardView
in a scrollView
that will create a new view by setting it by number, actually my app will receive a string from the TCP Server with a number, and i have to create the same amount of recyclerView
as the number sent by TCP Server, i've made already a cardView
, here is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="180dp"
android:baselineAligned="false"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="163dp"
android:background="@android:color/white"
android:padding="16dp">
<ImageView
android:id="@+id/self_picture"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
app:srcCompat="@drawable/selfcassa" />
<TextView
android:id="@+id/n_cassa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/self_picture"
android:text="CASSA SELF N°1"
android:textAlignment="center"
android:textColor="@color/red"
android:textSize="42sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Here is the RecyclerViewSelf (recyclerViewAdapter
) :
public class RecyclerViewSelf extends
RecyclerView.Adapter<RecyclerViewSelf.ViewHolder> {
private ArrayList<String> selfList;
public RecyclerViewSelf(ArrayList<String> selfList){
this.selfList = selfList;
}
@Override
public RecyclerViewSelf.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View contactView = LayoutInflater.from(parent.getContext()).inflate(R.layout.self_blueprint, parent, false);
return new ViewHolder(contactView);
}
@Override
public void onBindViewHolder(RecyclerViewSelf.ViewHolder viewHolder, int position) {
viewHolder.n_cassa.setText(selfList.get(position));
}
@Override
public int getItemCount() {
return selfList != null ? selfList.size() : 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView n_cassa;
private ImageView img;
public ViewHolder(View view) {
super(view);
n_cassa = view.findViewById(R.id.n_cassa);
img = view.findViewById(R.id.self_picture);
}
}
}
And here is the activity where i recall the RecyclerView:
public class help extends AppCompatActivity {
private ArrayList selfList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
Utils.darkenStatusBar(this, R.color.colorAccent);
ImageButton home = (ImageButton) findViewById(R.id.casa);
initViews();
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void initViews(){
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerviewSelfMachine);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
selfList = new ArrayList<>();
selfList.add("CASSA SELF N°1");
selfList.add("CASSA SELF N°2");
selfList.add("CASSA SELF N°3");
selfList.add("CASSA SELF N°4");
RecyclerView.Adapter adapter = new RecyclerViewSelf(selfList);
recyclerView.setAdapter(adapter);
}
}
UPDATED QUESTION
Actually i've created a recyclerView
where i set the data and it visualize 4 cardViews
, but now i would create number of cardView
got from the TCP Server or i get a string
with a number from the TCP Server to the TCP Client, and now i would create the number of cardViews
that i've got in the string sent by TCP Server.
I'm not sure that I fully understand what you're asking but the following appears to do what I interpret you as asking for.
private void initViews(int tcpcount){
String base = "CASSA SELF N°";
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerviewSelfMachine);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
selfList = new ArrayList<>();
for (int i=0; i < tcpcount; i++) {
selfList.add(base + Integer.toString(i+1));
}
RecyclerView.Adapter adapter = new RecyclerViewSelf(selfList);
recyclerView.setAdapter(adapter);
}
Obviously you'd change the call of initView from initViews();
to initViews(tcpnumber_as_int);