Cannot resolve `FileUtils` character in Codepath` Todo App` tutorial (Android)

I am trying to follow an application tutorial Todofrom Codepath ( https://guides.codepath.com ).

Source: https://guides.codepath.com/android/Basic-Todo-App-Tutorial

As far as I can tell, I followed the tutorial, but I get the following problem.

Cannot resolve symbol `FileUtils`

MainActivity.Java

package com.codepath.simpletodo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView; 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;

    private void readItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        } catch (IOException e) {
            items = new ArrayList<String>();
        }
    }

    private void writeItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, items);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvItems = (ListView) findViewById(R.id.lvItems);
        items = new ArrayList<String>();
        itemsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        lvItems.setAdapter(itemsAdapter);
        items.add("Default item");
        items.add("Another default item");
        items.add("The third default item");
        // setup remove listener method call
        setupListViewListener();
    }

    // Attaches a long click listener to the listview
    private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View item, int pos, long id) {
                        // Remove item within array at position
                        items.remove(pos);
                        // Refresh the adapter
                        itemsAdapter.notifyDataSetChanged();
                        // Return true consumes the long click event (marks it handled)
                        return true;
                    }
                });
    }

    public void onAddItem(View v) {
        EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
        String itemText = etNewItem.getText().toString();
        itemsAdapter.add(itemText);
        etNewItem.setText("");
    }
}

activity_mail.xml

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lvItems"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/btnAddItem" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/etNewItem"
    android:layout_alignTop="@+id/btnAddItem"
    android:hint="Enter a new item"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/btnAddItem"
    android:layout_toStartOf="@+id/btnAddItem"
    android:layout_alignParentBottom="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Item"
    android:id="@+id/btnAddItem"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="onAddItem"
    />

I am extremely new to Android and will be so happy just to get it to work, because I don’t have enough background to hope for debugging.

Any guidance would be greatly appreciated.

UPDATE 1

application /build.gradle

Original format

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.codepath.simpletodo"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
}

I have tried various combinations, including 'org.apache.commons:commons-io:1.3.2'.

  • Replacement 'com.android.support:appcompat-v7:24.0.0'for'org.apache.commons:commons-io:1.3.2'
  • Place both on one line: compile 'com.android.support:appcompat-v7:24.0.0', 'org.apache.commons:commons-io:1.3.2'.
  • : compile 'com.android.support:appcompat-v7:24.0.0' compile 'org.apache.commons:commons-io:1.3.2'.

Gradle project sync .

Error:(24, 13) Failed to resolve: compile org.apache.commons:commons-io:1.3.2

2

.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
}
+4
4

implementation 'org.apache.commons:commons-io:1.3.2' FileUtils. .

+16

, build.gradle

dependencies {
    compile 'org.apache.commons:commons-io:1.3.2'
}

build.gradle

+5

(3+) dex

dependecies {    'commons-io: commons-io: 2.4' }

+2
source

Version: Android Studio 3.4.1

You can click "Project Structure" and add dependencies

Then you will see that the file "build.gradle" has an implementation string.

Or you just add the implementation string yourself,

after you can click "Sync project with Gradle files", everything is fine too.

Finally you must

import org.apache.commons.io.FileUtils;

[ enter image description here[enter image description here

0
source

Source: https://habr.com/ru/post/1648125/


All Articles