ListView

MyDemo.java

package com.jackie.listview;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MainActivity extends Activity {
        private int pic[]=new int[]{R.drawable.pic_file1,R.drawable.pic_file2,
        R.drawable.pic_file3,R.drawable.pic_file4,R.drawable.pic_file5,
        R.drawable.pic_file6,R.drawable.pic_file7,R.drawable.pic_file8};
private String data[][]=new String[][]{{"疯狂java","李刚"},{"疯狂安卓","李兴华"},{"spring基础","李兴华"},
{"计算机网络","魔乐科技4"},{"操作系统","魔乐科技5"},{"数据结构","魔乐科技6"},{"linux菜鸟","魔乐科技7"},
{"数据库讲义","魔乐科技8"}};
private ListView datalist=null;
private List<Map<String,String>> list=new ArrayList<Map<String,String>>();//显示的内容包装
private SimpleAdapter simpleAdapter=null;//进行数据的转换操作
private TextView info=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.datalist=(ListView)super.findViewById(R.id.datalist);
this.info=(TextView)super.findViewById(R.id.info);
for(int x=0;x<this.data.length;x++){
Map<String,String> map=new HashMap<String,String>();
map.put("pic", String.valueOf(pic[x]));
map.put("title", this.data[x][0]);
map.put("author",this.data[x][1]);
map.put("type", "免费");
map.put("score", String.valueOf(R.drawable.score));
this.list.add(map);//保存了所有的数据行
}
this.simpleAdapter=new SimpleAdapter(this,this.list,R.layout.data_list,new 
String[]{"pic","title","author","type","score"},//Map中key的名称
new int[]{R.id.icon,R.id.title,R.id.author,R.id.type,R.id.score});//datalist.xml中定义的组件的资源
this.datalist.setAdapter(this.simpleAdapter);
this.datalist.setOnItemLongClickListener(new OnItemLongClickListenerImpl());
}
     private  class OnItemLongClickListenerImpl implements OnItemLongClickListener{


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String,String> map=(Map<String,String>)MainActivity.this.simpleAdapter.getItem(position);
String title=map.get("title");
String author=map.get("author");
MainActivity.this.info.setText("选中的标题是:"+title+"   "+"作者是:"+author);

return false;
}
     
     }
@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;
}
}

Main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="100px"
        android:gravity="center"
        android:textSize="30px" />


    <ListView
        android:id="@+id/datalist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>

data_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3px" />


    <LinearLayout
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:textSize="30px" />


        <TextView
            android:id="@+id/author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:textSize="30px" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:textSize="30px" />


        <ImageView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px" />
    </LinearLayout>


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值