温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SimpleAdapter之最简单的适配器

发布时间:2020-06-23 14:49:07 来源:网络 阅读:717 作者:671076656 栏目:移动开发
package com.example.testsimpleadapter;
public class MainActivity extends Activity implements OnItemClickListener{
private ListView listView;
private Button btn;
private List<Map<String, Object>> contents = new ArrayList<Map<String, Object>>();;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
initAdapter();
}
private void initAdapter(){

contents = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("p_w_picpath", R.drawable.ic_launcher);
map.put("text_title", "Test Title");
map.put("text_msg", "msg");
contents.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,
(List<Map<String, Object>>) contents, R.layout.item,
new String[] { "p_w_picpath", "text_title", "text_msg"}, new int[] {
R.id.p_w_picpath, R.id.text_title, R.id.text_msg});
listView.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(this, arg2 + "", Toast.LENGTH_SHORT).show();
}
}

//item.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:descendantFocusability="blocksDescendants" 
    tools:context=".MainActivity" >
    <ImageView
        android:id="@+id/p_w_picpath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="17dp"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/p_w_picpath"
        android:layout_toRightOf="@+id/p_w_picpath"
        android:text="Title Text"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/text_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/p_w_picpath"
        android:layout_alignLeft="@+id/text_title"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:text="msg Text" />
</RelativeLayout>


//布局文件里只有一个list


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI