码迷,mamicode.com
首页 > 移动开发 > 详细

Android——ListView相关作业

时间:2016-05-23 09:06:08      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

给ListVlew提供点击按钮添加新数据,单击项目修改,长按删除功能

layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.PractiseActivity7"
11     android:orientation="vertical">
12 
13     <GridView
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:id="@+id/gv_1"
17         android:numColumns="4"
18         android:stretchMode="columnWidth"
19         android:gravity="center"
20         android:horizontalSpacing="30dp"></GridView>
21     <Button
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:text="点击添加"
25         android:onClick="bt_tj_onClick"/>
26 </LinearLayout>

java类:

  1 package com.hanqi.testapp2;
  2 
  3 import android.app.AlertDialog;
  4 import android.content.DialogInterface;
  5 import android.os.Bundle;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.LayoutInflater;
  8 import android.view.View;
  9 import android.view.ViewGroup;
 10 import android.widget.AdapterView;
 11 import android.widget.BaseAdapter;
 12 import android.widget.GridView;
 13 import android.widget.ImageView;
 14 import android.widget.TextView;
 15 import android.widget.Toast;
 16 
 17 import java.util.ArrayList;
 18 import java.util.List;
 19 
 20 public class PractiseActivity7 extends AppCompatActivity {
 21 
 22     GridView gv_1;
 23     List<MyClass> liv;
 24     TextView tv_3;
 25     ImageView iv_3;
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_practise7);
 30         gv_1 = (GridView)findViewById(R.id.gv_1);
 31         tv_3 = (TextView)findViewById(R.id.tv_3);
 32         iv_3 = (ImageView)findViewById(R.id.iv_3);
 33 
 34         //1、数据 图片的id
 35         liv = new ArrayList<>();
 36         MyClass myClass = new MyClass(R.drawable.f1,"美食1的介绍");
 37         liv.add(myClass);
 38         liv.add(new MyClass(R.drawable.f2,"美食2的介绍"));
 39         liv.add(new MyClass(R.drawable.f3,"美食3的介绍"));
 40         liv.add(new MyClass(R.drawable.f4,"美食4的介绍"));
 41         liv.add(new MyClass(R.drawable.f5,"美食5的介绍"));
 42         liv.add(new MyClass(R.drawable.f6,"美食6的介绍"));
 43         liv.add(new MyClass(R.drawable.f7,"美食7的介绍"));
 44         liv.add(new MyClass(R.drawable.f8, "美食8的介绍"));
 45         final ImageAdapter imageAdapter = new ImageAdapter();
 46         {
 47             gv_1.setAdapter(imageAdapter);
 48         }
 49 
 50         gv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 51             @Override
 52             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 53 
 54             }
 55         });
 56 
 57         gv_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
 58             @Override
 59             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
 60 
 61                 //1.删除数据
 62                 //从集合里删除长按的数据
 63                 liv.remove(position);
 64                 //2.刷新视图,看到效果
 65                 //通知适配器,数据源改变
 66                 imageAdapter.notifyDataSetChanged();
 67                 Toast.makeText(PractiseActivity7.this, "删除了记录索引 = " + position, Toast.LENGTH_SHORT).show();
 68                 return true;//事件已经被消费,不要再处理了
 69             }
 70         });
 71     }
 72     public void bt_tj_onClick(View v)
 73     {
 74         LayoutInflater layoutInflater = getLayoutInflater();
 75         View view = layoutInflater.inflate(R.layout.activity_practise7_1, null);
 76         new AlertDialog.Builder(this)
 77                 .setTitle("添加的内容")
 78                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
 79                     @Override
 80                     public void onClick(DialogInterface dialog, int which) {
 81                         liv.add(new MyClass(R.drawable.f9, "美食9的介绍"));
 82                         ImageAdapter imageAdapter = new ImageAdapter();
 83                         {
 84                             gv_1.setAdapter(imageAdapter);
 85                         }
 86 
 87                     }
 88                 })
 89                 .setView(view)
 90                 .show();
 91     }
 92     class MyClass
 93     {
 94         private int img;
 95         private String content;
 96 
 97         public int getImg() {
 98             return img;
 99         }
100 
101         public void setImg(int img) {
102             this.img = img;
103         }
104 
105         public String getContent() {
106             return content;
107         }
108 
109         public void setContent(String content) {
110             this.content = content;
111         }
112         public MyClass(int img,String content)
113         {
114             this.img = img;
115             this.content = content;
116         }
117     }
118     //适配器
119     class ImageAdapter extends BaseAdapter
120     {
121         @Override
122         public int getCount() {
123             return liv.size();
124         }
125 
126         @Override
127         public Object getItem(int position) {
128             return liv.get(position);
129         }
130 
131         @Override
132         public long getItemId(int position) {
133             return 0;
134         }
135 
136         @Override
137         public View getView(int position, View convertView, ViewGroup parent) {
138             //1.得到数据 图片的id
139             MyClass myClass = liv.get(position);
140             //2.准备View
141             if (convertView ==null) {
142                 convertView = View.inflate(PractiseActivity7.this, R.layout.gridview_layout, null);
143             }
144             //3.适配
145             //转换
146             ImageView imageView = (ImageView)convertView.findViewById(R.id.iv_3);
147             imageView.setImageResource(myClass.getImg());
148 
149             TextView textView = (TextView)convertView.findViewById(R.id.tv_3);
150             textView.setText(myClass.getContent());
151 
152             return convertView;
153         }
154     }
155 }

效果为:

技术分享技术分享技术分享技术分享

修改功能还未做出来,而且目前有BUG,仍在调试中

Android——ListView相关作业

标签:

原文地址:http://www.cnblogs.com/hanazawalove/p/5518690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!