1

I'm a total beginner in programming and I need help in my second project which is a simple dictionary.

Shortly, at first I wanted to create an Array of Strings and display it by using a ListView. I had (and now I still have) list_item layout which contains two TextViews (one holds a word and second one its translation) and ListView in another XML. As I was learning from very clear tutorial, everything was working perfectly, nevertheless, I've decided to enlarge my dictionary. For this reason, instead of declaring all the words within the method, I wanted to load them from a txt file. I followed many tutorials on how to achieve this goal, however, I wasn't able to transform my code correctly. This is my code:

    public class MainActivity extends AppCompatActivity {
public static void main(String[] args) throws FileNotFoundException {
    ArrayList<AndroidFlavor> androidFlavors = new ArrayList<AndroidFlavor>();
    BufferedReader file = new BufferedReader(new FileReader("words.txt"));

    try {
        StringBuilder builder = new StringBuilder();
        String line;

        while ((line = file.readLine()) != null) {
            String token[] = line.split("\\|");
            String mPolish = token[0];
            String mEnglish = token[1];
            AndroidFlavor androidFlavor = new AndroidFlavor(mPolish, mEnglish);
            androidFlavors.add(androidFlavor);
        }
        for (int i = 0; i < androidFlavors.size(); i++) {
                System.out.println(androidFlavors.get(i).getPolish() + " "
                        + androidFlavors.get(i).getEnglish() + " ");


            }
        file.close();
    } catch (Exception e) {
    }}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

public void button1(View view) {
    setContentView(R.layout.free);
    AndroidFlavorAdapter flavorAdapter = new AndroidFlavorAdapter(this, androidFlavors);
    ListView listView = (ListView) findViewById(R.id.lista);
    listView.setAdapter(flavorAdapter);
}

I also have this:

   public class AndroidFlavor {

// String for polish words
private String mPolish;
// String for English translations
private String mEnglish;



// New AndroidFlavor object.

public AndroidFlavor(String vPolish, String vEnglish) {
    mPolish = vPolish;
    mEnglish = vEnglish;
}

//Get Polish word
public String getPolish() {
    return mPolish;
}

//Get English word
public String getEnglish() {
    return mEnglish;
}

}

And finally this:

   public class AndroidFlavorAdapter extends ArrayAdapter<AndroidFlavor> {

private static final String LOG_TAG = AndroidFlavorAdapter.class.getSimpleName();
    super(context, 0, androidFlavors);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }
    AndroidFlavor currentAndroidFlavor = getItem(position);
    TextView nameTextView = (TextView) listItemView.findViewById(R.id.polish);

    nameTextView.setText(currentAndroidFlavor.getPolish());
    TextView numberTextView = (TextView) listItemView.findViewById(R.id.english);
    numberTextView.setText(currentAndroidFlavor.getEnglish());


    return listItemView;

}
}

I have the following question: after clicking the button1, how can I open my ListView based on array of Strings from txt file? I'd like to obtain the same effect (i.e. display ListView from 2 TextViews) just like before I added txt file to my project.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Karina
  • 11
  • 4
  • Are you able to read the file at all? I don't think `new FileReader("words.txt")` should work. – OneCricketeer Sep 25 '16 at 12:11
  • Unfortunately, I'm not. I will try to change this line of code to something else, however I think that the main problem is with button1. – Karina Sep 25 '16 at 18:46
  • Well, where is the file on your device? If you make an assets folder, you could reference this question. http://stackoverflow.com/questions/9544737/read-file-from-assets – OneCricketeer Sep 25 '16 at 21:44

0 Answers0