我只是想在listview中存储一个数据,然后用textview显示它,我发现了问题,实际上是这个问题:
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
行动: 1 - 如果我删除第一个或第二个“\ n”,列表视图表示没有任何数据要显示。 2-如果我让两个“\ n”,则不显示value1和value2!
显示数据的类的oncreate函数:
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);
L.setTextFilterEnabled(true);
点击主类中的计算按钮时的代码:
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 = "";
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>
答案 0 :(得分:0)
您是否考虑通过包含两个TextView的适配器创建一个View来正确显示它?这是最好的方法。