将图像添加到列表视图

时间:2014-09-15 09:50:50

标签: android list view searchbar

我想将图像添加到列表视图中,这是我的列表视图活动代码:

package com.example.dssdfsd; 
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView lv;
ArrayAdapter<String> adapter;

EditText inputSearch;

ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String products[] = {"Dell Inspiron"};

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    adapter = new ArrayAdapter<String>(this, R.layout.list_item,            R.id.product_name,       products);
    lv.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            MainActivity.this.adapter.getFilter().filter(cs);  
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {                       
        }
    });
}   
}

我想将图片添加到列表视图中。我该怎么做?  任何人都可以帮我在列表视图中添加图片吗?

1 个答案:

答案 0 :(得分:0)

您可能需要为自定义ListView行创建自定义ListView适配器和新的XML布局。

请参阅Tutorial

例如:

<强> list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>
        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </TableRow>
</TableLayout>

<强> CustomList.java

public class CustomList extends ArrayAdapter<String>{
   private final Activity context;
   private final String[] web;
   private final Integer[] imageId;
   public CustomList(Activity context, String[] web, Integer[] imageId) {
      super(context, R.layout.list_single, web);
      this.context = context;
      this.web = web;
      this.imageId = imageId;
   }
   @Override
   public View getView(int position, View view, ViewGroup parent) {
      LayoutInflater inflater = context.getLayoutInflater();
      View rowView= inflater.inflate(R.layout.list_row, null, true);
      TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
      ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
      txtTitle.setText(web[position]);
      imageView.setImageResource(imageId[position]);
      return rowView;
   }
}