vendredi 14 août 2015

Add to favourite random strings and displaying them in Viewlist

So i am begginer android developer and i'm currently working on app that display to user random quotes. So i decided to expand functionality of app and add simply "add to favourite button" which saves currently displayed quote to txt files and if user click another button, list of liked quotes is display in ListView. I wrote the code based on thousands of tutorials and part responsible for saving seems to work but when i activate "favourite quotes" activity with "read saved data" function i see only weird "android.support.v7..." thing. Below is my code:

package com.example.dominik.moodq;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;

public class SecondActivity extends AppCompatActivity {

    Typeface customfont;
    TextView tx;
    TextView popup;
    TextView namev;
    ImageButton btn1, btn2, btn4, btn5;



    private static final Random RANDOM = new Random();

    private static final String[] HELLOS = {
            "Lovers who love truly do not write down their happiness",
            "Whatever the mind of man can conceive and believe, it can achieve",
            "Life isn’t about getting and having, it’s about giving and being.",
            "We become what we think about",
            "The two most important days in your life are the day you are born and the day you find out why.",
            "Fall seven times and stand up eight",
            "When I let go of what I am, I become what I might be"
    };

    private void sayHello() {
        // Select a random hello.
        final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        final Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
        popup = (TextView) findViewById(R.id.textView2);
        popup.startAnimation(animationFadeOut);
        namev.startAnimation(animationFadeOut);
        int helloLength = HELLOS.length;
        String hello = HELLOS[RANDOM.nextInt(helloLength)];
        Intent intent = getIntent();
        String edtext = intent.getStringExtra("name");
        popup.setText (hello);
        popup.setTypeface(customfont);
        namev.setTypeface(customfont);
        namev.setText(edtext);
        popup.startAnimation(animationFadeIn);
        namev.startAnimation(animationFadeIn);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        customfont = Typeface.createFromAsset(getAssets(), "fonts/customfont.ttf");
        tx = (TextView) findViewById(R.id.text);
        tx.setTypeface(customfont);
        namev = (TextView) findViewById(R.id.textView3);
        btn1 = (ImageButton) findViewById(R.id.imageButton2);
        btn2 = (ImageButton) findViewById(R.id.imageButton3);
        btn5 = (ImageButton) findViewById(R.id.imageButton5);
        btn4 = (ImageButton) findViewById(R.id.imageButton4);

        sayHello();

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sayHello();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sayHello();
            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                write();
                Toast.makeText(getApplicationContext(), "Quote saved to favourites",
                        Toast.LENGTH_SHORT).show();
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, FavActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.transition1, R.anim.transition2);
            }
        });
    }


    public void write () {
        String fileName = "MyFile";
        String content = popup.toString();

        FileOutputStream outputStream = null;
        try {
            outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
            outputStream.write(content.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And next activity should display list of favourite quotes, but it does nothing, even doesn't show any critical error. Only in place of first ListView item's row displays "android.support.v7..."

public class FavActivity extends AppCompatActivity {

ListView lista;
ArrayAdapter<String> adapter ;
String favquote;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fav);
    favquote = readFromFile();
    lista = (ListView) findViewById(R.id.listView);

    String cars[] = {favquote};

    ArrayList<String> carL = new ArrayList<String>();
    carL.addAll( Arrays.asList(cars) );

    adapter = new ArrayAdapter<String>(this, R.layout.lisv_view_layout, carL);

    lista.setAdapter(adapter);

}

private String readFromFile() {

    String ret = "";

    try {
        InputStream inputStream = openFileInput("MyFile");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}

}

I don't know what I'm doing wrong. Probably it's something stupid but, I've already searched the web and found nothing useful :<




Aucun commentaire:

Enregistrer un commentaire