我有两个片段应该更新第三个片段,用TableLayouts
linearlayout
更新TextViews
这是我的第三个片段:
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();
}
}
}
第一个更新方法有效,表格更新,但第二个更新方法没有显示任何内容。我测试了文件以查看它是否正常工作,并且它的编写和读取工作正常。
以下是带有表格的linearlayout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow5"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="serif"
android:textSize="18sp"
android:text="Grocery Savings"
android:gravity="center"
android:layout_span="6"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/store0"
android:text="Store"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/spent0"
android:text="Spent"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/saved0"
android:text="Saved"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/coupons0"
android:text="Coupons"
android:textStyle="bold"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/grocstore1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocspent1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocsaved1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/groccoupons1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/grocstore2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocspent2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocsaved2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/groccoupons2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/grocstore3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocspent3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/grocsaved3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/groccoupons3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow05"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="@+id/textView09"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="serif"
android:textSize="18sp"
android:text="Other Savings"
android:gravity="center"
android:layout_span="6"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow01"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/store00"
android:text="Store"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/spent00"
android:text="Spent"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/saved00"
android:text="Saved"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/coupons00"
android:text="Coupons"
android:textStyle="bold"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow02"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/othstore1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othspent1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othsaved1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othcoupons1"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow03"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/othstore2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othspent2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othsaved2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othcoupons2"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow04"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/othstore3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othspent3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othsaved3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
<TextView
android:id="@+id/othcoupons3"
android:textStyle="bold"
android:gravity="center_horizontal"></TextView>
</TableRow>
</TableLayout>
<!--<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grocery Savings:"
/>
<TextView
android:id="@+id/grocTotal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other Savings:"
/>
<TextView
android:id="@+id/othTotal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>-->
</LinearLayout>
我已经检查了两三次语法并且找不到任何错误,所以我不知道为什么这不起作用。任何帮助将不胜感激。
修改
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
我在UpdateTable2()方法中放置System.out.println(oth1.getText().toString());
以查看该字符串是否已放置在那里,它是,因此在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 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();
}
}