将用户输入存储在数组中并在recyclerview中显示

时间:2018-10-05 14:36:28

标签: android arraylist android-recyclerview recycler-adapter

我制作了一个屏幕键盘,用户可以在其中键入任何数字。

当用户单击“确定”按钮以将该数字存储在ArrayList中时,如何存储数字键盘上的用户输入?另外,在用户单击“确定”按钮后,如何立即在recyclerview内的textview中显示存储的号码。

MainActivity

public class MainActivity extends FragmentActivity implements View.OnClickListener
{

按钮btn [] =新按钮[12];

TextView userInput;

List<Data> moreData = allData();

private GridLayoutManager gridLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn[0] = (Button) findViewById(R.id.button1);
    btn[1] = (Button) findViewById(R.id.button2);
    btn[2] = (Button) findViewById(R.id.button3);
    btn[3] = (Button) findViewById(R.id.button4);
    btn[4] = (Button) findViewById(R.id.button5);
    btn[5] = (Button) findViewById(R.id.button6);
    btn[6] = (Button) findViewById(R.id.button7);
    btn[7] = (Button) findViewById(R.id.button8);
    btn[8] = (Button) findViewById(R.id.button9);
    btn[9] = (Button) findViewById(R.id.button0);
    btn[10] = (Button) findViewById(R.id.buttonClr);
    btn[11] = (Button) findViewById(R.id.buttonOK);

    for(int i=0; i<12; i++)
    {
        btn[i].setOnClickListener(this);
    }

    gridLayout = new GridLayoutManager(MainActivity.this, 3);

    RecyclerView allDataRecycler = (RecyclerView) findViewById(R.id.dataRecycler);
    allDataRecycler.setLayoutManager(gridLayout);
    DataAdapter dataAdapter = new DataAdapter(MainActivity.this, moreData, new MainActivity.selectedTableClickListener());
    allDataRecycler.setAdapter(dataAdapter);

}


public List<Data> allData()
{
    List<Data> allData = new ArrayList<Data>();

    return allData;

}

public void displayNumber(String numbers)
{
    userInput = (TextView) findViewById(R.id.numberInput);
    userInput.append(numbers);
}

@Override
public void onClick(View v)
{
    switch(v.getId())
    {
        case R.id.button1:
            displayNumber("1");
            break;

        case R.id.button2:
            displayNumber("2");
            break;

        case R.id.button3:
            displayNumber("3");
            break;

        case R.id.button4:
            displayNumber("4");
            break;

        case  R.id.button5:
            displayNumber("5");
            break;

        case R.id.button6:
            displayNumber("6");
            break;

        case R.id.button7:
            displayNumber("7");
            break;

        case R.id.button8:
            displayNumber("8");
            break;

        case R.id.button9:
            displayNumber("9");
            break;

        case R.id.button0:
            displayNumber("0");
            break;

        case R.id.buttonClr:
            userInput = (TextView)findViewById(R.id.numberInput);
            userInput.setText("");
            break;

        case R.id.buttonOK:

            break;
    }

}
}

DataAdapter

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.DataViewHolder>
{
public interface OnItemClickListener
{
    void onItemClicker(Data item);
}

List<Data> allData = Collections.emptyList();
Context context;

private final OnItemClickListener listener;

public DataAdapter(Context context, List<Data> allData, OnItemClickListener listener)
{
    this.allData = allData;
    this.context = context;
    this.listener = listener;
}

@NonNull
@Override
public DataViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
{
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cells, null);
    DataViewHolder dataHolder = new DataViewHolder(layoutView);
    return dataHolder;
}

@Override
public void onBindViewHolder(DataViewHolder holder, int position) {
    holder.tableNum.setText(allData.get(position).tableNum);
}

@Override
public int getItemCount() {
    return allData.size();
}

public class DataViewHolder extends RecyclerView.ViewHolder
{
    TextView tableNum;

    DataViewHolder(final View itemView)
    {
        super(itemView);
        selectedTableNum = (TextView) itemView.findViewById(R.id.selected_tableNum);
    }

    public void bind(final Data item, final OnItemClickListener listener)
    {
        tableNum.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                listener.onItemClicker(item);
            }
        });
    }
}
}

0 个答案:

没有答案