• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to declare array of list for an file

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>

display-output-from-an-file.jpg
[Thumbnail for display-output-from-an-file.jpg]
display from an file
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any one to reply for this and give an example for my doubt?
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue
 
Mohan Prasath
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Within the while loop store your strings (what you get through readline() method) in an ArrayList. This would make your index picking easier.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepika,
Kindly use code formatting tags to format your code. It will ease things for the Ranchers here.
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not able to understand what your telling. can you please tell clearly?
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this: http://www.coderanch.com/how-to/java/UseCodeTags
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
array-list-from-file.jpg
[Thumbnail for array-list-from-file.jpg]
array list not displayed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic