热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在android中添加节标题到自定义列表视图?

packagecom.VRG;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashMap

package com.VRG;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Practice extends Activity {
MediaPlayer mp;
private Integer prac_sounds[] = { R.raw.cow, R.raw.camel, R.raw.dog,
R.raw.donkey, R.raw.elephant, R.raw.horse, R.raw.lion, R.raw.rhino,
R.raw.sheep, R.raw.wolf, R.raw.crow, R.raw.dove, R.raw.duck,
R.raw.eagle, R.raw.owl, R.raw.parrot, R.raw.rooster, R.raw.cuckoo,
R.raw.peocock, R.raw.sparrow };
/** Called when the activity is first created. */
// I use HashMap arraList which takes objects
private ArrayList> myimages;
private static final String IMAGEKEY = "imagename";
private static final String PRICEKEY = "bookprice";
private static final String IMGKEY = "Images from Drawable";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView av, View v, int pos, long id) {
Log.i("111111111111111111", "" + mp);
// if ()
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
}
mp = MediaPlayer.create(Practice.this, prac_sounds[pos]);
mp.seekTo(0);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mpalmost) {
try {
mp.prepare();
Log.i("prepare", "during preparing");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.reset();
Log.i("reset", "during resetting");
// mp = null;
}
});
}
});
myimages = new ArrayList>();
HashMap hm;
hm = new HashMap();
hm.put(IMAGEKEY, "cow");
hm.put(IMGKEY, R.drawable.cow);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "camel");
hm.put(IMGKEY, R.drawable.camel);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "dog");
hm.put(IMGKEY, R.drawable.dog);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "donkey");
hm.put(IMGKEY, R.drawable.donkey);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "elephant");
hm.put(IMGKEY, R.drawable.elephant);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "horse");
hm.put(IMGKEY, R.drawable.horse);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "lion");
hm.put(IMGKEY, R.drawable.lion);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "rhino");
hm.put(IMGKEY, R.drawable.rhinos);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "sheep");
hm.put(IMGKEY, R.drawable.sheep);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "wolf");
hm.put(IMGKEY, R.drawable.wolf);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "crow");
hm.put(IMGKEY, R.drawable.crow);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "dove");
hm.put(IMGKEY, R.drawable.dove);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "duck");
hm.put(IMGKEY, R.drawable.duck);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "eagle");
hm.put(IMGKEY, R.drawable.eagle);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "owl");
hm.put(IMGKEY, R.drawable.owl);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "parrot");
hm.put(IMGKEY, R.drawable.parrot);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "rooster");
hm.put(IMGKEY, R.drawable.rooster);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "cuckoo");
hm.put(IMGKEY, R.drawable.cuckoo);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "peocock");
hm.put(IMGKEY, R.drawable.peocock);
myimages.add(hm);
hm = new HashMap();
hm.put(IMAGEKEY, "sparrow");
hm.put(IMGKEY, R.drawable.sparrow);
myimages.add(hm);
listView.setAdapter(new myListAdapter(myimages, this));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// listView.setStackFromBottom(false);
}
private class myListAdapter extends BaseAdapter {
private ArrayList> IMAGES;
private LayoutInflater mInflater;
public myListAdapter(ArrayList> images,
Context context) {
IMAGES = images;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object getItem(int position) {
return IMAGES.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (cOnvertView== null) {
cOnvertView= mInflater.inflate(R.layout.listview, null);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.v = (TextView) convertView.findViewById(R.id.text1);
holder.v1 = (TextView) convertView.findViewById(R.id.text2);
holder.icon = (ImageView) convertView.findViewById(R.id.img);
// holder.rating =
// (RatingBar)convertView.findViewById(R.id.star);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data with the holder.
holder.v.setText((String) IMAGES.get(position).get(IMAGEKEY));
holder.v1.setText((String) IMAGES.get(position).get(PRICEKEY));
holder.icon.setImageResource((Integer) IMAGES.get(position).get(
IMGKEY));
return convertView;
}
class ViewHolder {
TextView v;
TextView v1;
ImageView icon;
}
}
public void onBackPressed() {
Log.v("44444", "onBackPressed Called");
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
}
finish();
}
public void onKeyDown() {
onBackPressed();
}
}

这是我的list.xml:

android:orientation="vertical" android:layout_
android:layout_>
android:layout_ android:id="@+id/list"/>

 

这是我的listview.xml:

android:layout_ android:orientation="horizontal"
android:layout_>
android:orientation="vertical" android:layout_>
android:layout_ android:id="@+id/text1"
android:textSize="25dip" android:text="This is text1" />

android:id="@+id/text2"
android:text="This is text2" android:layout_/>

android:layout_ android:id="@+id/img" />

 

实际上我正在使用一个自定义列表视图.在那里我分别有动物,鸟类和一些随机图像.现在我需要为图像类别赋予标题.即我需要通过在列表中给出标题来根据类别分隔图像.请帮我一个忙.这是我的java编码:

解决方法:

Here是一个使用Section Header的完整示例,也有一个很好的解释.您还可以下载源代码并查看其工作原理.

UPDATE

你也可以看看我的blog post


推荐阅读
author-avatar
手机用户2502853881
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有