gpt4 book ai didi

java - 根据从 TCP 服务器获取的数量插入 recyclerView?

转载 作者:行者123 更新时间:2023-12-02 02:42:29 24 4
gpt4 key购买 nike

我正在尝试在 scrollView 中使用 cardView 制作一个简单的 recyclerView这将通过按数字设置来创建一个新 View ,实际上我的应用程序将从 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);
}
}

}

这是我调用 RecyclerView 的 Activity :

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 个 cardView,但现在我将创建多个 cardViewTCP服务器获取,或者我得到一个字符串,其中包含从TCP服务器TCP客户端的数字,现在我将创建 TCP 服务器 发送的字符串中的 cardViews 数量。

最佳答案

我不确定我是否完全理解您的要求,但以下内容似乎符合我对您的要求。

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

关于java - 根据从 TCP 服务器获取的数量插入 recyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com