我不知道为什么,但我有两个content_main:content_main和content_main2,在这两个content_main我都有按钮;我只想要一个按钮。
希望你能帮助我!!!!!!!
提前谢谢!!!!!!
这是我的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">-->
<!-- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/result2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:capitalize="characters"
android:layout_centerVertical="false"
android:layout_marginTop="30dp"
android:textSize="30dp"/>
<TextView
android:id="@+id/quantità2"
android:layout_width="100dp"
android:layout_height="500dp"
android:layout_centerVertical="false"
android:textSize="30dp"
android:layout_marginTop="30dp" />
</LinearLayout>-->
<LinearLayout
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- <TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="false"
android:textSize="30dp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultpizze"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"/>-->
<TextView
android:id="@+id/result2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultbirre"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"/>
<!-- <TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultpizze"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"/>-->
</LinearLayout>
<!-- <LinearLayout
android:id="@+id/content_main3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="false"
android:textSize="30dp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultpizze"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"/>
</LinearLayout>-->
<LinearLayout
android:id="@+id/content_main2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultpizze"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"/>
</LinearLayout>
<!--</ScrollView>-->
<Button
android:id="@+id/newbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_gravity="center_horizontal"
android:onClick="sendemail"
android:text="Manda Email">
</Button>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我的列表片段:
public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;
private static Context context = null;
private static FragmentActivity mInstance;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//super.onDestroy();
SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
context = getActivity();
mInstance= getActivity();
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);
}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView)getView().findViewById(R.id.listview);
displayPlanetList();
}
private void displayPlanetList() {
planetList = new ArrayList<Planet>();
planetList.add(new Planet("Margherita", 6, "€",1));
planetList.add(new Planet("Diavola", 7,"€",2));
planetList.add(new Planet("Bufalina", 5,"€",3));
planetList.add(new Planet("Marinara", 5, "€",4));
planetList.add(new Planet("Viennese", 4, "€", 5));
plAdapter = new PlanetAdapter(planetList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
};
lv.setAdapter(plAdapter);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
*//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//*
}*/
}
/*
public static void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
String result2 = "";
int totalAmount2=0;
String a="";
String z="";
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
result2 += "n" + p.getName()+" "+p.getDistance()+"€"+"q.tà :"+p.getQuantità();
int quantitaInt= Integer.parseInt(p.getQuantità() );
totalAmount2+=p.getDistance() * quantitaInt;
//z=String.valueOf(totalAmount2);
}
int totale=totalAmount+totalAmount2;
z=String.valueOf(totale);
}
Toast.makeText(context, result2 + "n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), result2 + "n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
Bundle bun2 = new Bundle();
bun2.putString("scelta", result2);
ThreeFragment fgsearch2 = new ThreeFragment();
fgsearch2.setArguments(bun2);
android.support.v4.app.FragmentTransaction transaction2 = mInstance.getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.content_main, fgsearch2);
transaction2.commit();
*/
/*
}
*/
public static void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
String result2 = "";
int totalAmount2=0;
String a="";
String z="";
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
result += "n" + p.getName()+" "+p.getDistance()+"€"+"q.tà :"+p.getQuantità();
int quantitaInt= Integer.parseInt(p.getQuantità() );
totalAmount2+=p.getDistance() * quantitaInt;
z=String.valueOf(totalAmount2);
}
}
Toast.makeText(context, result + "n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), result2 + "n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
Bundle bun = new Bundle();
bun.putString("scelta4", result);
bun.putString("scelta5", z);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = mInstance.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main, fgsearch);
transaction.replace(R.id.content_main, fgsearch);
transaction.commit();
/*Bundle bun = new Bundle();
// bun.putString("totalepizze",z);
bun.putInt("totalepizze",totalAmount2);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = mInstance.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();*/
/*
Bundle bun = new Bundle();
bun.putString("scelta", result2);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();
*/
}
}
三个片段:
public class ThreeFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv2;
ArrayList<Planet> planetList;
ListView lv;
ArrayList<Birra> birraList;
BirraAdapter biAdapter;
PlanetAdapter plAdapter;
Planet p;
String myInt="";
PlanetAdapter.PlanetHolder holder;
public ThreeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyListFragment mlf=new MyListFragment();
mlf.showResult(v);
// MyListFragment.showResult(v);
showResult2(v);
}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv2 = (ListView) getView().findViewById(R.id.listview2);
displayBirraList();
}
private void displayBirraList() {
birraList = new ArrayList<Birra>();
birraList.add(new Birra("Paulaner", 6, "€"));
birraList.add(new Birra("Forst", 7, "€"));
birraList.add(new Birra("Peroni", 5, "€"));
birraList.add(new Birra("Corona", 5, "€"));
birraList.add(new Birra("Nastro Azzurro", 4, "€"));
biAdapter = new BirraAdapter(birraList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv2.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Birra b = birraList.get(pos);
b.setSelected(isChecked);
/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
};
lv2.setAdapter(biAdapter);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
*//**//**//**//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
}
*/
}
public void showResult2(View v) {
String result = "Selected Product are :";
String a="";
int totalAmount = 0;
// String a = "";
for (Birra b : biAdapter.getBox()) {
if (b.selected) {
result += "n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
try {
int quantitaInt = Integer.parseInt(b.getQuantità());
totalAmount += b.distance * quantitaInt;
a=String.valueOf(totalAmount);
Bundle bun2 = new Bundle();
bun2.putString("scelta2", result);
bun2.putString("scelta3", a);
TwoFragment fgsearch2 = new TwoFragment();
fgsearch2.setArguments(bun2);
android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.content_main2, fgsearch2);
transaction2.replace(R.id.content_main2, fgsearch2);
transaction2.commit();
//android.support.v4.app.FragmentTransaction transaction3 = getActivity().getSupportFragmentManager().beginTransaction();
// transaction2.replace(R.id.content_main2, fgsearch2);
// transaction2.commit();
/* Bundle bun3 = new Bundle();
bun3.putString("scelta3", a);
TwoFragment fgsearch3 = new TwoFragment();
fgsearch3.setArguments(bun3);
android.support.v4.app.FragmentTransaction transaction3 = getActivity().getSupportFragmentManager().beginTransaction();
transaction3.replace(R.id.content_main2, fgsearch3);
transaction3.commit();*/
}catch(NumberFormatException exc){
System.out.println(exc);
Toast.makeText(getActivity(), "inserisci una quantita' a ""+b.name, Toast.LENGTH_LONG).show();
//System.out.println("inserisci una quantita' a %s"+b.name);
}
}
}
/* for (Planet p : plAdapter.getBox()) {
if (p.isSelected()) {
result += "n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità();
int quantitaInt = Integer.parseInt(p.getQuantità());
//totalAmount2+=p.distance * quantitaInt;
//z=String.valueOf(totalAmount2);
}
}*/
Toast.makeText(getActivity(), result + "n" +myInt + "n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
//Bundle bun2 = new Bundle();
// bun2.putString("scelta2", result);
/* TwoFragment fgsearch2 = new TwoFragment();
fgsearch2.setArguments(bun2);
android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.content_main, fgsearch2);
transaction2.commit();*/
/* TwoFragment fgsearch2=TwoFragment.getInstance(result);
fgsearch2.setArguments(bun2);
android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.content_main, fgsearch2);
transaction2.commit();
*/
/* Bundle bun = new Bundle();
bun.putInt("totalebirre", totalAmount);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();
*/
}
}
两个片段:
public class TwoFragment extends android.support.v4.app.Fragment {
private View v;
Intent chooser = null;
String myInt = "";
String ciao = "";
private String string = "";
private String string3 = "";
private String string4 = "";
private String string5 = "";
int stringbirre=0;
int stringpizze=0;
int tot=0;
private ProgressDialog processingDialog;
private MakePDF mProgressbarAsync;
private ProgressBar pdfProgress;
private int mProgressStatus = 0;
public TwoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
processingDialog = new ProgressDialog(getContext());
processingDialog.setCancelable(true);
processingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
processingDialog.setMessage("Work in Progress ...");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
Button mButton = (Button) rootView.findViewById(R.id.newbutton);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//sendemail();
// pdfProgress.setVisibility(View.GONE);
processingDialog.show();
mProgressbarAsync=new MakePDF();
mProgressbarAsync.execute();
}
});
TextView titolo3 = (TextView) rootView.findViewById(R.id.result);
//TextView titolo2 = (TextView) rootView.findViewById(R.id.result2);
TextView titolo4 = (TextView) rootView.findViewById(R.id.resultpizze);
TextView titolo5 = (TextView) rootView.findViewById(R.id.result2);
TextView titolo6 = (TextView) rootView.findViewById(R.id.resultbirre);
pdfProgress = (ProgressBar) rootView.findViewById(R.id.progressbar);
pdfProgress.setVisibility(View.GONE);
Bundle bundle = getArguments();
if (bundle != null) {
string = bundle.getString("scelta4");
titolo3.setText(string);
string3 = bundle.getString("scelta5");
titolo4.setText(string3);
}
Bundle bundle2 = getArguments();
if (bundle2 != null) {
string4 = bundle2.getString("scelta2");
titolo5.setText(string4);
string5 = bundle2.getString("scelta3");
titolo6.setText(string5);
}
return rootView;
}
public void createPDF() {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/droidText";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "salve.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
doc.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.androtuto);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
doc.add(myImg);
Paragraph p1 = new Paragraph(string+string3);
Font paraFont = new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
doc.add(p1);
Paragraph p2 = new Paragraph(string4+string5);
Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_CENTER);
p2.setFont(paraFont2);
doc.add(p2);
stream = new ByteArrayOutputStream();
bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
doc.add(myImg);
Phrase footerText = new Phrase("Pied de page ");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de.getMessage());
} catch (IOException e) {
Log.e("PDFCreator", "DocumentException:" + e.getMessage());
} finally {
doc.close();
}
}
public void viewPDF() {
String path = "/sdcard/droidText/salve.pdf";
File targetFile = new File(path);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
startActivity(intent);
}
private class MakePDF extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressStatus = 0;
}
@Override
protected Void doInBackground(Void...params) {
while(mProgressStatus<100){
try{
mProgressStatus++;
publishProgress(mProgressStatus);
Thread.sleep(100);
}catch(Exception e){
Log.d("Exception", e.toString());
}
}
createPDF();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
processingDialog.setProgress(mProgressStatus);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
processingDialog.dismiss();
viewPDF();
}
}
}
如果我将两个变量放入一个content_main中,Titolo3 和 titolo4 为空(其中还有 titolo5 和 titolo6)......我不知道为什么。。。。
试试这个 -
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/result2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultbirre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/content_main2"
android:layout_below="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="0dp"/>
<TextView
android:id="@+id/resultpizze"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
<Button
android:id="@+id/newbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_gravity="center_horizontal"
android:onClick="sendemail"
android:text="Manda Email"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>