2012-01-04 60 views
0

list_item.java呼叫和實例適配器爲ListView

public class List_Items extends ListActivity{ 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.list_item); 


     ListView lv = (ListView) this.findViewById(android.R.id.list); 

     lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, xxx)); 


     Button btn=(Button) findViewById(R.id.button_sync); 
     btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
        } 
        // code here 
       } 
       } 

ImageAndTextListAdapter.java

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> { 

// new method 
    private ListView listView; 
    private AsyncImageLoader asyncImageLoader; 

//constructor 
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) { 
    super(activity, 0, imageAndTexts); 

    //new method 
    this.listView = listView; 
    asyncImageLoader = new AsyncImageLoader(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Activity activity = (Activity) getContext(); 
    // ...... 

} 

}

ImageAndText.java

public class ImageAndText { 
    private String imageUrl; 
    private String text; 

public ImageAndText(String imageUrl, String text) { 
    this.imageUrl = imageUrl; 
    this.text = text; 
} 
public String getImageUrl() { 
    return imageUrl; 
} 
public String getText() { 
    return text; 
} 

}

AsyncImageLoader.java

public class AsyncImageLoader { 
    private HashMap<String, SoftReference<Drawable>> imageCache; 
    HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String,   SoftReference<Drawable>>(); 


public AsyncImageLoader() { 
    //HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String, SoftReference<Drawable>>(); 
} 

public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { 

    if (drawableMap.containsKey(imageUrl)) { 
     SoftReference<Drawable> softReference = imageCache.get(imageUrl); 
     Drawable drawable = softReference.get(); 
     if (drawable != null) { 
      return drawable; 
     } 
    } 
    final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message message) { 
      imageCallback.imageLoaded((Drawable) message.obj, imageUrl); 
     } 
    }; 

    //this is the new thread that download the image from url 
    new Thread() { 
     @Override 
     public void run() { 
      Drawable drawable = loadImageFromUrl(imageUrl); 
      imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); 
      Message message = handler.obtainMessage(0, drawable); 
      handler.sendMessage(message); 
     } 
    }.start(); 
    return null; 
} 

public static Drawable loadImageFromUrl(String url) { 
     InputStream inputStream; 
     try { 
      inputStream = new URL(url).openStream(); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
     return Drawable.createFromStream(inputStream, "src"); 
} 

public interface ImageCallback { 
    public void imageLoaded(Drawable imageDrawable, String imageUrl); 
} 

}

在這個例子中,list_item.java的列表視圖最初是空的,並且它會調用這將調用web網址提供列表視圖的ImageAndTextListAdapter圖像和文本的數據行動態。

我的問題是如何調用適配器,lv.setAdapter((ListAdapter)new ImageAndTextListAdapter(this,xxx));什麼應該是xxx?我可以只做一個xxx = List imageAndTexts ImageAndText類的列表,但不是重複ImaheAndTextListAdapter構造函數中的內容?

其次,我應該提供點擊程序,公共無效的onClick(視圖v){} // 代碼在這裏 }} 的LIST_ITEM內內。
我的目標是擊中按鈕,這將啓動提供所有必要數據的adpater的操作。

回答

1

從ImageAndTextListAdapter適配器中,xxx應該是一個List。

您的適配器需要一個Activity和List兩個參數。

所以,你應該創建一個列表,並創建ImageAndText類的對象添加到列表中這樣的..

ImageAndText image = new ImageAndText("url","Test"); 
List<ImageAndText>text; 
text.add(image); //Add the Object of ImageAndText 
ListView lv = (ListView) findViewById(android.R.id.list); 

//Here i supply the adapter with the text list created. 
lv.setAdapter((ListAdapter)new ImageAndTextListAdapter(Main.this, text)); 
+0

OK,點擊相關按鈕的功能裏面是什麼。點擊它,它會啓動填寫列表的所有操作。 – lilzz 2012-01-04 20:38:45

+0

我將不得不看更多的代碼來看看AsyncImageLoader();那麼我可以知道它需要什麼參數 – 2012-01-04 20:45:30

+0

此外,編譯器希望List text = NULL用於初始化。並警告說text.add(圖像)空指針訪問。如果代碼被執行,那麼它就會崩潰。我認爲它需要正確啓動。 – lilzz 2012-01-04 21:56:20