最近学习了listview,有一些心得,希望能帮到你们。
1.代码如下:
MainActivity.java
package com.example.weibokankan;
import java.util.ArrayList;
import java.util.List;
import com.example.weibokankan.weiboAdapter;
import com.example.weibokankan.MainActivity;
import com.example.weibokankan.R;
import com.example.weibokankan.weibo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lvweibo;
private List weibolist=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 建立数据源
initweibos();
// 创建Adapter
lvweibo = (ListView) findViewById(R.id.lv);
weiboAdapter adapter=new weiboAdapter(this, R.layout.kankan_item,weibolist);
lvweibo.setAdapter(adapter);
lvweibo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
weibo wb= weibolist.get(position);
Toast.makeText(MainActivity.this, wb.getName(), Toast.LENGTH_SHORT).show();
}
});
}
private void initweibos(){
weibo webo1=new weibo("娃娃脸",R.drawable.p1,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!");
weibolist.add(webo1);
weibo webo2=new weibo("你是猪吗",R.drawable.p2,"人生途中,有些是无法逃避的,比如命运;有些是无法更改的,比如情缘;有些是难以磨灭的,比如记忆;有些是难以搁置的,比如爱恋,与其被动地承受,不如勇敢地面对!");
weibolist.add(webo2);
weibo webo3=new weibo("你好,2015",R.drawable.p3,"人总是害怕改变,因为改变会带来一份陌生。活在过去的人没有勇气面对陌生——-但人生只有一个方向,就是向前走,就是踏入未来。在陌生里才有新的机会,大步朝前吧。");
weibolist.add(webo3);
weibo webo4=new weibo("扯",R.drawable.p4,"啦啦啦拉拉啊,路阿拉拉阿拉啦啦啦啦啦啦啦啦了");
weibolist.add(webo4);
weibo webo5=new weibo("星湖像花儿一样",R.drawable.p5,"一朵花的凋零,荒芜不了整个春天,一次挫折也荒废不了整个人生。");
weibolist.add(webo5);
weibo webo6=new weibo("等",R.drawable.p6,"挫折是一块石头,对于弱者来说它是拌脚石,让你停步不前。而对于强者来说它是垫脚石,使你站得更高。");
weibolist.add(webo6);
weibo webo7=new weibo("夜雨潇湘",R.drawable.p7,"所有杀不死你的,都会使你强大。");
weibolist.add(webo7);
weibo webo8=new weibo("潇湘夜雨",R.drawable.p8,"人生途中,有些是无法逃避的,比如命运;有些是无法更改的,比如情缘;有些是难以磨灭的,比如记忆;有些是难以搁置的,比如爱恋,与其被动地承受,不如勇敢地面对!");
weibolist.add(webo8);
weibo webo9=new weibo("浅笑如昔",R.drawable.p9,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!!");
weibolist.add(webo9);
weibo webo10=new weibo("潇湘夜雨",R.drawable.p10,"啦啦啦啦啦啦啦啦啦,德玛西亚!!!!!!");
weibolist.add(webo10);
}
}
package com.example.weibokankan;
public class weibo {
private String name;
private int imageId;
private String say;
public weibo(String name, int imageId, String say) {
super();
this.name = name;
this.imageId = imageId;
this.say=say;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getSay() {
return say;
}
public void setSay(String say) {
this.say = say;
}
}
package com.example.weibokankan;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.weibokankan.R;
import com.example.weibokankan.weibo;
public class weiboAdapter extends ArrayAdapter {
private int resourceId;
public weiboAdapter(Context context, int textViewResourceId,
List data) {
super(context, textViewResourceId, data);
resourceId=textViewResourceId;
}
// 这个方法在每个子项被滚动到屏幕内的时候会被调用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
weibo wb = getItem(position);
View view=LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView ivFruit=(ImageView) view.findViewById(R.id.ivimage);
TextView tvweibo=(TextView) view.findViewById(R.id.tv);
TextView tvweibosay=(TextView) view.findViewById(R.id.tvsay);
ivFruit.setImageResource(wb.getImageId());
tvweibo.setText(wb.getName());
tvweibosay.setText(wb.getSay());
return view;
}
}