带图像和文本的自定义适配器

时间:2016-09-21 06:51:13

标签: android listview

请帮助我如何使用包含图像和2个textview的自定义适配器构建自动完成文本视图。 不显示类型上的列表,但是当我评论图像的一部分时,列表显示正常但没有显示图像。

Auto complete
import java.util.HashMap;

import android.content.Context;`enter`enter code here` code here`
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

/** Customizing AutoCompleteTextView to return Country Name
 *  corresponding to the selected item
 */
public class Autocomplete extends AutoCompleteTextView {

    public Autocomplete(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /** Returns the country name corresponding to the selected item */
    @Override
    protected CharSequence convertSelectionToString(Object selectedItem) {
        /** Each item in the autocompetetextview suggestion list is a hashmap object */
        HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
        return hm.get("txt");
    }
}





  
Main Class

List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
        int[] flags = new int[]{R.drawable.progress_image,
                R.drawable.l1
        };
        HashMap<String, String> hm = new HashMap<String,String>();
        for(int i=0;i<array_sheet.length;i++){

            if(y==5) {
                hm.put("txt", array_sheet[i][y]);
                hm.put("flag", Integer.toString(flags[1]));
                hm.put("txt2", array_sheet[i][y + 1]);
            }
            else
            {
                hm.put("txt", array_sheet[i][y]);
               hm.put("flag", Integer.toString(flags[0]));
                hm.put("txt2", array_sheet[i][y - 1]);
            }

            aList.add(hm);
            System.out.println("ALIST "+aList);
        }
        HashSet hs = new HashSet();
        hs.addAll(aList);
        aList.clear();
        aList.addAll(hs);


        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), aList, R.layout.autocomplete, from, to);
        System.out.println(" ADAPTER "+aList);
        
        final Autocomplete autoComplete = ( Autocomplete) findViewById(R.id.autocomplete);

        autoComplete.setAdapter(adapter);
       
        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
            {

                System.out.println(position+" ADAPTER "+arg0.getAdapter().toString());
                HashMap<String, String> hm = (HashMap<String, String>) arg0.getAdapter().getItem(position);

                /** Getting a reference to the TextView of the layout file activity_main to set Currency */
             
                /** Getting currency from the HashMap and setting it to the textview */
                autoComplete.setText("Name : " + hm.get("txt"));
            }
        };
        autoComplete.setOnItemClickListener(itemClickListener);
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/description1"
        android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:textSize="15dp"
        android:padding="10dp"
        android:textColor="@color/black"
        />
    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Person"
        android:padding="10dp"
        />
    <TextView
        android:id="@+id/description2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:padding="10dp"
        android:textColor="@color/black"
        />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

参考 Android AutocompleteTextView with Custom Adapter

用以下代码替换 row_people.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/lbl_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_marginLeft="18dp"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />