开发者

Android: How can I get a String from and SD card into a list adapter?

I have been playing around with a LazyList code sample which is pretty neat for displaying images in a safe and non-memory-hogging way. It uses a very simple mechanism of displaying images based on a list that exists within the class file.

Example:

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" };

I am trying to load these values from an index file that is downloaded to the SD card, and then loaded from it. Here is the code that loads the index file and simply displays it as a Toast message (this works fine).

// PULL FILE FROM SD CARD
        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();
               }

            } 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();
            }

What I am struggling with is how to take this string that is being loaded from the SD card and pass it into the list adapter.

The code presently looks like this:

adapter=new LazyAdapter(this, mStrings);
list.setAdapter(adapter); 

I am trying to reload or refresh that same adapter using the values I pulled from the SD card file, presently called "readString".

Can anyone give me some pointers? I am leaving out the code that I created that doesn't work ... because I don't think I am handling this at all correctly. Also - go easy on my, I am a complete newbie to all of this. :-)

Thanks in advance for any help or advice!

-Ray

EDIT #1: Here is my what I am trying to do. Simply copy and paste the working code where I want it. I know it's not that simple, but I am having a hard time reconstructing it properly. Here is my edited code ...

// 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();
            }

The error that I get on "adapter=new LazyAdapter(this, readString);" is "The constructor LazyAdapter(new View.OnClickListener(){}, String) is undefined".

Having not shared the contents of LazyAdapter, that information may not be helpful. So here is the LazyAdapter class in it's entirety ...

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;
}

}


Assuming everything else is as it should be, instead of trying to create a new adapter in each iteration of the loop, store the readString's in an array and construct the adapter afterwards. If you don't know how big the array will be, you can use a list.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜