Sunday, September 19, 2010

Custom Listview with Image

public static ArrayList vidID = new ArrayList();
public static Hashtable> VideoTable = new Hashtable>();
Hashtable _tempVideo = new Hashtable();
ArrayList db_id;

--Start any required Loop

_tempVideo.put("id", curValue);
_tempVideo.put("title", curValue);
_tempVideo.put("filename", curValue);
_tempVideo.put("thumbnail", curValue);

VideoTable.put(curVidId, _tempVideo);
vidID.add(curVidId);

_tempVideo = new Hashtable();

---End Loop

private void fillData() {

ListView list = (ListView) findViewById(R.id.ListView01);

ArrayList> mylist = new ArrayList>();
HashMap map;

db_id = new ArrayList();
for (int i = 0; i < vidID.size(); i++) {
Hashtable temp = VideoTable
.get(vidID.get(i));
try {
map = new HashMap();
map.put("id", temp.get("id"));
map.put("title", temp.get("title"));
map.put("description", temp.get("description"));
map.put("theartist", temp.get("theartist"));
map.put("duration", temp.get("duration"));
map.put("views", temp.get("views") + " views");
map.put("server", temp.get("server"));
map.put("filename", temp.get("filename"));
map.put("thumbnail", temp.get("thumbnail"));

mylist.add(map);
db_id.add(vidID.get(i));
} catch (Exception e) {
}
}
SimpleAdapter videolist = new SimpleAdapter(this, mylist,
R.layout.videorow, new String[] { "id", "title", "views",
"duration", "description", "server", "filename",
"thumbnail"}, new int[] { R.id.TextView05,
R.id.TextView01, R.id.TextView02, R.id.TextView03,
R.id.TextView04, R.id.TextView06, R.id.TextView07,
R.id.TextView08 });
list.setAdapter(videolist);
list.setOnItemClickListener(this);
list.setTextFilterEnabled(true);
list.setOnHierarchyChangeListener(this);
}

@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

TextView filename = (TextView) arg1.findViewById(R.id.TextView07);
String url = filename.getText().toString();
Log.e(".", url);
Bundle bundle = new Bundle();
bundle.putString("URL", url);
Intent myIntent = new Intent();
myIntent.setClassName("Package Name", "Package Name With Class");
myIntent.putExtras(bundle);
startActivity(myIntent);
}


@Override
public void onChildViewAdded(View parent, View child) {
View row = child;
try {
ImageView imView = (ImageView) row.findViewById(R.id.ImageView01);
imView.setImageBitmap(null);
TextView id = (TextView) row.findViewById(R.id.TextView05);
TextView thumbnail = (TextView) row.findViewById(R.id.TextView08);
TextView filename = (TextView) row.findViewById(R.id.TextView07);
imView.setTag(filename.getText().toString());
String imgurl = thumbnail.getText().toString();
grabURL(imgurl, imView, filename.getText().toString());

ImageView addFav = (ImageView) row.findViewById(R.id.ImageView02);
addFav.setTag(id.getText().toString());
addFav.setOnClickListener(myListner);
} catch (Exception e) {
}
}
@Override
public void onChildViewRemoved(View parent, View child) {

try {
View row = child;
ImageView imView = (ImageView) row.findViewById(R.id.ImageView02);
imView.setTag(null);
imView.setImageBitmap(null);
} catch (Exception e) {
}
}


public void grabURL(String url, ImageView iv, String name) {
GrabURL a = new GrabURL();
a.imgView1 = iv;
a.name = name;
a.execute(url);
}

private class GrabURL extends AsyncTask {
ImageView imgView1;
String name;

protected void onPreExecute() {
}

protected Bitmap doInBackground(String... urls) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(urls[0]);
} catch (MalformedURLException e) {
}

try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);

} catch (Exception e) {
Log.e(".", e.getMessage());
}
return bmImg;
}

protected void onPostExecute(Bitmap bm) {
if (name == imgView1.getTag().toString())
imgView1.setImageBitmap(bm);
}

}

No comments:

Post a Comment