概述:我正在开发一个名为GPA计算器的项目。用户可以为每个科目添加他的科目的成绩和学分,并计算GPA。我正在为每个学期的GPA计算使用片段。
我正在为每个主题详细信息使用自定义列表视图。 Listview包含EditText(subject_name),Spinner(grade_earned),Spinner(Credit_hrs_of_subject)和Button(delete_current_row)。
我使用Button将新行(主题,成绩和credit_hours)添加到自定义列表视图。
另一个用于计算GPA的按钮。(从自定义列表视图项获取值并计算GPA)。
直到这里我的代码工作正常,我得到了理想的结果。
问题:当我向listview添加一行并更改微调器的值(成绩和学分时),然后创建另一行时,它会将先前微调器的值刷新到第一项。通过滚动列表 OR 也会删除所选的微调器项值并显示所有微调器上的第一个项目。
提前致谢 因为我在过去的三个晚上都在研究这个问题,但未能解决它..
我的代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class OneFragment extends ListFragment {
public ListView MyListView;
public ArrayList<String> TitlesArray = new ArrayList<String>();
ArrayAdapter<String> adapter;
int count = 1;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button btnAdd = (Button) getView().findViewById(R.id.btnAddSub1);
Button btnCalculate= (Button) getView().findViewById(R.id.btnCalculate);
MyListView=getListView();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TitlesArray.add("Subject " + count++);
adapter.notifyDataSetChanged();
}
});
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get list items data
List<String> GradeList = new ArrayList<String>();
List<String> CreditsList = new ArrayList<String>();
for (int i = 0; i < MyListView.getChildCount(); i++) {
// Get row's spinner
View listItem = MyListView.getChildAt(i);
Spinner grades = (Spinner) listItem.findViewById(R.id.spinnerGrades);
Spinner CrdHrs = (Spinner) listItem.findViewById(R.id.spinnerCrdHrs);
// Get selection
String grade = (String) grades.getSelectedItem();
String CreditHrs = (String) CrdHrs.getSelectedItem();
GradeList.add(grade);
CreditsList.add(CreditHrs);
}
//calculating gpa--formula work--working fine
String[] gradeArray = GradeList.toArray(new String[0]);
String[] CreditsArray = CreditsList.toArray(new String[0]);
double addedvals=0;
double addCreditsTotol=0;
for(int i=0; i<MyListView.getChildCount();i++){
double valxOfgrade= Double.parseDouble(MainActivity.getValueOfGrade(getActivity(),gradeArray[i]));
double valxOfcredits= Double.parseDouble(CreditsArray[i]);
double multiply=valxOfcredits*valxOfgrade;
addedvals+=multiply;
addCreditsTotol+=Double.parseDouble(CreditsArray[i]);
}
double GPAval=addedvals/addCreditsTotol;
TextView TVgpa = (TextView) getActivity().findViewById(R.id.TVGPA);
TVgpa.setText("Your gpa is : "+String.valueOf(GPAval));
}
});
adapter = new MynewAdapter(getActivity(), R.layout.list_view_semester, TitlesArray);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
public class MynewAdapter extends ArrayAdapter<String> {
public int layout;
public MynewAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
boolean delete;
Context mContext;
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final int pos=position;
ViewHolder mainViewHolder = null;
mContext = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (EditText) convertView.findViewById(R.id.ETsubject);
viewHolder.grade = (Spinner) convertView.findViewById(R.id.spinnerGrades);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, MainActivity.Grades);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.grade.setAdapter(dataAdapter);
viewHolder.credits = (Spinner) convertView.findViewById(R.id.spinnerCrdHrs);
viewHolder.btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
viewHolder.grade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str=(String) parent.getItemAtPosition(position);
Log.d("values str",String.valueOf(str));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//delete button
viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delete=true;
TitlesArray.remove(position);
Log.d("focus","delete");
adapter.notifyDataSetChanged();
}
});
convertView.setTag(viewHolder);
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
viewHolder.title.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
/*
* When focus is lost save the entered value of editText
*/
if (!hasFocus) {
if(delete==true){
delete=false;
}else{
String enteredTitle = ((EditText) v).getText().toString();
TitlesArray.set(pos, enteredTitle);
}
}
}
});
return convertView;
}
public class ViewHolder {
EditText title;
Spinner grade;
Spinner credits;
ImageButton btnDelete;
}
}
}
答案 0 :(得分:0)
经过长时间的搜索,我找到了解决问题的方法。这是我跟着Answer here
的答案的链接我的更新代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class OneFragment extends ListFragment {
public ListView MyListView;
public ArrayList<String> TitlesArray = new ArrayList<String>();
ArrayAdapter<String> adapter;
int count = 1;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button btnAdd = (Button) getView().findViewById(R.id.btnAddSub1);
Button btnCalculate= (Button) getView().findViewById(R.id.btnCalculate);
final EditText etSubs= (EditText) getView().findViewById(R.id.ETnoOfSubs);
MyListView=getListView();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int subs= Integer.parseInt(etSubs.getText().toString());
for(int i=0;i<subs;i++) {
TitlesArray.add("Subject " + count++);
}
adapter.notifyDataSetChanged();
}
});
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get list items data
List<String> GradeList = new ArrayList<String>();
List<String> CreditsList = new ArrayList<String>();
for (int i = 0; i < MyListView.getChildCount(); i++) {
// Get row's spinner
View listItem = MyListView.getChildAt(i);
Spinner grades = (Spinner) listItem.findViewById(R.id.spinnerGrades);
Spinner CrdHrs = (Spinner) listItem.findViewById(R.id.spinnerCrdHrs);
// Get selection
String grade = (String) grades.getSelectedItem();
String CreditHrs = (String) CrdHrs.getSelectedItem();
GradeList.add(grade);
CreditsList.add(CreditHrs);
}
//calculating gpa--formula work--working fine
String[] gradeArray = GradeList.toArray(new String[0]);
String[] CreditsArray = CreditsList.toArray(new String[0]);
double addedvals=0;
double addCreditsTotol=0;
for(int i=0; i<MyListView.getChildCount();i++){
double valxOfgrade= Double.parseDouble(MainActivity.getValueOfGrade(getActivity(),gradeArray[i]));
double valxOfcredits= Double.parseDouble(CreditsArray[i]);
double multiply=valxOfcredits*valxOfgrade;
addedvals+=multiply;
addCreditsTotol+=Double.parseDouble(CreditsArray[i]);
}
double GPAval=addedvals/addCreditsTotol;
TextView TVgpa = (TextView) getActivity().findViewById(R.id.TVGPA);
TVgpa.setText("Your gpa is : "+String.valueOf(GPAval));
}
});
adapter = new MynewAdapter(getActivity(), R.layout.list_view_semester, TitlesArray);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
public class MynewAdapter extends ArrayAdapter<String> {
public int layout;
public MynewAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
boolean delete;
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View v = convertView;
ViewHolder viewHolder;
if(v == null){
v=inflater.inflate(layout, parent,false);
viewHolder = new ViewHolder();
viewHolder.title = (EditText) v.findViewById(R.id.ETsubject);
viewHolder.grade = (Spinner) v.findViewById(R.id.spinnerGrades);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, MainActivity.Grades);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.grade.setAdapter(dataAdapter);
viewHolder.credits = (Spinner) v.findViewById(R.id.spinnerCrdHrs);
viewHolder.btnDelete = (ImageButton) v.findViewById(R.id.btnDelete);
v.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) v.getTag();
}
String s = getItem(position);
viewHolder.title.setText(s);
viewHolder.grade.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str=(String) parent.getItemAtPosition(position);
Log.d("values str",String.valueOf(str));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//delete button
viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delete=true;
TitlesArray.remove(position);
Log.d("focus","delete");
adapter.notifyDataSetChanged();
}
});
viewHolder.title.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
/*
* When focus is lost save the entered value of editText
*/
if (!hasFocus) {
if(delete==true){
delete=false;
}else{
String enteredTitle = ((EditText) v).getText().toString();
TitlesArray.set(position, enteredTitle);
}
}
}
});
return v;
}
public class ViewHolder {
EditText title;
Spinner grade;
Spinner credits;
ImageButton btnDelete;
}
}
}