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;
}
}
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>
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>
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>