通过自定义适配器在listView的每一行中加载不同的图像时遇到麻烦

时间:2013-02-20 12:10:42

标签: android android-listview listadapter

我用这个tutorial来学习如何设置我自己的适配器并用自定义行填充listView。

现在我尝试使用适配器在每一行中获取不同的图像(image1,image2等)。 任何人都可以提供一个提示,我必须实现图像的获取!?

来自main.java的片段:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    m_orders = new ArrayList<Order>();
    int r = 0;
    this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
    setListAdapter(this.m_adapter);
    viewOrders = new Runnable(){
        @Override
        public void run() {
            getOrders();
        }
    };
    Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();     
    private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        if(m_orders != null && m_orders.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<m_orders.size();i++)
            m_adapter.add(m_orders.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};
private void getOrders(){
      try{
          m_orders = new ArrayList<Order>();
          Order o1 = new Order();
          o1.setOrderName("SF services");
          Order o2 = new Order();
          o2.setOrderName("SF Advertisement");
          m_orders.add(o1);
          m_orders.add(o2);
          Thread.sleep(2000);
          Log.i("ARRAY", ""+ m_orders.size());
        } catch (Exception e) { 
          Log.e("BACKGROUND_PROC", e.getMessage());
        }
        runOnUiThread(returnRes);
    }


private class OrderAdapter extends ArrayAdapter<Order> {

    private ArrayList<Order> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            Order o = items.get(position);
            if (o != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    if (tt != null) {
                          tt.setText("Name: "+o.getOrderName());                            }
            }
            return v;
    }

和row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip" />  
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center_vertical"
    />
</LinearLayout>

提前thx。我认为找到答案并不难。但我是一个菜鸟并且迷失了一点:(

1 个答案:

答案 0 :(得分:0)

我假设您在询问是否通过网络加载图片。

Get them in 

     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ...
        }

使用延迟加载图片获取延迟加载的更多信息...只是google it ...