File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes how to declare array of list for an file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "how to declare array of list for an file" Watch "how to declare array of list for an file" New topic
Author

how to declare array of list for an file

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i have generated an code for calling an file and it works fine. now i have different names that exists in an file. i want to declare that in that file and when i enter that number i want to display that name alone in the output

the text file look like this
apple
------------
mango
-------------
pineapple
-------------
jackfruit
-------------
papaya
-------------
grapes
------------
gauva
-----------
i want to get selective output displaying only one word. can any one illustrate with an example for an file access?
and i want to declare apple [0], mango[1] and so on till gauva[6]
when i enter 6 it should display guava alone. can any one give some examples how to declare that for an file?

the code is

package com.javasamples;

//reading an embedded RAW data file

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.*;

public class FileDemo1Activity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
PlayWithRawFiles();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"Problems: " + e.getMessage(), 1).show();
}
}// onCreate

public void PlayWithRawFiles() throws IOException {
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.drawable.my_base_data);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
buf.append(str + "\n" );
}
}
is.close();
Toast.makeText(getBaseContext(),
buf.toString(), Toast.LENGTH_LONG).show();


}// PlayWithSDFiles

} // FilesDemo4

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>




[Thumbnail for display-output-from-an-file.jpg]

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
is there any one to reply for this and give an example for my doubt?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
PatienceIsAVirtue
Mohan Prasath
Ranch Hand

Joined: Jul 25, 2011
Posts: 38


Within the while loop store your strings (what you get through readline() method) in an ArrayList. This would make your index picking easier.

With regards,
Mohan Prasath
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i have changed the code. but i am getting an error like this
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,str));

code
package com.android.hellolistview;

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

//import com.javasamples.R;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Hellolistview extends ListActivity {
/** Called when the activity is first created. */

/* @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayWithRawFiles1();
}private void PlayWithRawFiles1() {
// TODO Auto-generated method stub

}
public void PlayWithRawFiles() throws IOException {
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.drawable.my_base_data);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,str));

// ListView lv = getListView();
//lv.setTextFilterEnabled(true);
ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text

}
});
buf.append(str + "\n" );
}
}
is.close();


// PlayWithSDFiles


// FilesDemo4

// onCreate


} }

if i change this statement it not showing the output . what problem is here ?
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Deepika,
Kindly use code formatting tags to format your code. It will ease things for the Ranchers here.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i am not able to understand what your telling. can you please tell clearly?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Read this: http://www.coderanch.com/how-to/java/UseCodeTags
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
ya understood ..

i have corrected the code. there is no error in the code. but the output is not displayed. how to solve it ?
[code = android]

package com.android.hellolistview;

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

//import com.javasamples.R;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Hellolistview extends ListActivity {
/** Called when the activity is first created. */

/* @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayWithRawFiles1();
}private void PlayWithRawFiles1() {
// TODO Auto-generated method stub

}
public void PlayWithRawFiles() throws IOException {
String str="";


StringBuffer buf = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.drawable.my_base_data);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
// String str1 = str;
//String[] str1 = str;
String[] str1 = str.split("\\\n");
char[] ch = new char[500];
ch=str.toCharArray();
boolean flag1=true, flag2=true;

for(int i=0; i<ch.length; i++) {

switch(ch[i]) {
case ' ': flag1 = false;
break;
case '\n': flag1 = true;
continue;
}
if(flag1)
System.out.print(ch[i]);
}



int i = 0;
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ch[i]));

// ListView lv = getListView();
//lv.setTextFilterEnabled(true);
ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text

}
});
buf.append(ch[i] + "\n" );
}
}
is.close();

Toast.makeText(getBaseContext(),
buf.toString(), Toast.LENGTH_LONG).show();
// PlayWithSDFiles


// FilesDemo4

// onCreate


} }
[/code]
the screen look like this



[Thumbnail for array-list-from-file.jpg]

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to declare array of list for an file