1

I wrote an android app in Eclipse and I show some information to user from my database with using xml.Here is my xml and related code part.

<?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="wrap_content">
    <TextView android:text="get info from db" 
        android:id="@+id/dbView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        </TextView>
</LinearLayout>

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        TextView history=(TextView) findViewById(R.id.dbView);
        Database myDB=new Database(this);
        myDB.open();
        String data=myDB.getData();
        myDB.closeDB();
        history.setText(data);
    }

public String getData() {

            String attributes[] = new String[]{Key_ID,Key_Name,Key_Cost,Key_Type,Key_Date};

            Cursor cursor=myDatabase.query(DB_TABLE,attributes,null,null,null,null,Key_Name+ " DESC ");
            String result="";
            int Row = cursor.getColumnIndex(Key_ID);
            int Nameof = cursor.getColumnIndex(Key_Name);
            int Costof = cursor.getColumnIndex(Key_Cost);
            int Typeof = cursor.getColumnIndex(Key_Type);
            int Dateof = cursor.getColumnIndex(Key_Date);
            for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                result = result + cursor.getString(Row)+" "+cursor.getString(Nameof)+" "+cursor.getString(Costof)+" "+cursor.getString(Typeof)+" "+cursor.getString(Dateof)+"\n" ;
            }
            return result;
        }

I tried to minimize the density of code as much as possible in this example. So if you need extra info about code I can add it. What I want to accomplish is following. When I write tuples one by one in each row I want to say that if Typeof="x" then write this tuple with green color else write this tuple with red color. How can I do that?

  • in the codepart where you bind your view to the data, where you fill your view – Henning Luther Jul 02 '16 at 07:35
  • The for loop iterates through each element in table and writes the output to android(in the getData method.)Then it is used in onCreate method.What I need is something like this. If cursor.getString(Typeof)== "something" then make this line green. – Felipe Hield Jul 02 '16 at 07:40
  • 1
    Duplicate : http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview – Henning Luther Jul 02 '16 at 08:22

0 Answers0