新闻客户端News
来源:互联网 发布:特效照相机软件下载 编辑:程序博客网 时间:2024/05/17 02:13
AsyncHttpClient 和 SmartImageView 的使用
运行界面
界面交互代码
MainActivity
ListView 的 setOnItemClickListener() 方法
用于监听Item的点击事件 使用时 需要传入一个OnItemClickListener 实现类对象 且需要实现onItemClick() 方法
ListView 的 setSelection() 方法
设置当前选中的条目
public class MainActivity extends AppCompatActivity { ListView lv_news; LinearLayout Loading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_news = (ListView) findViewById(R.id.lv_news); Loading = (LinearLayout) findViewById(R.id.loading); fillData3(); } private void fillData3() { System.out.println(">>>>>>>>>>>>>执行了"); AsyncHttpClient asynchttpClient = new AsyncHttpClient(); asynchttpClient.get("http://10.61.19.112:8080/NewsInfo.json", new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) { try { String json = new String(bytes, "utf-8"); List<newsInfo> newsInfos = JsonParse.getNewsInfo(json); if (newsInfos == null) { Toast.makeText(MainActivity.this, "解析失败!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "获取成功!", Toast.LENGTH_SHORT).show(); Loading.setVisibility(View.INVISIBLE); lv_news.setAdapter(new newsAdapter(MainActivity.this, newsInfos)); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) { System.out.println(">>>>>>>>>获取失败!"); } }); }}
newsInfo类
定义了 IconPath 图片路径 新闻标题 描述 等等描述
public class newsInfo { private String icon; private String title; private String content; private int type; private long comment; public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getType() { return type; } public void setType(int type) { this.type = type; } public long getComment() { return comment; } public void setComment(long comment) { this.comment = comment; }}
JsonParse类
android
public class JsonParse { public static List<newsInfo> getNewsInfo(String json){ Gson gson =new Gson(); Type listType = new TypeToken<List<newsInfo>>(){ }.getType(); List<newsInfo> newsInfos = gson.fromJson(json,listType); return newsInfos; }}
newsAdapter类
androidpublic class newsAdapter extends ArrayAdapter<newsInfo>{ public newsAdapter(Context context, List<newsInfo> objects) { super(context, R.layout.news_item , objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { newsInfo news= getItem(position); View view; ViewHolder viewHolder; if (convertView == null) { //任务 补充完整 view = LayoutInflater.from(getContext()).inflate(R.layout.news_item, null); viewHolder = new ViewHolder(); //获取控件 viewHolder.siv = (SmartImageView) view.findViewById(R.id.siv_icon); viewHolder.tvtitle = (TextView) view.findViewById(R.id.tv_title); viewHolder.tvdescription = (TextView) view.findViewById(R.id.tv_description); viewHolder.tvtype = (TextView) view.findViewById(R.id.tv_type); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.siv.setImageUrl(news.getIcon()); viewHolder.tvtitle.setText(news.getTitle()); viewHolder.tvdescription.setText(news.getContent()); viewHolder.tvtype.setText(news.getType()+""); return view; } class ViewHolder { SmartImageView siv; TextView tvtitle; TextView tvdescription; TextView tvtype; }}
布局文件
Activity_main.xml
android<?xml version="1.0" encoding="utf-8"?><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" tools:context=".MainActivity" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="invisible"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加载信息..." /> </LinearLayout> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout></LinearLayout>
*news_item.xml
TextView用于显示数据库中的某条数据的详细信息
ImageView用于增加减少金额和删除金额
android<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="65dp"> <com.loopj.android.image.SmartImageView android:id="@+id/siv_icon" android:layout_width="80dp" android:layout_height="60dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"></com.loopj.android.image.SmartImageView> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="20" android:singleLine="true" android:text="我是标题" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="16" android:maxLines="1" android:text="我是描述" android:textColor="#99000000" android:textSize="14sp" /> <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="5dp" android:layout_marginRight="10dp" android:text="评论" android:textColor="#99000000" android:textSize="12sp" /></RelativeLayout>
阅读全文
0 0
- 新闻客户端News
- Java-新闻news案例
- news新闻分页
- daily news新闻阅读客户端应用源码(兼容iPhone和iPad)
- Magento新闻插件-----Scalena News
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 源生JS实现回到顶部--缓冲效果
- Android Studio初认识
- IDEA 乱码以及junit模板
- 1121. Damn Single (25)[数学逻辑]
- MySQL中文索引插件mysqlcft安装及使用
- 新闻客户端News
- 如何解决log4net无法打印日志
- springcloud springcloudRibbon
- 注解方式进行spring定时任务的调用
- MySQL 学习<八> MySQL索引
- 解决 Pod install 问题:cannot load such file -- cfpropertylist
- Android学习十 多媒体
- Struts2拦截器的实现
- Linux内存管理详细解析