Beranda > Android, Android Studio, Pemrograman > Membaca Data dari File di Assets dan Memasukkan Ke Spinner – Android Studio

Membaca Data dari File di Assets dan Memasukkan Ke Spinner – Android Studio

logo android studio

read file

Sekarang kita akan mencoba memasukkan isi dari sebuat file text kedalam spinner.

File yang diperlukan:

1. MainActivity.java

2. activity_main.xml

3. dataoperator.txt

4. AndroidManifest.xml

Pertama buat file MainActivity.java, yang berisi:

package com.wordpress.yopiardinal.readandwritefile;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

public class MainActivity extends ActionBarActivity{
    EditText et;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //et = (EditText) findViewById(R.id.txtData);
    }
            public void getdata(View v)
             {
                try
                {
                 AssetManager am= getAssets();
                 InputStream is= am.open("dataoperator.txt");
                    Vector<String>str=new Vector<String>();

                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String st="";
                    StringBuilder sb =new StringBuilder();
                    String line = br.readLine();
                    int index = 0;

                    while (line != null) {

                        str.add(line);
                        line = br.readLine();
                    }

                    Spinner spinner = (Spinner) findViewById(R.id.spinner);
                    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, str);

                    spinner.setAdapter(adapter);

                 }catch (IOException ep) {

                }

    }// onCreate
}

Kedua, buat file activity_main.xml, yang berisi:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="  Pilih Data Operator :"
        android:id="@+id/textView" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_gravity="center_horizontal" />

    <Button
    android:id="@+id/btnWriteSDFile"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:text="Ambil Data Operator"
    android:onClick="getdata"/>

</LinearLayout>

Ketiga, buat sebuah file bernama dataoperator.txt pada folder assets, yang berisi:

Simpati
XL
Indosat
Axis
SmartFren

Terakhir kita memodifikasi file AndroidManifest.xml :

...
<uses-permission android:name="android.permission.SEND_SMS" />
...

Sumber :
https://www.youtube.com/watch?v=L2kqVYr2T6c
http://www.codeitive.com/0NigeqqVXW/load-spinner-from-text-file-android.html

  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: