gpt4 book ai didi

java - 如何在按下 ListView 外的按钮时获取 ListView 中所有元素的值

转载 作者:搜寻专家 更新时间:2023-11-01 08:44:45 25 4
gpt4 key购买 nike

我的 ListView 的每一行都由一个 TextView 和一个 EditText 组成,我在 ListView 之外有一个按钮。单击该按钮时,我想检索所有 ExitText 值。

我在 DialogFragment 中生成了 ListView。这是我要完成的 saveButton onClick。这是我的代码。任何帮助将不胜感激。

public class SubVitalsEntryDialogFragment extends DialogFragment {

ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
ListView mylist;
Button saveButton;
Button cancelButton;
EditText entryEditText;

public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putParcelableArrayList("subVitalList", sub);
f.setArguments(args);
return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
saveButton = (Button) view.findViewById(R.id.entrySaveButton);
cancelButton = (Button) view.findViewById(R.id.entryCancelButton);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setTitle("Enter your vitals");

cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDialog().dismiss();
}
});

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
///??????????

}
});
return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);
subVitalList = getArguments().getParcelableArrayList("subVitalList");
ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
mylist.setAdapter(adapter);

}

private class MyListAdapter extends ArrayAdapter<SubVitals>{

public MyListAdapter(ArrayList<SubVitals> subVitalList){

super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
Log.d("Tag", "super okay");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null)
itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);

String currentVital = subVitalList.get(position).getSubVitalName();

//Fill the view

TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryTextView));
vitalText.setText(currentVital);

return itemView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subVitalList.size();
}
}

}

This is what it looks like

------------编辑----------------

public class SubVitalsEntryDialogFragment extends DialogFragment {

ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
ListView mylist;
Button saveButton;
Button cancelButton;
EditText entryEditText;
List<String> vitalsEntered = new ArrayList<>();
private HashMap<String, String> textValues = new HashMap<String, String>();

public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putParcelableArrayList("subVitalList", sub);
f.setArguments(args);
return f;
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
saveButton = (Button) view.findViewById(R.id.entrySaveButton);
cancelButton = (Button) view.findViewById(R.id.entryCancelButton);
//getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setTitle("Enter your vitals");

cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDialog().dismiss();
}
});

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("SAVE INSIDE","OK");
for (Map.Entry<String,String> entry : textValues.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d("HASHCHECK","Key: " + key + " Value" + value);
// do stuff
}


}
});
return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);
subVitalList = getArguments().getParcelableArrayList("subVitalList");
ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
mylist.setAdapter(adapter);

}

private class MyListAdapter extends ArrayAdapter<SubVitals>{



public MyListAdapter(ArrayList<SubVitals> subVitalList){

super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
Log.d("Tag", "super okay");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
//Making sure we've a view to work with(may have been given null
Log.d("TAG","Inside get view");
View itemView = convertView;
boolean convertViewWasNull = false;

if (itemView == null){
itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);
convertViewWasNull = true;
}

TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryTextView));
EditText vitalEditText = (EditText) itemView.findViewById(R.id.vitalEntryEditText);

if(convertViewWasNull ){
vitalEditText.addTextChangedListener(new GenericTextWatcher(vitalEditText));
}

String currentVital = subVitalList.get(position).getSubVitalName();

//Fill the view

vitalText.setText(currentVital);
return itemView;

}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subVitalList.size();
}

private class GenericTextWatcher implements TextWatcher {

private View view;
private GenericTextWatcher(View view) {
this.view = view;
}

public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

public void afterTextChanged(Editable editable) {

String text = editable.toString();
//save the value for the given tag :
textValues.put(view.getTag().toString(), editable.toString());
}
}

}

}

最佳答案

不要使用 ListView。它并非设计用于此类目的。 ListView 中的行被回收,滚动时可能会丢失已经输入的数据。请改用 LinearLayout。这样您就可以直接访问其中的 View 。

关于java - 如何在按下 ListView 外的按钮时获取 ListView 中所有元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com