Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何从SD卡和列表适配器中获取字符串?_Android - Fatal编程技术网

Android:如何从SD卡和列表适配器中获取字符串?

Android:如何从SD卡和列表适配器中获取字符串?,android,Android,我一直在玩一个LazyList代码示例,它非常适合以安全且不占用内存的方式显示图像。它使用一种非常简单的机制,根据类文件中存在的列表显示图像 例如: private String[] mStrings={ "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png", "http://a3.twimg.com/profile_images/740897825/AndroidCast-3

我一直在玩一个LazyList代码示例,它非常适合以安全且不占用内存的方式显示图像。它使用一种非常简单的机制,根据类文件中存在的列表显示图像

例如:

private String[] mStrings={
        "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
        "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png" };
我试图从下载到SD卡的索引文件加载这些值,然后从SD卡加载。下面是加载索引文件并将其显示为Toast消息的代码(这很好)

我正在努力解决的是如何将从SD卡加载的字符串传递到列表适配器中

目前代码如下所示:

adapter=new LazyAdapter(this, mStrings);
list.setAdapter(adapter); 
我正在尝试使用从SD卡文件(目前称为“readString”)中提取的值重新加载或刷新同一适配器

谁能给我一些指点吗?我省略了我创建的不起作用的代码。。。因为我认为我根本没有正确处理这件事。还有-对我的朋友放松点,我对这一切都是一个完全的新手。:-)

提前感谢您的帮助或建议

-射线

编辑#1: 以下是我的想法,我正在尝试做的事情。只需将工作代码复制并粘贴到我想要的地方。我知道这不是那么简单,但我很难正确地重建它。这是我编辑的代码

// PULL FILE FROM CARD ATTEMPT #2
        try{
               File f = new File(Environment.getExternalStorageDirectory()+"/LazyList/gkindex.txt");
               FileInputStream fileIS = new FileInputStream(f);
               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
               String readString = new String(); 
               //just reading each line and pass it on the debugger
               while((readString = buf.readLine())!= null){
                   Toast.makeText(MainActivity.this, "WIN: "+readString, Toast.LENGTH_LONG).show();
               // THIS DOESN'T WORK ...
                    adapter=new LazyAdapter(this, readString);
                    list.setAdapter(adapter);
               }

            } catch (FileNotFoundException e) {
                Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (IOException e){
                Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
我在“adapter=newlazyadapter(this,readString);”上得到的错误是“构造函数LazyAdapter(newview.OnClickListener(){},String)未定义”

由于没有共享LazyAdapter的内容,这些信息可能没有帮助。这是LazyAdapter类的全部内容

package com.fedorvlasov.lazylist;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 
public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
    return data.length;
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}
public static class ViewHolder{
    public TextView text;
    public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();
    holder.text.setText("item "+position);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

}假设其他一切都是它应该的,而不是尝试在循环的每次迭代中创建一个新的适配器,而是将
readString
存储在一个数组中,然后构造适配器。如果不知道数组的大小,可以使用列表

List<String> strings = new ArrayList<String>();
while((readString = buf.readLine())!= null){
    Toast.makeText(MainActivity.this, "WIN: "+readString, Toast.LENGTH_LONG).show();
    strings.add(readString);
}
adapter=new LazyAdapter(this, strings.toArray());
list.setAdapter(adapter);
List strings=new ArrayList();
而((readString=buf.readLine())!=null){
Toast.makeText(MainActivity.this,“WIN:+readString,Toast.LENGTH_LONG).show();
strings.add(readString);
}
adapter=新的LazyAdapter(this,strings.toArray());
list.setAdapter(适配器);

您当前的解决方案有哪些地方不起作用?有什么问题?为什么我不能将代码粘贴到我的注释中?仍在尝试学习如何使用堆栈溢出。:-)我编辑了我的原始问题,以便以有组织的方式粘贴代码。您可能需要绕过错误处理,以便程序正常失败,但这应该可以让您开始。你好,Kyle,这对我来说很有意义。但是我的适配器出现了一个错误——“构造函数LazyAdapter(MainActivity,Object[])未定义”。它告诉我它需要LazyAdapter(MainActivity,String[]),而我给它发送了一个对象。另外,我很抱歉不能相信你的建议。我是新来的,但我还没有足够的分数?我试着这样做:“adapter=newlazyadapter(this,(String[])strings.toArray(newstring[0]);”但是没有加载任何内容(没有图像)。老实说,我不确定“0”在那里做什么。直接将URL加载到数组中会更好吗?我不知道我真的需要将它存储到一个文件中,然后加载它。我觉得这应该是一个简单的两行代码。但在寻找任何例子时遇到困难:-/对不起,老兄,我出城了。我说使用列表的原因是因为你不知道它需要多长时间(这使得数组更难)。“adapter=new LazyAdapter(this,(String[])strings.toArray())”应该有效-toArray()正在请求参数?通过传递一个新字符串[],您将使它生成一个空数组。toArray()的任何参数都不应起作用。
List<String> strings = new ArrayList<String>();
while((readString = buf.readLine())!= null){
    Toast.makeText(MainActivity.this, "WIN: "+readString, Toast.LENGTH_LONG).show();
    strings.add(readString);
}
adapter=new LazyAdapter(this, strings.toArray());
list.setAdapter(adapter);