大家好,又见面了,我是你们的朋友全栈君。
这几天学会了ListView组件,希望能帮到你们。
程序测试图如下:
1.代码如下:
MainActivity.java
代码语言:javascript复制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<weibo> weibolist=new ArrayList<weibo>();
@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("失心症",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);
}
}
Jetbrains全家桶1年46,售后保障稳定
2.代码如下:
weibo.java
代码语言:javascript复制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;
}
}
3.代码如下:
weiboAdapter.java
代码语言:javascript复制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<weibo> {
private int resourceId;
public weiboAdapter(Context context, int textViewResourceId,
List<weibo> 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;
}
}
4.代码如下:
activity_main.xml
代码语言:javascript复制<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@ id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
5.代码如下:
kankan_item.xml
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@ id/ivimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="@drawable/p1" />
<RelativeLayout
android:id="@ id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@ id/tv"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple" />
<TextView
android:id="@ id/tvsay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:layout_alignLeft="@id/tv"
android:text="1fdggwhvgtrryvbxcvffrigoiggkgfijijkmkvmgiovmgiomkom iogm" />
</RelativeLayout>
</LinearLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/213440.html原文链接:https://javaforall.cn