001.
package
com.myapp;
002.
003.
import
java.util.ArrayList;
004.
import
java.util.HashMap;
005.
006.
import
android.os.Bundle;
007.
import
android.view.LayoutInflater;
008.
import
android.view.Menu;
009.
import
android.view.View;
010.
import
android.view.ViewGroup;
011.
import
android.widget.BaseAdapter;
012.
import
android.widget.ImageButton;
013.
import
android.widget.ImageView;
014.
import
android.widget.ListView;
015.
import
android.widget.TextView;
016.
import
android.widget.Toast;
017.
import
android.app.Activity;
018.
import
android.app.AlertDialog;
019.
import
android.content.Context;
020.
import
android.content.DialogInterface;
021.
import
android.graphics.Color;
022.
023.
public
class
MainActivity
extends
Activity {
024.
025.
ArrayList<HashMap<String, String>> MyArrList;
026.
027.
@Override
028.
public
void
onCreate(Bundle savedInstanceState) {
029.
super
.onCreate(savedInstanceState);
030.
setContentView(R.layout.activity_main);
031.
032.
MyArrList =
new
ArrayList<HashMap<String, String>>();
033.
HashMap<String, String> map;
034.
035.
036.
map =
new
HashMap<String, String>();
037.
map.put(
"ImageID"
,
"1"
);
038.
map.put(
"ImageDesc"
,
"Sea View1"
);
039.
map.put(
"ImagePath"
,
"pic_a"
);
040.
MyArrList.add(map);
041.
042.
043.
map =
new
HashMap<String, String>();
044.
map.put(
"ImageID"
,
"2"
);
045.
map.put(
"ImageDesc"
,
"Sea View2"
);
046.
map.put(
"ImagePath"
,
"pic_b"
);
047.
MyArrList.add(map);
048.
049.
050.
map =
new
HashMap<String, String>();
051.
map.put(
"ImageID"
,
"3"
);
052.
map.put(
"ImageDesc"
,
"Sea View 3"
);
053.
map.put(
"ImagePath"
,
"pic_c"
);
054.
MyArrList.add(map);
055.
056.
057.
map =
new
HashMap<String, String>();
058.
map.put(
"ImageID"
,
"4"
);
059.
map.put(
"ImageDesc"
,
"Sea View 4"
);
060.
map.put(
"ImagePath"
,
"pic_d"
);
061.
MyArrList.add(map);
062.
063.
064.
final
ListView lstView1 = (ListView)findViewById(R.id.listView1);
065.
066.
lstView1.setAdapter(
new
ImageAdapter(
this
));
067.
068.
}
069.
070.
071.
public
class
ImageAdapter
extends
BaseAdapter
072.
{
073.
private
Context context;
074.
075.
public
ImageAdapter(Context c)
076.
{
077.
078.
context = c;
079.
}
080.
081.
public
int
getCount() {
082.
083.
return
MyArrList.size();
084.
}
085.
086.
public
Object getItem(
int
position) {
087.
088.
return
position;
089.
}
090.
091.
public
long
getItemId(
int
position) {
092.
093.
return
position;
094.
}
095.
public
View getView(
final
int
position, View convertView, ViewGroup parent) {
096.
097.
098.
LayoutInflater inflater = (LayoutInflater) context
099.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
100.
101.
if
(convertView ==
null
) {
102.
convertView = inflater.inflate(R.layout.activity_column,
null
);
103.
}
104.
105.
106.
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
107.
imageView.getLayoutParams().height =
80
;
108.
imageView.getLayoutParams().width =
80
;
109.
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
110.
imageView.setPadding(
5
,
5
,
5
,
5
);
111.
int
ResID = context.getResources().getIdentifier(MyArrList.get(position).get(
"ImagePath"
),
"drawable"
, context.getPackageName());
112.
imageView.setImageResource(ResID);
113.
114.
115.
TextView txtPosition = (TextView) convertView.findViewById(R.id.ColImgID);
116.
txtPosition.setPadding(
10
,
0
,
0
,
0
);
117.
txtPosition.setText(MyArrList.get(position).get(
"ImageID"
) +
"."
);
118.
119.
120.
TextView txtPicName = (TextView) convertView.findViewById(R.id.ColImgDesc);
121.
txtPicName.setPadding(
5
,
0
,
0
,
0
);
122.
txtPicName.setText(MyArrList.get(position).get(
"ImageDesc"
));
123.
124.
125.
ImageButton cmdShared = (ImageButton) convertView.findViewById(R.id.imgCmdShared);
126.
127.
cmdShared.setOnClickListener(
new
View.OnClickListener() {
128.
public
void
onClick(View v) {
129.
Toast.makeText(MainActivity.
this
,
"Your Shared (ImageID = "
+ MyArrList.get(position).get(
"ImageID"
) +
")"
,Toast.LENGTH_LONG).show();
130.
131.
132.
133.
134.
135.
136.
}
137.
});
138.
139.
140.
ImageButton cmdView = (ImageButton) convertView.findViewById(R.id.imgCmdView);
141.
142.
cmdView.setOnClickListener(
new
View.OnClickListener() {
143.
public
void
onClick(View v) {
144.
Toast.makeText(MainActivity.
this
,
"Your View (ImageID = "
+ MyArrList.get(position).get(
"ImageID"
) +
")"
,Toast.LENGTH_LONG).show();
145.
146.
147.
148.
149.
150.
151.
}
152.
});
153.
154.
155.
ImageButton cmdDelete = (ImageButton) convertView.findViewById(R.id.imgCmdDelete);
156.
157.
final
AlertDialog.Builder adb =
new
AlertDialog.Builder(MainActivity.
this
);
158.
cmdDelete.setOnClickListener(
new
View.OnClickListener() {
159.
public
void
onClick(View v) {
160.
adb.setTitle(
"Delete?"
);
161.
adb.setMessage(
"Are you sure delete ["
+ MyArrList.get(position).get(
"ImageDesc"
) +
"]"
);
162.
adb.setNegativeButton(
"Cancel"
,
null
);
163.
adb.setPositiveButton(
"Ok"
,
new
AlertDialog.OnClickListener() {
164.
public
void
onClick(DialogInterface dialog,
int
which) {
165.
Toast.makeText(MainActivity.
this
,
"Your Delete (ImageID = "
+ MyArrList.get(position).get(
"ImageID"
) +
")"
,Toast.LENGTH_LONG).show();
166.
167.
168.
169.
170.
}});
171.
adb.show();
172.
}
173.
});
174.
175.
176.
return
convertView;
177.
178.
}
179.
180.
}
181.
182.
183.
@Override
184.
public
boolean
onCreateOptionsMenu(Menu menu) {
185.
getMenuInflater().inflate(R.menu.activity_main, menu);
186.
return
true
;
187.
}
188.
189.
}