新手 看了网上很多资料后 学习写的一个listview

来源:互联网 发布:比较好的vpn 知乎 编辑:程序博客网 时间:2024/05/17 06:57
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package wx.android.test.listviewbutton;
  
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
  
public class ListViewButton extendsActivity {
    /** Called when the activity is first created. */
    ArrayList<HashMap<String, Object>> listitem ;
    privateListView hh;
  
    @Override
      
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          
         
         hh=(ListView)findViewById(R.id.lv);
        MyAdapter mAdapter =new MyAdapter(this);
        hh.setAdapter(mAdapter);
        hh.setOnItemClickListener(newOnItemClickListener(){
              
            @Override
            publicvoid onItemClick(AdapterView<?> arg0, View arg1,int arg2,
                    longarg3) {
                // TODO Auto-generated method stub
                //setTitle("你点击了第"+arg2+"行");
                 Log.v("MyListViewBase","你点击了ListView条目" + arg2);
            }
              
              
        });
    }
    privateArrayList<HashMap<String,Object>> getDate(){
          
        ArrayList<HashMap<String,Object>> listitem =new ArrayList<HashMap<String,Object>>();
    for(inti=0;i<20;i++){
        HashMap<String,Object> map =newHashMap<String,Object>();
        map.put("ItemTitle","第"+i+"行");
        map.put("ItemText","这是第"+i+"行");
        map.put("ItemImage", R.drawable.icon);
        listitem.add(map);
    }
          
          
        returnlistitem;
    
    }
    publicvoid showinfo(){
      
        newAlertDialog.Builder(this)
        .setTitle("Are you sure exit?").setCancelable(false).setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                      
                    @Override
                    publicvoid onClick(DialogInterface dialog,int which) {
                        // TODO Auto-generated method stub
                        ListViewButton.this.finish();
  
                    }
                }).setNegativeButton("No",new DialogInterface.OnClickListener() {
                      
                    @Override
                    publicvoid onClick(DialogInterface dialog,int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                }).show();
          
  
        }
     
    privateclass MyAdapter extends BaseAdapter{
  
        privateLayoutInflater mInflater;
        publicMyAdapter(Context context){
        this.mInflater=LayoutInflater.from(context);
          
        }
  
        @Override
        publicint getCount() {
            // TODO Auto-generated method stub
            returngetDate().size();
        }
  
        @Override
        publicObject getItem(intposition) {
            // TODO Auto-generated method stub
            returnnull;
        }
  
        @Override
        publiclong getItemId(intposition) {
            // TODO Auto-generated method stub
            return0;
        }
  
        @Override
        publicView getView(finalint position, View convertView, ViewGroup parent) {
            ViewHolder hold;
            // Log.v("MyListViewBase", "getView " + position + " " + convertView); 
            if(convertView==null){
                convertView =mInflater.inflate(R.layout.item,null);
                hold=newViewHolder();
                hold.title=(TextView)convertView.findViewById(R.id.ItemTitle);
                hold.text=(TextView)convertView.findViewById(R.id.ItemText);
                hold.image=(ImageView)convertView.findViewById(R.id.ItemImage);
                hold.bt=(Button)convertView.findViewById(R.id.ItemButton);
                convertView.setTag(hold);
            }
            else{
                hold=(ViewHolder)convertView.getTag();
                }
                hold.title.setText(getDate().get(position).get("ItemTitle").toString());
                hold.text.setText(getDate().get(position).get("ItemText").toString());
                  
                hold.image.setBackgroundResource((Integer)getDate().get(position).get("ItemImage"));
                hold.bt.setOnClickListener(newOnClickListener(){
                      
  
                    @Override
                    publicvoid onClick(View v) {
                        // TODO Auto-generated method stub
                        showinfo();
                        //Log.v("MyListBase","你点击了按钮"+position);    
                    }
                });
              
              
              
              
            returnconvertView;
            // TODO Auto-generated method stub
              
        }
          
    }
    publicfinal class ViewHolder{     
        publicImageView image;
        publicTextView title;    
        publicTextView text;    
        publicButton   bt;     
        
}
item.xml
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
     <Button
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/ItemButton" 
     android:text="点我"
     android:layout_toLeftOf="@+id/ItemImage"
    />    
   <TextView 
android:id="@+id/ItemTitle" 
android:layout_height="wrap_content"
android:layout_width="fill_parent" 
android:textSize="20sp" 
   /> 
   <TextView  
   android:id="@+id/ItemText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/ItemTitle"
    />
    <ImageView
    android:id="@+id/ItemImage"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     android:layout_marginLeft="138dip"
      android:layout_marginRight="138dip" 
    />
      
</RelativeLayout>
main.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
    android:id="@+id/lv"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  
    />
  
</LinearLayout>
原创粉丝点击