Android - 为列表中的每个项目设置不同的ImageView

时间:2013-08-07 04:08:12

标签: android android-listview android-imageview listadapter

我正在尝试为列表的每一行设置不同的ImageView。所有图像都存储在我的资源文件夹中,因此我将assets文件夹中的路径存储在xml文件中。在我的下面的代码中,我设置列表中每行的标题和描述,但我不知道如何设置特定的imageView。

以下是我的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub


    Bundle getbundle = getArguments();
    KEY_CATEGORY= getbundle.getString("id");

    View v = inflater.inflate(R.layout.main, container, false);

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = getXml("Images.xml"); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_CATEGORY);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_MUSCLE, "Rs." + parser.getValue(e, KEY_MUSCLE));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
        map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_MUSCLE, KEY_FILE }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost, R.id.tvfilepath });

    setListAdapter(adapter);

    ListView lv = (ListView)v.findViewById(android.R.id.list);
    return v;
}

这是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="wrap_content"
android:orientation="horizontal">  
<ImageView
            android:id="@+id/imageView1"
            android:layout_width="80dp"
            android:layout_height="80dp" />
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Name Label -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#dc6800"
        android:textSize="16sp"
        android:textStyle="bold"
        android:paddingTop="6dip"
        android:paddingBottom="2dip" />
    <!-- Description label -->
    <TextView
        android:id="@+id/desciption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#acacac"
        android:paddingBottom="2dip">
    </TextView>
    <!-- Linear layout for cost and price Cost: Rs.100 -->
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <!-- Cost Label -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:gravity="left"
        android:textStyle="bold"
        android:text="Cost: " >
    </TextView>
    <!-- Price Label -->
    <TextView
        android:id="@+id/cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#acacac" 
        android:textStyle="bold"
        android:gravity="left">
    </TextView>

    <TextView
        android:id="@+id/tvfilepath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#acacac" 
        android:textStyle="bold"/>

    </LinearLayout>
</LinearLayout>

</LinearLayout>

4 个答案:

答案 0 :(得分:1)

您应该实现自己的适配器,而不是使用SimpleAdapter。在自定义适配器中,实现getView方法将图像设置为imageView。

答案 1 :(得分:1)

编写您自己的自定义适配器,如下所示

public class ShowFriendsAdapter extends BaseAdapter {

private Activity mActivity;
private List<ManageFriendsDetails> mFriendsDetails;

public ShowFriendsAdapter(Activity mActivity, List<ManageFriendsDetails> mFriendsDetails) {
    this.mActivity = mActivity;
    this.mFriendsDetails = mFriendsDetails;
}

@Override
public int getCount() {
    return mFriendsDetails.size();
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    final ViewHolderItem mViewHolderItem;

    if (convertView == null) {
        LayoutInflater li = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.row_show_friend, parent, false);

        mViewHolderItem = new ViewHolderItem();
        mViewHolderItem.imgFriendImage = (ImageView) convertView.findViewById(R.id.img_friend_image);
        mViewHolderItem.tvFriendName = (TextView) convertView.findViewById(R.id.tv_friend_name);
        mViewHolderItem.tvFriendEmail = (TextView) convertView.findViewById(R.id.tv_friend_email);
        convertView.setTag(mViewHolderItem);
    } else {
        mViewHolderItem = (ViewHolderItem) convertView.getTag();
    }
    mViewHolderItem.tvFriendName.setText(mFriendsDetails.get(position).getfName());
    mViewHolderItem.tvFriendEmail.setText(mFriendsDetails.get(position).getfEmail());
mViewHolderItem.imgFriendImage.setImageResource(mFriendsDetails.get(position).getfImage());
    return convertView;
}

static class ViewHolderItem {

    private ImageView imgFriendImage;
    private TextView tvFriendName, tvFriendEmail;

}
}

答案 2 :(得分:0)

这可能不是确切的答案,但这可能对您有所帮助

http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text

答案 3 :(得分:0)

我认为你应该使用自定义列表视图而不是简单的列表视图。并将您的图像放在可绘制文件夹而不是资源文件夹中。

public class CustomListView extends Activity {
 ArrayList<Contact> imageArry = new ArrayList<Contact>();
 ContactImageAdapter adapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // add image and text in arraylist
  imageArry.add(new Contact(R.drawable.facebook, "FaceBook"));
  imageArry.add(new Contact(R.drawable.google, "Google"));
  imageArry.add(new Contact(R.drawable.ical, "Ical"));
  imageArry.add(new Contact(R.drawable.outlook, "Outlook"));
  imageArry.add(new Contact(R.drawable.twitter, "Twitter"));
  // add data in contact image adapter
  adapter = new ContactImageAdapter(this, R.layout.list, imageArry);
  ListView dataList = (ListView) findViewById(R.id.list);
  dataList.setAdapter(adapter);

 }

查看此博客 - Androidhub4you