【问题标题】:Display more than one item in array list在数组列表中显示多个项目
【发布时间】:2017-06-19 05:44:16
【问题描述】:

大家好,我想在一个列表中水平显示多个项目。此项目是从具有两个按钮确定和取消的对话框中选取的。当您单击确定时,它应该从编辑文本中选择值并从对话框中选择字符串值并水平显示在列表中。

Array List Adapter
public class VehicleListAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> vehicle_no;
public VehicleListAdapter(Context context, int resource, List<String> objects) {
    super(context, resource, objects);
    this.context = context;
    this.vehicle_no = objects;


}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //get the vehicle number we are displaying
    String my_vehicle = vehicle_no.get(position);

    //get the inflater and inflate the XML layout for each item
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.vehicle_num_layout, null);
    TextView txt_vehicle_num = (TextView) view.findViewById(R.id.txt_vehicle_num);
    txt_vehicle_num.setText(my_vehicle);
    return view;
}

}

Activity with the list
button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.prompts, null);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);

            final EditText userInput = (EditText) promptsView
                    .findViewById(R.id.editTextDialogUserInput);

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // get user input and set it to result
                                    // edit text

                                    if(userInput.getText().toString().trim().length()==0) {
                                        userInput.setError("Vehicle Number Required");
                                        focus.start();
                                        String stime = focus.getText().toString();


                                    }
                                    else {
                                        result.setText(userInput.getText());
                                        ListView list = (ListView) findViewById(R.id.list);
                                        vehicle_list.add(userInput.getText().toString().trim());
                                        VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
                                        list.setAdapter(listAdapter);
                                    }
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }
    });

【问题讨论】:

  • 你遇到了什么错误
  • 在 'vehicle_num_layout' 文件中再添加一个 textView 并在您的适配器中使用它

标签: android arrays android-layout listview arraylist


【解决方案1】:

首先,为什么每次用户点击对话框确定时都要创建新的列表视图。从对话框正按钮单击中删除此行 ListView list = (ListView) findViewById(R.id.list);

在您的代码中的正面按钮单击后添加如下所示的行

VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
list.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged(); // Add this line to tell adapter that data has been changed

【讨论】:

  • 嗨 Divyang 我希望从对话框中连续显示多个项目。每次按下对话框中的确定按钮时,都会在插入的前一个列表下按下一个新列表
  • 有想法的人
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多