Liebe Leute,
Ich kämpfe mit dem TableLayout in Android ein wenig.
Ich will ein Layout mit 4 Komponenten pro Zeile:
EditText (Uhrzeit) Spinner(dynamisch befüllt) EditText (nur Integer) Spinner(dynamisch befüllt) Button (zum Löschen der Zeile)
Anmerkung: das Uhrzeitfeld ist noch nicht im Quellcode
Der Minus-Button wird mir gar nicht angezeigt.
Das EditText Feld (nur Integer) wird zu klein angezeigt und setEms() oder setWidth() hat beides keine Veränderung.
Dann hätte ich eine Änderung frage:
Wie kann ich wenn ich im 1. Spinner etwas auswähle (auf onSelectItemListener()) den anderen Spinner-Inhalt ändern? Ich habe eine andere List<String> je nach dem was ich auswähle und will den Spinner-Inhalt des 2. Spinners dann neu befüllen.
Die Liste befüllt er mir natürlich aber wie kann ich das im Layout sichtbar ändern?
Mein zusätzliches Problem beim Löschen ist folgendes:
Ich weise den einzelnen Elementen, welche ich am Ende auslesen will, eine ID zusammengesetzt aus dem Rowcount und einer Zahl (3000 für EditText, 1000 für den 1.Spinner, usw.) zu.
Jedoch wenn ich lösche müsste ich ja den rowcount verringern, jedoch ist dann beim iterieren ein Fehler. habt ihr da eine Idee?
[XML]- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical">
- <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content">
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" androidrientation="vertical">
- <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
- <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
- <EditText android:id="@+id/editText1" android:layout_width="30dp" android:layout_height="wrap_content" android:ems="10" android:inputType="numberSigned">
<requestFocus />
</EditText>
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" androidrompt="@string/product_prompt" />
</TableRow>
</TableLayout>
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btnBerechnen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_berechnen" />
<Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_add" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>[/XML]
Ich kämpfe mit dem TableLayout in Android ein wenig.
Ich will ein Layout mit 4 Komponenten pro Zeile:
EditText (Uhrzeit) Spinner(dynamisch befüllt) EditText (nur Integer) Spinner(dynamisch befüllt) Button (zum Löschen der Zeile)
Anmerkung: das Uhrzeitfeld ist noch nicht im Quellcode
Der Minus-Button wird mir gar nicht angezeigt.
Das EditText Feld (nur Integer) wird zu klein angezeigt und setEms() oder setWidth() hat beides keine Veränderung.
Dann hätte ich eine Änderung frage:
Wie kann ich wenn ich im 1. Spinner etwas auswähle (auf onSelectItemListener()) den anderen Spinner-Inhalt ändern? Ich habe eine andere List<String> je nach dem was ich auswähle und will den Spinner-Inhalt des 2. Spinners dann neu befüllen.
Die Liste befüllt er mir natürlich aber wie kann ich das im Layout sichtbar ändern?
Mein zusätzliches Problem beim Löschen ist folgendes:
Ich weise den einzelnen Elementen, welche ich am Ende auslesen will, eine ID zusammengesetzt aus dem Rowcount und einer Zahl (3000 für EditText, 1000 für den 1.Spinner, usw.) zu.
Jedoch wenn ich lösche müsste ich ja den rowcount verringern, jedoch ist dann beim iterieren ein Fehler. habt ihr da eine Idee?
[XML]- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical">
- <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content">
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" androidrientation="vertical">
- <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
- <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
- <EditText android:id="@+id/editText1" android:layout_width="30dp" android:layout_height="wrap_content" android:ems="10" android:inputType="numberSigned">
<requestFocus />
</EditText>
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" androidrompt="@string/product_prompt" />
</TableRow>
</TableLayout>
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btnBerechnen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_berechnen" />
<Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_add" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>[/XML]
Java:
package at.gemuesebauauer.ernterechner;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
@SuppressLint({ "ParserError", "ParserError", "ParserError", "ParserError" }) public class MainActivity extends Activity {
private TableLayout tabLay;
private List<Integer> amount= new ArrayList<Integer>();
private List<String> productsforspinner= new ArrayList<String>();
private List<String> client= new ArrayList<String>();
private List<String> manufacture= new ArrayList<String>();
private File clients;
private File filemanufactures;
private TextFileEdit TextFileEdit = new TextFileEdit();
private int numberofrows;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberofrows=0;
clients=new File (MainActivity.this.getFilesDir().getPath().toString() + "/kunden.txt");
filemanufactures= new File (MainActivity.this.getFilesDir().getPath().toString() + "/erzeugnisse.txt");
TextFileEdit.writeToSDCard(clients, "Herzog");
TextFileEdit.writeToSDCard(clients, "Marksteiner");
TextFileEdit.writeToSDCard(clients, "LGV");
try {
client=TextFileEdit.loadTextFile(clients);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Kunde kunde1= new Kunde (client.get(0), MainActivity.this);
List<Produkt> tmp_clientproduktliste =TextFileEdit.getProduktliste(kunde1.getFile(), filemanufactures);
for(int i=0;i<tmp_clientproduktliste.size();i++){
productsforspinner.add(tmp_clientproduktliste.get(i).getProduktname());
}
createRowsMain(5);
TextFileEdit.writeToSDCard(filemanufactures,"01 Brokkoli PH");
TextFileEdit.writeToSDCard(filemanufactures,"02 Brokkoli PK");
TextFileEdit.writeToSDCard(filemanufactures,"03 Salat PH");
TextFileEdit.writeToSDCard(kunde1.getFile(),"Produkt 1");
TextFileEdit.writeToSDCard(kunde1.getFile(),"030201030301");
TextFileEdit.writeToSDCard(kunde1.getFile(),"Produkt 2");
TextFileEdit.writeToSDCard(kunde1.getFile(),"040310020501");
TextFileEdit.writeToSDCard(kunde1.getFile(),"Produkt 3");
TextFileEdit.writeToSDCard(kunde1.getFile(),"05031002");
TextFileEdit.writeToSDCard(kunde1.getFile(),"Produkt 4");
TextFileEdit.writeToSDCard(kunde1.getFile(),"1003");
tabLay = (TableLayout)findViewById(R.id.tableLayout1);
tabLay.removeAllViews();
//createRow();
Button buBerechnen = (Button)findViewById(R.id.btnBerechnen);
Button buAdd = (Button)findViewById(R.id.btnAdd);
buBerechnen.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bestellliste ernteliste= new Bestellliste("Bestellliste 1");
String product="";
int amounte=0;
for (int y=1;y<=numberofrows;y++) {
EditText text = (EditText) findViewById(y+3000);
Spinner sp_product = (Spinner) findViewById(y+1000);
Spinner sp_client = (Spinner) findViewById(y+2000);
if (text.getText().toString().equals("")) {
amounte=0;
}
else {
amounte=Integer.valueOf(text.getText().toString());
}
product=sp_product.getSelectedItem().toString();
String clientname=sp_client.getSelectedItem().toString();
ernteliste.addClientToBestellliste(clientname);
ernteliste.getBestelllistepositionen().
get(clientname).addlistManufactures(product, amounte);
}
for(HashMap.Entry<String, Bestelllisteposition> e : ernteliste.getBestelllistepositionen().entrySet()){
String s = e.getKey();
Bestelllisteposition d = e.getValue();
String product1="";
String outtext="";
int amount2=0;
for(HashMap.Entry<String, Integer> f : ernteliste.getBestelllistepositionen().get(s).getlistManufactures().entrySet()){
product1 = f.getKey();
amount2 = f.getValue();
outtext=outtext+"Artikel "+ product1 +" BINDE Menge "+amount2+"\n";
}
Toast.makeText(MainActivity.this,
"Kunde "+ d.getClient() + "\n"+
outtext
, Toast.LENGTH_LONG).show();
} //end of for ernteliste
} //end of for erntelistenposition
});
buAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
createRowsMain(1);
}
});
} // end of onCreate
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
public Spinner newSpinner(List<String> list) {
final Spinner sp1= new Spinner(MainActivity.this);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
// TODO Auto-generated method stub
// Toast.makeText(getBaseContext(),list.get(position), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return sp1;
} // enf of newSpinner()
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
data.getExtras().get("");
}
}
public void createRowsMain(int rowcount){
for (int i=0; i<rowcount;i++) {
numberofrows++;
TableRow tr;
EditText editText;
Spinner sp_product;
Spinner sp_client;
Button minus;
sp_product = newSpinner(productsforspinner);
sp_product.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
// TODO Auto-generated method stub
Kunde kunde_tmp= new Kunde (arg0.getItemAtPosition(position).toString(), MainActivity.this);
List<Produkt> tmp_clientproduktliste =TextFileEdit.getProduktliste(kunde_tmp.getFile(), filemanufactures);
for(int i=0;i<tmp_clientproduktliste.size();i++){
productsforspinner.add(tmp_clientproduktliste.get(i).getProduktname());
}
/*
if(arg0.getItemAtPosition(position).toString().contains("MIXXX")){
Intent i = new Intent(getApplicationContext(), Mix_Salat_Activity.class);
i.putExtra("selected_item", arg0.getItemAtPosition(position).toString());
startActivityForResult(i, 100);
}*/
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
minus = new Button(MainActivity.this);
sp_product.setId(numberofrows+1000);
sp_client = newSpinner(client);
sp_client.setId(numberofrows+2000);
editText = new EditText(MainActivity.this);
/* Create a new row to be added. */
editText.setId(numberofrows+3000);
//editText.setWidth(25);
editText.setEms(65);
//editText.setHeight(20);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
minus.setId(numberofrows+4000);
minus.setText("-");
minus.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
minus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// final TableRow tr1=tr;
// tabLay.removeView(tr1);
tabLay.removeViewAt(numberofrows);
}
});
tr = new TableRow(MainActivity.this);
tr.addView(sp_client);
tr.addView(editText);
tr.addView(sp_product);
tr.addView(minus);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setId(numberofrows+5000);
tabLay.addView(tr,new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem1:
setContentView(R.layout.activity_main);
Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
break;
case R.id.menuitem2:
Intent newProductActivity = new Intent(getApplicationContext(), NewProductActivity.class);
/*
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("email", inputEmail.getText().toString());
Log.e("n", inputName.getText()+"."+ inputEmail.getText());
*/
startActivity(newProductActivity);
Toast.makeText(this, "Neues Produkt!", Toast.LENGTH_LONG).show();
break;
case R.id.menuitem3:
setContentView(R.layout.new_client);
Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
case R.id.menuitem4:
setContentView(R.layout.activity_main);
Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
/*
public void printTheDoc() {
try {
// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream("filename.gif"));
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
// Monitor print job events
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the input stream
is.close();
} catch (PrintException e) {
} catch (IOException e) {
}
}*/
}