创建一个由字符串设置的recyclerViews数量?

时间:2017-07-20 08:27:53

标签: java android tcp android-recyclerview

我收到一个String来自 TCP服务器的号码到我的 TCP客户端String具体说明现金数量在线注册,我必须创建一个cardView的每个收银机,我创建了一个cardViewrecyclerViewAdapter,但现在设置收银机数量的人是我添加字符串在ArrayList。 在 MainActivity 中, TCP服务器发送的String尚未设置为静态,但现在我不知道如何使用它来创建所需的金额。< / p>

以下是代码,其中我唤起了recyclerViewAdapter

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

1 个答案:

答案 0 :(得分:1)

//从服务器收到字符串后

String num = serverValue;

int limit = Integer.valueof(num);

selfList = new ArrayList<>();

for(int i=0; i<limit; i++)
{
    selfList.add("" + i);    // add "" with i if you want list with string type
}

// call your init method here
initViews();


// your init method
private void initViews(){
    RecyclerView recyclerView = 
    (RecyclerView)findViewById(R.id.recyclerviewSelfMachine);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = 
    new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);

    RecyclerView.Adapter adapter = new RecyclerViewSelf(selfList);
    recyclerView.setAdapter(adapter);
    }
}