Return filename from SimpleCursorAdapter to Activity - android

I wanna return a particular filename from a simplecursoradapter back to the activity from which it was called.
I tried this link How communicate between Adapter and Activity and also this one Data between activity and adapter
But either didnt hel p me. I am posting both the activity code and adapter code here.Kindly help
Activity
videolist = (ListView) findViewById(R.id.VideoMusicList);
videolist.setAdapter(adapter);
//Adapter
public class SdCardAdapter extends SimpleCursorAdapter {
static final String TAG = "[SongListAdapter]";
int position;
CheckBox media_selected;
final String SETTING_TODOLIST = "todolist";
private String chk;
private Context context;
private Object itemText;
private ArrayList<string> selectedItems = new ArrayList<string>();
// String file;
private Object convertView;
// private final List<Model> list;
int count;
ListView listview;
private List<Model> list;
private Cursor videocursor;
private LayoutInflater mInflater;
private OnClickListener mClick;
private OnCheckedChangeListener mChecked;
String file_rel_path;
String file_abs_path;
String last_file;
/**
* The Class ViewHolder.
*/
static class ViewHolder {
/** The sdcard_item. Layout of each item in the list view */
RelativeLayout sdcard_item;
LinearLayout sdcard;
/** The title. Textview to display the song title */
TextView media_name;
CheckBox media_selected;
ListView listview;
int position;
}
public SdCardAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View v = super.newView(context, cursor, parent);
final Cursor filecursor = cursor;
final ViewHolder vh = new ViewHolder();
vh.media_name = (TextView) v.findViewById(R.id.sdcard_title);
position = cursor.getPosition();
count = cursor.getCount();
vh.media_selected = (CheckBox) v.findViewById(R.id.sdcard_checkbox);
vh.media_selected
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
System.out.println("checkbox ckicked......");
if (vh.media_selected.isChecked()) {
vh.media_selected.setId(1);
vh.media_selected.getId();
last_file = file_rel_path;
file_rel_path = vh.media_name.getText().toString();
Log.d("filename_........", file_rel_path);
v.setBackgroundColor(Color.GRAY);
} else if (!vh.media_selected.isChecked()) {
file_rel_path="";
vh.media_selected.setId(0);
vh.media_selected.getId();
v.setBackgroundColor(Color.BLACK);
}
Log.d("Position", "" + position);
for (int i = 0; i < count; i++) {
filecursor.moveToPosition(i);
file_abs_path = filecursor.getString(filecursor
.getColumnIndex(MediaStore.Video.Media.TITLE));
if (file_abs_path.equals(file_rel_path)) {
String file_path = filecursor.getString(filecursor
.getColumnIndex(MediaStore.Video.Media.DATA));
Log.d("filename.......", file_path);
}
}
}
});
vh.sdcard_item = (RelativeLayout) v.findViewById(R.id.sdcard_item);
v.setTag(vh);
return v;
}
}
//This is the string that i want to pass to my previous activity
String file_path = filecursor.getString(filecursor.getColumnIndex(MediaStore.Video.Media.DATA));

Use interface callBack :
In SdCardAdapter class create interface :
public interface SdCardAdapterListener
{
public void sendFilePath(String path);
}
Add a listener param in SdCardAdapter constructor :
private SdCardAdapterListener delegate;
public SdCardAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, SdCardAdapterListener delegate)
{
super(context, layout, c, from, to);
this.delegate = delegate;
}
In Activity :
SdCardAdapter adapter = new SdCardAdapter(this, layout, c, from, to, new SdCardAdapterListener()
{
#Override
public void sendFilePath(String path)
{
// do something with path
}
});
videolist = (ListView) findViewById(R.id.VideoMusicList);
videolist.setAdapter(adapter);
Then, simply call delegate.sendFilePath(path) in SdCardAdapter for send to sendFilePath method in your Activity.

Related

how to delete a row from a ListView with a delete button in the row

I have a class which extends ArrayAdapter<String>. I want to have a delete image button deletes the particular row.. This is my code:
public class ViewCartList extends ArrayAdapter<String> {
private String[] cart_item_name;
private String[] cart_item_quan;
private String[] cart_item_price;
private Activity context;
public ViewCartList(Activity context, String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
super(context,R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan[position]);
textViewItemName.setText(cart_item_name[position]);
textViewItemPrice.setText(cart_item_price[position]);
imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
}
});
return listViewItem;
}
}
And This is my Basket class where I am using this above adapter class.
What to do if i want to remove a particular row from the ListView from that delete button given above..
public class Basket extends AppCompatActivity implements View.OnClickListener {
TextView cartview;
MyCartDatabse myDatabase;
SQLiteDatabase sql;
ContentValues cv;
String wr;
ListView list;
public static String[] quan =null;
public static String[] itemname=null;
public static String[] baseprice=null;
TextView edit_order;
public boolean imrow;
ViewCartList vc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basket);
myDatabase = new MyCartDatabse(this,"mydb",null,1);
sql = myDatabase.getWritableDatabase();
cv = new ContentValues();
list = (ListView)findViewById(R.id.listcart);
getRecords();
edit_order = (TextView)findViewById(R.id.edit_order);
edit_order.setOnClickListener(this);
}
public void getRecords(){
sql = myDatabase.getReadableDatabase();
Cursor cursor = sql.rawQuery("select * from cart ",null);
quan = new String[cursor.getCount()];
itemname = new String[cursor.getCount()];
baseprice = new String[cursor.getCount()];
int i = 0;
if(cursor.getCount()>0){
while(cursor.moveToNext()){
String uquan = cursor.getString(5);
String uname = cursor.getString(1);
String uprice = cursor.getString(4);
quan[i] = uquan;
itemname[i] = uname;
baseprice[i] = uprice;
i++;
}
vc = new ViewCartList(this,quan,itemname,baseprice);
list.setAdapter(vc);
}
else{
// Do something
}
cursor.close();
}
#Override
public void onClick(View view) {
edit_order.setText("Apply Changes");
}
}
The elegant approach for your problem is to pass an ArrayList of object to your Adapter and then handle the delete action.
So you might consider creating an object like this.
public class Cart {
public String cart_item_name;
public String cart_item_quan;
public String cart_item_price;
}
Now take an ArrayList of that class and populate the ArrayList to pass to your Adapter.
public class ViewCartList extends BaseAdapter {
private Context context;
private ArrayList<Cart> mCartList;
public ViewCartList(Context context, ArrayList<Cart> mCartList) {
this.context = context;
this.mCartList = mCartList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(mCartList.get(position).cart_item_quan);
textViewItemName.setText(mCartList.get(position).cart_item_name);
textViewItemPrice.setText(mCartList.get(position).cart_item_price);
// Remove the following and set the visibility true from the layout.
// imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Delete the row
mCartList.remove(position);
notifyDatasetChanged();
}
});
return listViewItem;
}
}
If you want to completely delete the item you should consider using List<String> instead of Array
Here is updated code for List
public class ViewCartList extends ArrayAdapter<String> {
private List<String> cart_item_name; // Use List here
private List<String> cart_item_quan; // Use List here
private List<String> cart_item_price; // Use List here
private Activity context;
public ViewCartList(Activity context, List<String> cartitemquan, List<String> cartitemname,List<String> cartitemprice){
super(context, R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan.get(position));
textViewItemName.setText(cart_item_name.get(position));
textViewItemPrice.setText(cart_item_price.get(position));
imcut.setVisibility(View.VISIBLE);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
cart_item_quan.remove(position);
cart_item_name.remove(position);
cart_item_price.remove(position);
notifyDataSetChanged();
}
});
return listViewItem;
}
}
Arrays are fixed length and can not be resized once created. You can set an element to null to remove an object reference;
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
cart_item_name[position] = null;
cart_item_quan[position]= null;
cart_item_price[position] = null;
notifydatasetchanged();
}
});
public class ViewCartList extends ArrayAdapter<String> {
private String[] cart_item_name;
private String[] cart_item_quan;
private String[] cart_item_price;
private Activity context;
public ViewCartList(Activity context, String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
super(context,R.layout.viewcartlist,cartitemquan);
this.context = context;
this.cart_item_quan = cartitemquan;
this.cart_item_name = cartitemname;
this.cart_item_price = cartitemprice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);
TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);
textViewItemQuan.setText(cart_item_quan[position]);
textViewItemName.setText(cart_item_name[position]);
textViewItemPrice.setText(cart_item_price[position]);
imcut.setVisibility(View.VISIBLE);
imcut.setTag(position);
imcut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// I want to delete that particular row
int pos = Integer.parseInt(view.getTag().toString());
mCartList.remove(position);
notifyDatasetChanged();
}
});
return listViewItem;
}
}

Android: onClickListener in the ArrayAdapter not responding

I created an activity, created the list view and set the setItemsCanForus() method to true so that each view in the adapter can have its own onclicklisteners. I managed to make the list but now the views don't respond to the clicks.
Here's the part of the activity.
private ArrayList<String> filePath;
private ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_video);
**filePath = new ArrayList<>();
lv = (ListView) findViewById(R.id.find_video_list);
fetchVideoList();
lv.setItemsCanFocus(true);**
}
private void fetchVideoList() {
String[] projection = {
MediaStore.Video.Thumbnails._ID
};
// Create the cursor pointing to the SDCard
Cursor cursor = getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Video.Thumbnails.VIDEO_ID);
assert cursor != null;
cursor.close();
filePath = getFilePaths();
VideoFromDeviceItem adapter = new VideoFromDeviceItem(FindVideoActivity.this, filePath);
**lv.setAdapter(adapter);**
}
And here is the class that extends the ArrayAdapter<String> class.
public class VideoFromDeviceItem extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> filePaths;
public VideoFromDeviceItem(Activity context, ArrayList<String> filePaths) {
super(context, R.layout.video_item, filePaths);
this.context = context;
this.filePaths = filePaths;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.upload_list, null, true);
VideoView videoItem = (VideoView) rowView.findViewById(R.id.videoItem);
TextView tvFilePath = (TextView) rowView.findViewById(R.id.tvFilePath);
TextView tvVideoRunningTime = (TextView) rowView.findViewById(R.id.tvVideoRunningTime);
videoItem.setVideoPath(filePaths.get(position));
videoItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, position, Toast.LENGTH_SHORT).show();
}
});
// remove the file path and simply get the file name.
String fullPath = filePaths.get(position);
String shortName = new File(fullPath).getName();
tvFilePath.setText(shortName);
// get the running time of the video.
long runningTime = getVideoRunningTime(filePaths.get(position));
String strRunningTime = fromMillisecToFormattedTime(runningTime);
tvVideoRunningTime.setText(strRunningTime);
return rowView;
}
As you can see above, I set an OnClickListener() to a video view with a toast message just to test if the listener is actually responding. And there's no response from the clicks.
Try chaning this :
private final Activity context;
to this :
private final Context context;
And your constructor to this :
public VideoFromDeviceItem(Context context, ArrayList<String> filePaths) {
super(context, R.layout.video_item, filePaths);
this.context = context;
this.filePaths = filePaths;
}
EDIT
You can use this instead :
videoItem.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
return false;
}
});
Why the onTouchListener() not onClickListener() in this case?
There are some bugs with onClickListener() since it's a VideoView... so the best way to do the onClickListener() is the onTouchListener() BUT if you want the onClickListener() just add a Button inside of your VideoView or wherever and you can set the setOnClickListener() on it .

Delete selected checkboxes

I am trying to get the selected checkboxes deleted. I have no clue what to do after this code below:
I am using a simple cursor adapter. I also want to know how I could select and deselect all the checkboxes. I had researched everything but can't find any answer. I've been stuck for 3 days. ALso, Im not sure if the code below is even the correct path i need.
public class SecondCheckedListView extends ListActivity {
private Cursor mCursor;
private Long mRowId;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondcheckedlistview);
db.open();
fillData();
}
private void fillData() {
mCursor = db.getAllContacts();
startManagingCursor(mCursor);
String[] from = new String[]{DBAdapter.KEY_FIRSTNAME, DBAdapter.KEY_LASTNAME};
int[] to = new int[]{R.id.textView1, R.id.textView2};
SimpleCursorAdapter d =
new SimpleCursorAdapter(this, R.layout.rowcheckedlistview, mCursor, from, to);
setListAdapter(d);
}
public class MyAdapter extends SimpleCursorAdapter {
private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final int mNameIndex;
private final int mIdIndex;
private final LayoutInflater mLayoutInflater;
private final class ViewHolder {
public TextView firstname;
public TextView lastname;
public CheckBox cBox;
}
public MyAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
this.mIdIndex = mCursor.getColumnIndex(DBAdapter.ROW_ID);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(pos)) {
ViewHolder viewHolder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.firstname = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.lastname = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.cBox = (CheckBox) convertView.findViewById(R.id.bcheck);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
String firstname = mCursor.getString(mNameIndex);
String rowId = mCursor.getString(mIdIndex);
}
return convertView;
}
}
}
what do you mean "delete"? You want to remove the checkbox from the listview? You might need
to do something like
viewHolder.RemoveView(cBox);
If you want to select/delsect all checkboxes, you'll probably want that based upon some other botton or checkbox. I do that with another checkbox like so:
private OnClickListener setSelectAllListener = new OnClickListener() {
#Override
public void onClick(View v) {
selectAllTouched = true;
if (!selectAllCheck.isChecked()) {
selectAll = false;
studs.notifyDataSetChanged();
}
else {
selectAll = true;
studs.notifyDataSetChanged();
}
}
};
And then in my custom adapter that does stuff with each checkbox in a listview, I use this:
if (selectAllTouched) {
if(selectAll){
holder.here.setChecked(true);
itemCheckedHere.set(pos, true);
int who= new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 1, attendDate );
}
else{
holder.here.setChecked(false);
itemCheckedHere.set(pos, false);
int who = new Integer(holder.text2.getText().toString());
mDbHelper.updateAttend(who, classnum, ATTEND, 0, attendDate );
}
}
I used a sparse array, itemCheckedHere, to hold the values of all the check boxes so scrolling doesn't change the values on resued views.

Android: Do some action AFTER filling ListView elements

I have an activity with listview and attached footer. I created my custom adapter for filling ListView. After I filled it I need to do some actions.
This is my activity's code part:
public class MainActivity extends ListActivity {
final public String NO_USERS_IN_DB = "No users yet, please add some...";
DBAdapter db = new DBAdapter(this);
Activity activity = MainActivity.this;
private static final Pattern PATTERN = Pattern.compile(": *([^|]+)");
private static final int REQUEST_LOAD = 0;
boolean rewrite;
boolean userSelected = false;
final public int REQUEST_SAVE = 1;
boolean showAvatars;
boolean settingsGot = false;
int backgroundColor;
int titleColor;
int buttonBackgroundColor;
int buttonTextColor;
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
if(!settingsGot){
getSettings();
settingsGot = true;
}
String[] users = getUsers();
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.main, parent, false);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(users[position]);
if(showAvatars){
ImageView icon=(ImageView)row.findViewById(R.id.icon);
byte[] bb = getAvatar(users[position]);
if(bb != null && bb.length != 0){
icon.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
icon.setVisibility(View.VISIBLE);
}
}
return row;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
db.open();
String[] users = db.getUsersList();
boolean showList = true;
if (users[0].equals("NOUSERSINTHEBASE")){
users[0] = NO_USERS_IN_DB;
}
if (showList){
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
this.setListAdapter(new MyCustomAdapter(this,
R.layout.main, users));
}else{
setContentView(R.layout.footer);
}
db.close();
}
So, method I need to do some actions after method getView done its work. Where should I place my doSomeMoreActionsAfterGetView() ?
After setListAdapter you can change it...

Data in ListActivity not cached when going back to it

I've got a ListActivity filled by an adapter extending SimpleCursorAdapter. Some category buttons can be clicked, which fills the listview with a list of articles in the corresponding category. When clicking on an item in the list a new activity is launched which shows the detail of the article.
Now, when pressing the back button in that article detail page, no data is shown. Does it have to reload the data from the DB? Isn't the data supposed to be cached?
Moreover, when going back to the myListActivity, onCreate is not called. Only onResume is called. Shall I do everything in onResume? In that case what's the best way to keep the value of the current category and retrieve it when going back to the ListActivity from the article detail?
Thanks
public class Home extends ListActivity implements OnItemClickListener{
private DBAdapter dbAdapter;
private HomeListAdapter adapter; //extends SimpleCursorAdapter
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
// open database
dbAdapter = new DBAdapter(this);
dbAdapter.open();
/* more stuff */
getArticlesbyCat(category);
/* more stuff */
}
private void getArticles(int category){
Cursor c = dbAdapter.fetchArtByCat(category);
c.moveToFirst();
startManagingCursor(c);
String[] from = new String[] {};
int[] to = new int[] {};
setListAdapter(new HomeListAdapter(this, R.layout.article_row, c, from, to));
adapter.notifyDataSetChanged();
c.close();
}
#Override
public void onItemClick(AdapterView arg0, View v, int position, long arg3) {
/* Go to article page */
}
#Override
protected void onPause() {
super.onPause();
// close database
dbAdapter.close();
}
}
EDIT
public class HomeListAdapter extends SimpleCursorAdapter {
private int mLayout;
private Cursor mCursor;
private Resources mResources;
// Column index
private int mCatInd;
private int mURLPicInd;
private int mIntroInd;
private int mTitleInd;
private LayoutInflater mLayoutInflater;
private final ImageDownloader mImageLoader;
private final class ViewHolder {
public TextView category;
public ImageView image;
public TextView intro;
public TextView title;
}
public HomeListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.mCursor = c;
this.mCatInd = mCursor.getColumnIndex(DBAdapter.KEY_CATEGORY_NAME);
this.mURLPicInd = mCursor.getColumnIndex(DBAdapter.KEY_URL_PIC);
this.mIntroInd = mCursor.getColumnIndex(DBAdapter.KEY_INTRO);
this.mTitleInd = mCursor.getColumnIndex(DBAdapter.KEY_TITLE);
this.mLayoutInflater = LayoutInflater.from(context);
mResources = context.getResources();
mImageLoader = new ImageDownloader(mResources,
((BitmapDrawable)mResources.getDrawable(R.drawable.default_row_pic)).getBitmap(), 1);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.category = (TextView) convertView.findViewById(R.id.cat);
viewHolder.image = (ImageView) convertView.findViewById(R.id.picture);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.intro = (TextView) convertView.findViewById(R.id.intro);
viewHolder.intro.setMaxLines(3);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String category = mCursor.getString(mCatInd);
String title = mCursor.getString(mTitleInd);
String intro = mCursor.getString(mIntroInd);
String url_pic = mCursor.getString(mURLPicInd);
viewHolder.category.setText(category);
viewHolder.title.setText(title);
viewHolder.intro.setText(intro);
if(!url_pic.equals("")){
mImageLoader.download(url_pic, (ImageView) viewHolder.image);
}
else {
viewHolder.image.setImageResource(R.drawable.default_row_pic);
}
}
return convertView;
}
}

Categories

Resources