按从 TCP 服务器获得的数量插入 recyclerView?
Inserting recyclerView by amount of number got from TCP Server?
我正在尝试用 scrollView
中的 cardView
制作一个简单的 recyclerView
这将通过按数字设置它来创建一个新视图,实际上我的应用程序将从 TCP 服务器 接收一个带有数字的字符串,我必须创建相同数量的 recyclerView
作为TCP服务器发送的号码,我已经做了一个cardView
,这里是代码:
<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>
这是 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);
}
}
}
这里是 activity 我 回忆起 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);
}
}
更新问题
实际上我已经创建了一个 recyclerView
,我在其中设置了数据并可视化了 4 cardViews
,但现在我将创建 cardView
从 TCP 服务器 或我从 TCP 服务器 到 TCP 客户端 得到一个 string
,以及现在我将创建 TCP 服务器 发送的字符串中的 cardViews
的数量。
在初始化另一个适配器时删除,如下所示
RecyclerViewSelf selfrecycler = new RecyclerViewSelf(this, selfList);
使用
recyclerView.setAdapter(self);
代替
recyclerView.setAdapter(selfrecycler);
方法应该是这样的
private void prepareSelf() {
Self s = new Self("CASSA SELF 1");
selfList.add(s);
self.notifyDataSetChanged();
}
我不确定我是否完全理解您的要求,但以下内容似乎符合我对您的要求。
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);
}
显然,您会将 initView 的调用从 initViews();
更改为 initViews(tcpnumber_as_int);
我正在尝试用 scrollView
中的 cardView
制作一个简单的 recyclerView
这将通过按数字设置它来创建一个新视图,实际上我的应用程序将从 TCP 服务器 接收一个带有数字的字符串,我必须创建相同数量的 recyclerView
作为TCP服务器发送的号码,我已经做了一个cardView
,这里是代码:
<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>
这是 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);
}
}
}
这里是 activity 我 回忆起 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);
}
}
更新问题
实际上我已经创建了一个 recyclerView
,我在其中设置了数据并可视化了 4 cardViews
,但现在我将创建 cardView
从 TCP 服务器 或我从 TCP 服务器 到 TCP 客户端 得到一个 string
,以及现在我将创建 TCP 服务器 发送的字符串中的 cardViews
的数量。
在初始化另一个适配器时删除,如下所示
RecyclerViewSelf selfrecycler = new RecyclerViewSelf(this, selfList);
使用
recyclerView.setAdapter(self);
代替
recyclerView.setAdapter(selfrecycler);
方法应该是这样的
private void prepareSelf() {
Self s = new Self("CASSA SELF 1");
selfList.add(s);
self.notifyDataSetChanged();
}
我不确定我是否完全理解您的要求,但以下内容似乎符合我对您的要求。
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);
}
显然,您会将 initView 的调用从 initViews();
更改为 initViews(tcpnumber_as_int);