helmuthelmut2000
Goto Top

In einem ListView (Android Studio) bestimmte Werte farbig anzeigen

Hi,

Ich würde mal eure Hilfe gebrauchen.
Ich habe mir im Android Studio eine App erstellt, wo ich meine Heizungsdaten anzeigen lasse.
Die Werte stehen in einer mysql DB und ich habe es so weit, dass die mir in einer ListView angezeigt werden.
Leider bin ich ein Anfänger in der Android Entwicklung, aber mich interessiert das sehr.

Ich hole mir die Daten mit PHP aus der DB und die werden auch angezeigt.
Jetzt mein Problem.
Ich würde gerne bestimmte Werte mit z.B. Rotem Text anzeigen.
Wie mache ich das Richtig?
Mich würde interessieren wo man da die wahrscheinlich if Anweisung reinschreibt.


Meine XML Datei schaut so aus:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:orientation="vertical"
tools:context=".temperatur">

<TableRow
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#aacfef">

<TextView
android:id="@+id/col0"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Datum/Zeit"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:textSize="15dp" />

<TextView
android:id="@+id/col2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="Außen"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:textSize="15dp" />

<TextView
android:id="@+id/col3"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="WWas."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:textSize="15dp" />

</TableRow>

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="490dp">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/lv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="@drawable/tableheaderborder"
android:paddingLeft="3dp"
android:text="@string/col1Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />

<TextView
android:id="@+id/lv1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/tableheaderborder"
android:paddingLeft="3dp"
android:text="@string/col2Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />

<TextView
android:id="@+id/lv2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/tableheaderborder"
android:paddingLeft="3dp"
android:text="col3"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />

</LinearLayout>

</ScrollView>

</HorizontalScrollView>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="2dp">

<Button
android:id="@+id/button5"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="60Tg" />

<Button
android:id="@+id/button7"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Alles" />

<Button
android:id="@+id/button8"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="zurück" />

</TableRow>

</LinearLayout>


Und meine Java schaut so aus:

package temperatur.de.heizung;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.AsyncTask;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
    • Created by Abhishek Panwar on 7/14/2017.
    */

    public class fetchData_alles extends AsyncTask<Void,Void,Void> {
    String data ="";
    String data1 ="";
    String dataParsed = "";
    String dataParsed1 = "";
    String dataParsed2 = "";
    String singleParsed_datum ="";
    String singleParsed_temp_aussen ="";
    String singleParsed_temp_wwasser ="";
    String singleParsed_temp_kessel ="";

    public static int data2;

    @override
    protected Void doInBackground(Void... voids) {
    try {
    URL url = new URL("http://meineDaten.php");
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = httpURLConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    while(line != null){
    line = bufferedReader.readLine();
    data = data + line;
    }

    JSONArray JA = new JSONArray(data);
    for(int i =0 ;i <JA.length(); i++) {
    JSONObject JO = (JSONObject) JA.get(i);
    singleParsed_datum = "" + JO.get("datum") + "\n";
    singleParsed_temp_aussen = "" + JO.get("temp_aussen") + "" + "\n";
    singleParsed_temp_wwasser = "" + JO.get("temp_wwasser") + "" + "\n";
    singleParsed_temp_kessel = "kessel:" + JO.get("temp_kessel") + "" + "\n";

    dataParsed = dataParsed + singleParsed_datum;
    dataParsed1 = dataParsed1 + singleParsed_temp_aussen;
    dataParsed2 = dataParsed2 + singleParsed_temp_wwasser;
    }

    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (JSONException e) {
    e.printStackTrace();
    }
    return null;
    }

    @override
    protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    temperatur.data.setText(this.dataParsed);
    temperatur.data1.setText(this.dataParsed1);
    temperatur.data2.setText(this.dataParsed2);

    }

    }


    Und noch:


    package temperatur.de.heizung;

    import android.content.Intent;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;

    import java.util.List;

    public class temperatur extends AppCompatActivity {
    Button click5;
    Button click7;
    public static TextView data;
    public static TextView data1;
    public static TextView data2;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_temperatur);

    click5 = (Button) findViewById(R.id.button5);
    click7 = (Button) findViewById(R.id.button7);
    data = (TextView) findViewById(R.id.lv);
    data1 = (TextView) findViewById(R.id.lv1);
    data2 = (TextView) findViewById(R.id.lv2);

    click5.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
    fetchData process = new fetchData();
    process.execute();
    }
    });

    click7.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
    fetchData_alles process = new fetchData_alles();
    process.execute();
    }

    });

    Button btn8 = (Button) findViewById(R.id.button8);
    btn8.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
    Intent intent = new Intent(temperatur.this, MainStartActivity.class);
    startActivity(intent);
    }
    });
    }
    }


    Danke.

Content-ID: 374232

Url: https://administrator.de/contentid/374232

Ausgedruckt am: 25.11.2024 um 07:11 Uhr