zoukankan      html  css  js  c++  java
  • android ListView SimpleAdapter 带图片

    main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
    </ListView>

    </RelativeLayout>

    item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <ImageView
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ItemImage"/>
    <TextView
    android:id="@+id/ItemTitle"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textSize="20sp"/>
    <TextView
    android:id="@+id/ItemText"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_below="@+id/ItemTitle"/>
    </RelativeLayout>

    主要代码

    public class MainActivity extends Activity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) this.findViewById(R.id.listView1);

    ArrayList<HashMap<String, Object>> lst = new ArrayList<HashMap<String, Object>>();

    for (int i = 0; i < 10; i++) {

    HashMap<String, Object> map = new HashMap<String, Object>();

    map.put("Title", "标题" + i);

    map.put("Content", "内容" + i);

    map.put("Image", R.drawable.ic_launcher);

    lst.add(map);

    }

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, lst,
    R.layout.item, new String[] { "Title", "Content", "Image" },
    new int[] { R.id.ItemTitle, R.id.ItemText, R.id.ItemImage });

    listView.setAdapter(simpleAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {

    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "点了"+arg3, 1000).show();

    }


    });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

  • 相关阅读:
    2021年2月22
    2021年2月21
    2021年2月20
    2021年2月19
    动态添加titie属性
    根据内容改变文字颜色!
    自定义弹出层!
    来回切换图标以及文字
    20180831xlVBA_WorksheetsCosolidate
    20180830xlVBA_合并计算
  • 原文地址:https://www.cnblogs.com/honeynm/p/3795117.html
Copyright © 2011-2022 走看看