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

时间:2015-07-08 15:13:21

标签: android list listview view adapter

I have this register window with edit text fields

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

这是我的权限类,所有编辑文本字段如何在按钮上单击“添加”并将它们添加到列表视图,事实是,在一行中我需要显示名称姓氏然后我单击列表项目显示日期和时间。 我已经尝试了谷歌的每个教程,但我想我错过了一些东西。实际上我可以通过使用toString()方法从编辑文本字段中获取字符串,但对我来说最困难的部分是如何将所有这些字符串存储在listitem中并在listview中显示它们。

1 个答案:

答案 0 :(得分:1)

First Create User Object:

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

Then Create Custom list adapter : this adapter has a constructor which takes context, and arraylist of Users as arguments:

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

}

now add listview to your layout:

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

and now create a new layout for the list row item with two textviews one for surname and other for name :

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>

Now add this code to Permission activity:

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

then add this to Permission onCreate()

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

and add this code to add button

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