我按照这个guid行成功创建了我的动态布局
http://android-er.blogspot.tw/2014/01/get-text-from-dynamically-added-view.html 但是,我有从它获得价值的问题。我的想法是添加一个按钮,当我按下按钮时,它将所有动态布局textview值保存到parse.com
任何人都可以帮我这个吗? 感谢
XML:
<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/date"
android:ems="10"
android:inputType="time"
android:hint="Time of the Event"
/>
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/time"
android:layout_toRightOf="@+id/time"
android:text="Add" />
<Button
android:id="@+id/next_toFriends_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/time"
android:text="Next" />
<LinearLayout
android:id="@+id/time_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/next_toFriends_button"
android:orientation="vertical" >
</LinearLayout>
java:
public class CreateEvent_time_Activity extends FragmentActivity {
EditText mDate;
EditText mTime;
Button mButtonAdd;
LinearLayout container;
Button mButtonNext;
protected String mObjectId;
protected String Time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_time_event);
mDate=(EditText)findViewById(R.id.date);
mDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker;
mDatePicker=new DatePickerDialog(CreateEvent_time_Activity.this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month,
int day) {
mDate.setText(month +"/"+day+"/"+ year);
}
}, mYear,mMonth, mDay);
mDatePicker.setTitle("select date");
mDatePicker.show();
}
});
mTime=(EditText)findViewById(R.id.time);
mTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(CreateEvent_time_Activity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
mTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, false);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
mButtonAdd=(Button)findViewById(R.id.button_add);
container=(LinearLayout)findViewById(R.id.time_container);
mButtonAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.time_row,null);
TextView dateout = (TextView)addView.findViewById(R.id.dateout);
TextView timeout = (TextView)addView.findViewById(R.id.timeout);
dateout.setText(mDate.getText().toString());
timeout.setText(mTime.getText().toString());
String time=dateout.getText().toString();
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);
}
});
mButtonNext=(Button)findViewById(R.id.next_toFriends_button);
mButtonNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=getIntent();
mObjectId=intent.getStringExtra("mobjectId");
ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseConstants.CLASS_Shopinfo);
query.getInBackground(mObjectId, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject event, ParseException e) {
if(e==null){
// this is where i want to store the data to parse.com
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(CreateEvent_time_Activity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
});
}
}
答案 0 :(得分:0)
下面是一个代码片段,如何在布局中动态添加:
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button) addView
.findViewById(R.id.remove);
addView.setTag(tag);
buttonRemove.setTag(tag);
dynamicLayoutsTags.add(tag);
Container.addView(addView);
tag++;
buttonRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// ((LinearLayout) addView.getParent())
// .removeView(addView);
Integer removeTag = (Integer) v.getTag();
View deleteView = Container.findViewWithTag(removeTag);
Container.removeView(deleteView);
dynamicLayoutsTags.remove(removeTag);
}
});
}
});