我正在尝试阅读一个简单的文本文件,该文件是一个单词和数字列表,并将其显示在Android应用程序的可滚动列表中。
我在代码中指定的任意字符串数组没有任何问题,例如字符串数组“myStringArray”或“alphabts”。但是,当我尝试读取文本文件并在其中一个列表中使用它时,我的应用程序崩溃了。
这可能是文本文件的格式错误,或者我应该将文件放在不同的位置并从那里引用它。
感谢您的帮助!
Java代码:
package com.coderzheaven.pack;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class AlphabetListDemo extends Activity {
//String of alphabets //
String[] myStringArray = {"Calories","Protein","Carbs"};
String[] alphabts = {"1","2","3"};
ListView L1, L2;
myAdapter myadp;
myAdapter2 myadp2;
String prod_arr[] = {};
InputStream is = this.getResources().openRawResource(
R.drawable.filetoread);
DataInputStream myDIS = new DataInputStream(is);
ArrayList<String> list = new ArrayList<String>();
final String[] bob = list.toArray(new String[list.size()]);
//final String[] bob = alphabts; Here if I comment out the four
//lines above and and use the alphabts string it works fine
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
L1 = (ListView)findViewById(R.id.list1);
L2 = (ListView)findViewById(R.id.list2);
myadp = new myAdapter(this,alphabts);
L2.setAdapter(myadp);
L2.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setProducts(arg2);
}
});
}
public void setProducts(int number){
prod_arr = new String[25];
// adding some dummy data //
for(int i = 0; i < 25 ; i++){
prod_arr[i] = bob[number];
}
//setting the adapter in listview //
myadp2 = new myAdapter2(AlphabetListDemo.this,prod_arr);
L1.setAdapter(myadp2);
}
class myAdapter extends ArrayAdapter<String>
{
TextView label;
ImageView image;
View row;
public myAdapter(Context context,String[] arr)
{
super(context, android.R.layout.simple_list_item_1, arr);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
try{
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.lv_rows, parent, false);
label = (TextView)row.findViewById(R.id.item_title);
label.setText(myStringArray[position]);
label.setTextColor(Color.YELLOW);
}catch(Exception e){
}
return row;
}
}
// adapter for second list.....
class myAdapter2 extends ArrayAdapter<String>
{
TextView label;
ImageView image;
View row;
public myAdapter2(Context context,String[] arr)
{
super(context, android.R.layout.simple_list_item_1, arr);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
try{
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.lv_rows, parent, false);
label = (TextView)row.findViewById(R.id.item_title);
label.setText(prod_arr[position]);
label.setTextColor(Color.WHITE);
}catch(Exception e){
}
return row;
}
}
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<!-- this list contains products -->
<ListView
android:id="@+id/list1"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:cacheColorHint="#00000000"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"
android:fadingEdge="vertical"
android:padding="0dip"
android:scrollbars="none"
android:smoothScrollbar="true"
android:soundEffectsEnabled="true" />
<ListView
android:id="@+id/list2"
android:layout_weight="4"
android:cacheColorHint="#00000000"
android:scrollbars="none"
android:fadingEdge="vertical"
android:soundEffectsEnabled="true"
android:dividerHeight="1px"
android:padding="0dip"
android:smoothScrollbar="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginBottom="5dip" />
</LinearLayout>
lv_rows.xml与main.xml位于同一位置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="Main Item"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14dp"
android:textStyle="normal" />
</RelativeLayout>
答案 0 :(得分:0)
遇到问题你忘记了+号。
InputStream is = this.getResources().openRawResource(
+R.drawable.filetoread);
答案 1 :(得分:0)
为什么要从drawable文件夹中读取文本文件? 正确的方法是将此文件放入项目的assets文件夹中。 如果您使用Android Studio,请右键单击您的项目 - &gt;新 - &gt;文件夹 - &gt;资产文件夹。
然后将文件移入其中,只需使用AssetManager打开它:
AssetManager assetsManager = getApplicationContext().getAssets(); // or getBaseContext()
try{
InputStream inputStream = assetsManager.open("yourfile.txt");
// read inputStream ...
// and do not forget to close it here when your read is completed!!!
}catch (IOException ex){
ex.printStackTrace();
}