如何从编辑文本中获取字符串并在列表视图中显示它们?

How to get strings from edit text and show them in list view?

Permission.class

 public class Permission extends ActionBarActivity{
 EditText Date;
 EditText Time;
 EditText userName;
 EditText userSurname;

 EditText Name;
 String Surname;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_permission);

    Button add = (Button) findViewById(R.id.add);
    Button cancel = (Button) findViewById(R.id.cancel);


    // Create custom dialog object          
    // set values for custom dialog components - text, image and button
    userName = (EditText) findViewById(R.id.dialog_username);
    userSurname = (EditText)findViewById(R.id.dialog_usersurname);
    Time = (EditText)findViewById(R.id.dialog_time2);
    Date = (EditText)findViewById(R.id.dialog_date2);   


           Date.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //To show current date in the datepicker
                    Calendar mcurrentDate=Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog mDatePicker=new DatePickerDialog(Permission.this, new OnDateSetListener() {                  
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                            // TODO Auto-generated method stub                      
                            /*      Your code   to get date and time    */
                            Date.setText(selectedyear+"-"+(selectedmonth+1)+"-"+selectedday);
                        }
                    },mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select date");                
                    mDatePicker.show();  }
            });


           Time.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker

                   Calendar mcurrentTime=Calendar.getInstance();
                   int mHour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                   int mMinute = mcurrentTime.get(Calendar.MINUTE);


                   TimePickerDialog mTimePicker=new TimePickerDialog(Permission.this, new OnTimeSetListener() {                  
                       public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute ) {
                           // TODO Auto-generated method stub                      
                           /*      Your code   to get date and time    */
                          if (selectedminute<10){
                              Time.setText(selectedhour+":"+"0"+(selectedminute)+""+"val");
                          }
                          else
                        Time.setText(selectedhour+":"+(selectedminute));
                       }
                   },mHour, mMinute, false);
                   mTimePicker.setTitle("Select date");                
                   mTimePicker.show();  }
           });


          cancel.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker
                    startActivity(new Intent(Permission.this, Choose.class));
                  Permission.this.finish();
               }     
           });

          add.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub

              }     
          });

        }


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.permission, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

这是我的许可 class 所有编辑文本字段如何让它们全部点击按钮 "Add" 并将它们添加到列表视图,事实是我需要在一行中显示姓名姓氏,然后我单击显示日期和时间的列表项。 我已经尝试了 google 上每个教程的所有内容,但我想我遗漏了一些东西。事实上,我可以使用 toString() 方法从编辑文本字段中获取字符串,但对我来说最困难的部分是如何将所有这些字符串存储在列表项中并在列表视图中显示它们。

首先创建用户对象:

public class Users {


String name,surname,mydate,mytime;

public Users(String name, String surname, String mydate, String mytime) {
    this.name = name;
    this.surname = surname;
    this.mydate = mydate;
    this.mytime = mytime;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getMydate() {
    return mydate;
}

public void setMydate(String mydate) {
    this.mydate = mydate;
}

public String getMytime() {
    return mytime;
}

public void setMytime(String mytime) {
    this.mytime = mytime;
}
}

然后创建自定义列表适配器: 这个适配器有一个构造函数,它将上下文和用户数组列表作为参数:

public class CustomArrayAdapter extends BaseAdapter {
private ArrayList<Users> listData;
private LayoutInflater layoutInflater;

public CustomArrayAdapter(Context aContext, ArrayList<Users> listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(aContext);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {


    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.Surname = (TextView) convertView.findViewById(R.id.surname_tv);
        holder.name = (TextView) convertView.findViewById(R.id.name_tv);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //
    holder.Surname.setText(listData.get(position).getSurname());
    holder.name.setText(listData.get(position).getName());
    return convertView;
}

static class ViewHolder {
    TextView Surname;
    TextView name;
}

}

现在将列表视图添加到您的布局中:

 <ListView
    android:id="@+id/users_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

现在为列表行项目创建一个新布局,其中包含两个文本视图,一个用于姓氏,另一个用于名称:

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
>

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/surname_tv"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/surname_tv"
    android:id="@+id/name_tv"
    android:layout_height="wrap_content" />
</RelativeLayout>

现在将此代码添加到权限 activity:

ArrayList<Users> usersList;
private CustomArrayAdapter adapter;
ListView lv1;

然后将其添加到 Permission onCreate()

    lv1 = (ListView) findViewById(R.id.users_list);
    usersList = new ArrayList<Users>();

并添加此代码以添加按钮

usersList.add(new Users(userSurname.getText().toString(),userName.getText().toString(),Date.getText().toString(),Time.getText().toString()));
    adapter = new CustomArrayAdapter(this,usersList);
    lv1.setAdapter(adapter);