Android 在片段中按下按钮时更新文本视图
我有两个片段,应该更新第三个片段,它在两个不同的Android 在片段中按下按钮时更新文本视图,android,android-fragments,android-linearlayout,tablelayout,Android,Android Fragments,Android Linearlayout,Tablelayout,我有两个片段,应该更新第三个片段,它在两个不同的表格布局中更新文本视图,包装在线性布局中 这是我的第三个片段: public class HistoryFragment extends Fragment { static TextView pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, grocTot; static TextView oth1, oth2, oth3, oth
表格布局中更新文本视图
,包装在线性布局中
这是我的第三个片段:
public class HistoryFragment extends Fragment {
static TextView pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, grocTot;
static TextView oth1, oth2, oth3, oth4, oth5, oth6, oth7, oth8, oth9, oth10, oth11, oth12, othTot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.history_layout, container, false);
pos1 = (TextView)view.findViewById(R.id.grocstore1);
...
oth1 = (TextView)view.findViewById(R.id.othstore1);
...
return view;
}
//when button is pressed in first fragment, this updates
public void updateTable() {
File myFile = new File(Environment.getExternalStorageDirectory(), "dir/savings.csv");
String line;
ArrayList<String> savingList = new ArrayList<String>();
int count = 0, total = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(myFile));
while((line = reader.readLine()) != null) {
String[] strings = line.split(",");
for(String s : strings) {
savingList.add(s);
//total += Integer.parseInt(strings[count+3]);
}
//System.out.println(savingList.get(count));
count += 4;
total += Integer.parseInt(savingList.get(count - 2));
//System.out.println(total);
//System.out.println(count);
}
//System.out.println(count);
if(count == 4) {
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText("-");
pos6.setText("-");
pos7.setText("-");
pos8.setText("-");
pos9.setText("-");
pos10.setText("-");
pos11.setText("-");
pos12.setText("-");
}
else if (count == 8){
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText(savingList.get(savingList.size()-8));
pos6.setText("$"+savingList.get(savingList.size()-7));
pos7.setText("$"+savingList.get(savingList.size()-6));
pos8.setText(savingList.get(savingList.size()-5));
pos9.setText("-");
pos10.setText("-");
pos11.setText("-");
pos12.setText("-");
}
else {
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText(savingList.get(savingList.size()-8));
pos6.setText("$"+savingList.get(savingList.size()-7));
pos7.setText("$"+savingList.get(savingList.size()-6));
pos8.setText(savingList.get(savingList.size()-5));
pos9.setText(savingList.get(savingList.size()-12));
pos10.setText("$"+savingList.get(savingList.size()-11));
pos11.setText("$"+savingList.get(savingList.size()-10));
pos12.setText(savingList.get(savingList.size()-9));
}
//grocTot.setText("$"+String.valueOf(total));
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//when button is pressed in second fragment, this updates
public void updateTable2() {
File myFile2 = new File(Environment.getExternalStorageDirectory(), "dir/other.csv");
String line;
ArrayList<String> savingList2 = new ArrayList<String>();
int count = 0, total = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(myFile2));
while((line = reader.readLine()) != null) {
String[] strings = line.split(",");
for(String s : strings) {
savingList2.add(s);
//total += Integer.parseInt(strings[count+3]);
}
//System.out.println(savingList.get(count));
count += 4;
total += Integer.parseInt(savingList2.get(count - 2));
//System.out.println(total);
System.out.println(count);
}
System.out.println(count);
if(count == 4) {
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
System.out.println("sup sfkj" + savingList2.get(savingList2.size()-4));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText("-");
oth6.setText("-");
oth7.setText("-");
oth8.setText("-");
oth9.setText("-");
oth10.setText("-");
oth11.setText("-");
oth12.setText("-");
}
else if (count == 8){
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText(savingList2.get(savingList2.size()-8));
oth6.setText("$"+savingList2.get(savingList2.size()-7));
oth7.setText("$"+savingList2.get(savingList2.size()-6));
oth8.setText(savingList2.get(savingList2.size()-5));
oth9.setText("-");
oth10.setText("-");
oth11.setText("-");
oth12.setText("-");
}
else {
System.out.println("sup sfkj" + savingList2.get(savingList2.size()-4));
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText(savingList2.get(savingList2.size()-8));
oth6.setText("$"+savingList2.get(savingList2.size()-7));
oth7.setText("$"+savingList2.get(savingList2.size()-6));
oth8.setText(savingList2.get(savingList2.size()-5));
oth9.setText(savingList2.get(savingList2.size()-12));
oth10.setText("$"+savingList2.get(savingList2.size()-11));
oth11.setText("$"+savingList2.get(savingList2.size()-10));
oth12.setText(savingList2.get(savingList2.size()-9));
}
//othTot.setText("$"+String.valueOf(total));
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Add2Fragment extends Fragment {
static EditText spent,saved,coupons;
Button writeExcelButton;
Spinner spinner;
UpdateButton2 updateButton;
public interface UpdateButton2 {
public void onButtonClick2();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
updateButton = (UpdateButton2) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement UpdateFile");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add2_layout, container, false);
setSpinnerContent(view);
spent = (EditText) view.findViewById(R.id.text_spent2);
saved = (EditText) view.findViewById(R.id.text_saved2);
coupons = (EditText) view.findViewById(R.id.text_coupons2);
writeExcelButton = (Button) view.findViewById(R.id.button_addOth);
writeExcelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateFile();
buttonClicked(v);
}
});
return view;
}
private void setSpinnerContent (View view) {
//set spinner
}
public void UpdateFile() {
//write to file
}
public void buttonClicked(View v) {
updateButton.onButtonClick2();
}
}
我已经检查了两到三次语法,没有发现任何错误,所以我不知道为什么这不起作用。任何帮助都将不胜感激
编辑
MainActivity中的updateTable调用:
public void onButtonClick() {
android.support.v4.app.Fragment f = this.getSupportFragmentManager().findFragmentByTag(getFragmentTag(0));
((HistoryFragment) f).updateTable();
}
public void onButtonClick2() {
android.support.v4.app.Fragment f2 = this.getSupportFragmentManager().findFragmentByTag(getFragmentTag(0));
((HistoryFragment) f2).updateTable2();
}
编辑2
我放置了System.out.println(oth1.getText().toString())
在UpdateTable2()方法中,查看字符串是否被放置在其中,因此在tablelayout
中显示值似乎只是一个问题。但我仍然不知道为什么
编辑3
public class AddFragment extends Fragment {
static EditText spent,saved,coupons;
Button writeExcelButton;
Spinner spinner;
UpdateButton updateButton;
public interface UpdateButton {
public void onButtonClick();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
updateButton = (UpdateButton) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement UpdateFile");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_layout, container, false);
setSpinnerContent(view);
spent = (EditText) view.findViewById(R.id.text_spent1);
saved = (EditText) view.findViewById(R.id.text_saved1);
coupons = (EditText) view.findViewById(R.id.text_coupons1);
writeExcelButton = (Button) view.findViewById(R.id.button_addGroc);
writeExcelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateFile();
buttonClicked(v);
}
});
return view;
}
private void setSpinnerContent (View view) {
//set spinner
}
public void UpdateFile() {
//write to file
}
public void buttonClicked(View v) {
updateButton.onButtonClick();
}
}
第二段:
public class HistoryFragment extends Fragment {
static TextView pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, pos10, pos11, pos12, grocTot;
static TextView oth1, oth2, oth3, oth4, oth5, oth6, oth7, oth8, oth9, oth10, oth11, oth12, othTot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.history_layout, container, false);
pos1 = (TextView)view.findViewById(R.id.grocstore1);
...
oth1 = (TextView)view.findViewById(R.id.othstore1);
...
return view;
}
//when button is pressed in first fragment, this updates
public void updateTable() {
File myFile = new File(Environment.getExternalStorageDirectory(), "dir/savings.csv");
String line;
ArrayList<String> savingList = new ArrayList<String>();
int count = 0, total = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(myFile));
while((line = reader.readLine()) != null) {
String[] strings = line.split(",");
for(String s : strings) {
savingList.add(s);
//total += Integer.parseInt(strings[count+3]);
}
//System.out.println(savingList.get(count));
count += 4;
total += Integer.parseInt(savingList.get(count - 2));
//System.out.println(total);
//System.out.println(count);
}
//System.out.println(count);
if(count == 4) {
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText("-");
pos6.setText("-");
pos7.setText("-");
pos8.setText("-");
pos9.setText("-");
pos10.setText("-");
pos11.setText("-");
pos12.setText("-");
}
else if (count == 8){
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText(savingList.get(savingList.size()-8));
pos6.setText("$"+savingList.get(savingList.size()-7));
pos7.setText("$"+savingList.get(savingList.size()-6));
pos8.setText(savingList.get(savingList.size()-5));
pos9.setText("-");
pos10.setText("-");
pos11.setText("-");
pos12.setText("-");
}
else {
pos1.setText(savingList.get(savingList.size()-4));
pos2.setText("$"+savingList.get(savingList.size()-3));
pos3.setText("$"+savingList.get(savingList.size()-2));
pos4.setText(savingList.get(savingList.size()-1));
pos5.setText(savingList.get(savingList.size()-8));
pos6.setText("$"+savingList.get(savingList.size()-7));
pos7.setText("$"+savingList.get(savingList.size()-6));
pos8.setText(savingList.get(savingList.size()-5));
pos9.setText(savingList.get(savingList.size()-12));
pos10.setText("$"+savingList.get(savingList.size()-11));
pos11.setText("$"+savingList.get(savingList.size()-10));
pos12.setText(savingList.get(savingList.size()-9));
}
//grocTot.setText("$"+String.valueOf(total));
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//when button is pressed in second fragment, this updates
public void updateTable2() {
File myFile2 = new File(Environment.getExternalStorageDirectory(), "dir/other.csv");
String line;
ArrayList<String> savingList2 = new ArrayList<String>();
int count = 0, total = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(myFile2));
while((line = reader.readLine()) != null) {
String[] strings = line.split(",");
for(String s : strings) {
savingList2.add(s);
//total += Integer.parseInt(strings[count+3]);
}
//System.out.println(savingList.get(count));
count += 4;
total += Integer.parseInt(savingList2.get(count - 2));
//System.out.println(total);
System.out.println(count);
}
System.out.println(count);
if(count == 4) {
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
System.out.println("sup sfkj" + savingList2.get(savingList2.size()-4));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText("-");
oth6.setText("-");
oth7.setText("-");
oth8.setText("-");
oth9.setText("-");
oth10.setText("-");
oth11.setText("-");
oth12.setText("-");
}
else if (count == 8){
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText(savingList2.get(savingList2.size()-8));
oth6.setText("$"+savingList2.get(savingList2.size()-7));
oth7.setText("$"+savingList2.get(savingList2.size()-6));
oth8.setText(savingList2.get(savingList2.size()-5));
oth9.setText("-");
oth10.setText("-");
oth11.setText("-");
oth12.setText("-");
}
else {
System.out.println("sup sfkj" + savingList2.get(savingList2.size()-4));
oth1.setText(savingList2.get(savingList2.size()-4));
oth2.setText("$"+savingList2.get(savingList2.size()-3));
oth3.setText("$"+savingList2.get(savingList2.size()-2));
oth4.setText(savingList2.get(savingList2.size()-1));
oth5.setText(savingList2.get(savingList2.size()-8));
oth6.setText("$"+savingList2.get(savingList2.size()-7));
oth7.setText("$"+savingList2.get(savingList2.size()-6));
oth8.setText(savingList2.get(savingList2.size()-5));
oth9.setText(savingList2.get(savingList2.size()-12));
oth10.setText("$"+savingList2.get(savingList2.size()-11));
oth11.setText("$"+savingList2.get(savingList2.size()-10));
oth12.setText(savingList2.get(savingList2.size()-9));
}
//othTot.setText("$"+String.valueOf(total));
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Add2Fragment extends Fragment {
static EditText spent,saved,coupons;
Button writeExcelButton;
Spinner spinner;
UpdateButton2 updateButton;
public interface UpdateButton2 {
public void onButtonClick2();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
updateButton = (UpdateButton2) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement UpdateFile");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add2_layout, container, false);
setSpinnerContent(view);
spent = (EditText) view.findViewById(R.id.text_spent2);
saved = (EditText) view.findViewById(R.id.text_saved2);
coupons = (EditText) view.findViewById(R.id.text_coupons2);
writeExcelButton = (Button) view.findViewById(R.id.button_addOth);
writeExcelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateFile();
buttonClicked(v);
}
});
return view;
}
private void setSpinnerContent (View view) {
//set spinner
}
public void UpdateFile() {
//write to file
}
public void buttonClicked(View v) {
updateButton.onButtonClick2();
}
}
在主活动中调用
updateTable()
和updateTable2()
函数的位置。我会编辑我的帖子,让它显示出来。我在XML中看不到任何按钮。你的钮扣在哪里?我也没有看到任何调用在代码中设置click侦听器。你确定你设置了侦听器吗?是的,我会在我的帖子中添加我的其他片段。