I just want to store a data in a listview then displaying it with a textview, i identified the problem, it's in fact this ligne:
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
Operations made: 1-if i delete the first or the second "\n" the listview indicates that there is not any datas to display. 2- if i let the two "\n" then the value1 and value2 are not displayed !
The oncreate function of the class that show the datas:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.histo);
context = getApplicationContext();
activity = this;
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
updatelv(this);
ListView L = (ListView) findViewById(R.id.lv);
开发者_Python百科 L.setTextFilterEnabled(true);
the code when tapping the calculate button in the main class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
Toast.makeText(carburant.this, "Bienvenue!", Toast.LENGTH_LONG).show();
data = "";
final EditText vol;
final EditText kil;
final EditText cons;
vol = (EditText) findViewById(R.id.volume2);
kil= (EditText) findViewById(R.id.kilometrage2);
cons= (EditText) findViewById(R.id.consom2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value1 = (((EditText) findViewById(R.id.volume2))
.getText()).toString();
String value2 = (((EditText) findViewById(R.id.kilometrage2)).getText())
.toString();
if (value1 .equalsIgnoreCase("") && value2.equalsIgnoreCase(""))
{
Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_SHORT).show();
}
else{
float q1=Float.parseFloat(vol.getText().toString());
float q2=Float.parseFloat(kil.getText().toString());
float x=((q1 / q2)* 100);
String y= Float.toString(x);
cons.setText(y);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String date = format.format(new Date());
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context, fileDir + fileName, data);
data = "";
The list_item XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
Histo.XML:
<?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" android:layout_weight="1"
android:id="@+id/ViewHisto">
<ListView android:id="@+id/lv" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
Main.XML:
<LinearLayout android:id="@+id/layoutPrincipal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/volume1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Volume(en Litres):">
</TextView>
<EditText android:id="@+id/volume2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="@string/vol"
>
</EditText>
<TextView android:id="@+id/kilometrage1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Kilometrage(en Kilomètres) :">
</TextView>
<EditText android:id="@+id/kilometrage2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="@string/kil">
</EditText>
<TextView android:id="@+id/consom1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Consommation moyenne(en Litres/100KM) :">
</TextView>
<EditText android:id="@+id/consom2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="@string/cons">
</EditText>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Calculer"
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Reset"
android:id="@+id/button2"
android:layout_marginLeft="280px"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Did you consider creating a View to through an Adapter containing two TextViews to you display it correctly? It's the best approach.
精彩评论