Search code examples
androidlistviewsearchbar

add image to list view


i want to add image to my list view and this is my list view activity code:

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

i want to add image to my list view. how can i do it? can any one help me to add image on my list view?


Solution

  • You're probably going to need to create a custom ListView Adapter and a new XML Layout for the custom ListView row.

    See Tutorial.

    Ex:

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