I have created the Adapter class which extends the BaseAdapter and i also the class to query the DB. Now i am stock at connecting both result and the custom layout i Just don't know how to achieve it. If anybody could kindly help
Adapter Class
public class DrugAdapter extends BaseAdapter{
private static LayoutInflater lf = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
public DrugAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
HashMap<String, String> drug = new HashMap<String, String>();
drug = data.get(position);
// Setting all values in list view
//title.setText());
//description.setText(song.get(CustomizedListView.KEY_ARTIST));
return vi;
}
}
the method in the DBHandler Class that returns the Result as a list from the DB
//Get all drugs from the DB
public List<Drug> getAllDrugs(){
List<Drug> drugList = new ArrayList<Drug>();
String selectQuery = "SELECT * FROM " + TABLE_DRUGS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
// Adding contact to list
drugList.add(drug);
} while (cursor.moveToNext());
}
return drugList;
}
Drug Object Class
public class Drug {
int id;
String name;
String descri;
public Drug(int id, String name, String desc) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
descri = desc;
}
public Drug(String name, String desc){
this.name = name;
descri = desc;
}
public Drug() {
// TODO Auto-generated constructor stub
}
public int getID() {
// TODO Auto-generated method stub
return this.id;
}
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
public String get_desc() {
// TODO Auto-generated method stub
return descri;
}
public void setID(int newId) {
// TODO Auto-generated method stub
this.id = newId;
}
public void setName(String newName) {
// TODO Auto-generated method stub
this.name = newName;
}
public void setDesc(String newDesc) {
descri = newDesc;
}
}
Please any idea how i am suppose to set the ListView Adapter..
i Have tried some few tricks like this
List k =db.getAllDrugs();
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
ls.setAdapter(adap);
just show that the data is being read from the table.. that's where i get stuck
Code for the getView Method
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
TextView id = (TextView) vi.findViewById(R.id.drug_id);
Drug drug = data.get(position);
// Setting all values in list view
title.setText(drug.getName());
description.setText(drug.get_desc());
id.setText("h");
return vi;
}
The last setText is flagging the error..
Start by replacing List k =db.getAllDrugs(); by List<Drug> k =db.getAllDrugs();
Then, your compiler will complain about your adapter being a String adapter. At this point replace
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
with
DrugAdapter adap = new DrugAdapter(this, k);
Compiler will now complain that your list is not the right type. Change references to ArrayList<HashMap<String, String>> data in DrugAdapter with List<Drug> data.
Then properly implement getItem :
public Drug getItem(int position) {
return data.get(position);
}
In getView declare drug like so:
Drug drug = data.get(position);
Then do whatever you have to do to put the content of drug in the view.
You should study a bit more of ArrayList, you are missing something very simple.
You must set an adapter for your ListView, like this:
myListView.setAdapter(new DrugAdapter(getActivity(), getAllDrugs()));
Also, the second argument on your DrugAdapter class is wrong, if your getAllDrugs() returns List<Drug> object, your DrugAdapter constructor must be as the following:
public DrugAdapter(Activity a, ArrayList<Drug> drugList);
Your DrugAdapter class must be something like this:
public class DrugAdapter extends BaseAdapter{
private LayoutInflater lf = null;
private Activity activity;
private ArrayList<Drug> dl;
public DrugAdapter(Activity a, ArrayList<Drug> drugList){
activity = a;
dl = drugList;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return dl.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
title.setText(dl.get(position).getName());
//use dl.get(position).yourMethodName() to get its value accordingly to your needs
}
}
Remember: this code is not performance enhanced, I didn't enhanced it because it is not what you're asking, try to figure out what you may do to improve its performance later, but first, try to understand about ArrayList.
It's not usually necessary for your custom adapter to inherit from BaseAdapter. There are many Adapter implementations in the framework that take care of a bunch of boilerplate. In your case, you should be using a CursorAdapter for performance reasons. Cursors are optimized for the platform and dumping them to arrays is wasteful for any non-tiny result.
public class DrugAdapter extends CursorAdapter {
public DrugAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public Object getItem(int position) {
// This allows us to get proper Drug objects from the adapter
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
return drug;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// We only need to create the new view here
final LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.single_list_item, parent);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// This is where we populate the list item data
((TextView) view.findViewById(R.id.drug_title)).setText(cursor.getString(1));
((TextView) view.findViewById(R.id.drug_description)).setText(cursor.getString(2));
}
}
You'll need to pass the List which contains the data from the database to the Adapter. Then use the data from the list inside your getView() method. You can get rid of the HashMap I think.
Related
Hi I can not delete a row from list View i don't know how to delete it i search a lot from Internet i try out many examples but yet i can't solve the issue . when i click the hldr.delete button the complete row of list View delete from the list solution pls.
PlaceOrder Activity
public class PlaceOrder extends Activity {
String [] pIds;
String [] pNames;
String [] pPrizes;
static ListView lv;
ImageView bck;
String [] listImages;
String food_id;
String userdata[];
Intent i;
TextView totalprze;
float tprize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_order);
lv=(ListView)findViewById(R.id.cart_list);
bck=(ImageView) findViewById(R.id.placeholder_bg_btn);
totalprze =(TextView) findViewById(R.id.place_order_price);
i=new Intent(this,Menu.class);
bck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle=new Bundle();
//bundle.putStringArray("images", ListImages);
bundle.putString("food_id", food_id);
bundle.putStringArray("images", listImages);
bundle.putStringArray("userData",userdata);
i.putExtras(bundle);
startActivity(i);
}
});
if(this.getIntent().getExtras()!=null)
{
Bundle b=this.getIntent().getExtras();
pIds=b.getStringArray("pId");
pNames=b.getStringArray("PName");
pPrizes=b.getStringArray("pPrize");
userdata=b.getStringArray("userData");
tprize=b.getFloat("totalprize");
food_id=b.getString("food_id");
listImages=b.getStringArray("images");
String prz=Float.toString(tprize);
totalprze.setText("$"+prz);
lv.setAdapter(new cartAdapter(PlaceOrder.this, pIds, pNames, pPrizes,userdata));
pIds=null;
pNames=null;
pPrizes=null;
}
}
public void onBackPressed() {
pIds=null;
pNames=null;
pPrizes=null;
}
}
CartAdapter
public class cartAdapter extends BaseAdapter{
String [] pIdz;
String [] pNamz;
String [] pPrizs;
String [] userData;
// List<String> imges;
Context context;
// private ShopingCartHelper obj;
private static LayoutInflater inflater=null;
JSONArray jCat = null;
int count=0;
ProgressDialog pDialog;
public cartAdapter(PlaceOrder ctx,
String[] pIds,String[] pNams, String[] pprise,String [] userdata) {
pIdz=pIds;
pNamz=pNams;
context=ctx;
pPrizs=pprise;
userData=userdata;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
if(pIdz==null){
Toast.makeText(context, "There is issue with net connection.", Toast.LENGTH_LONG).show();
//Intent i=new Intent(context,WelcomeActivity.class);
//context.startActivity(i);
return count ;
}else{
return pIdz.length;
}
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class holder{
TextView pid;
TextView pname;
TextView pprise;
Button delete;
ListView lv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final holder hldr=new holder();
View rowView = null;
Bitmap bitmap = null;
rowView = inflater.inflate(R.layout.place_order_item_list, null);
hldr.pid=(TextView) rowView.findViewById(R.id.item_id);
hldr.pname=(TextView) rowView.findViewById(R.id.item_name);
hldr.pprise=(TextView) rowView.findViewById(R.id.item_price);
hldr.delete=(Button) rowView.findViewById(R.id.delete);
hldr.pid.setText(pIdz[position]);
hldr.pname.setText(pNamz[position]);
hldr.pprise.setText(pPrizs[position]);
/* convertView.setTag(hldr);
hldr.delete.setTag(hldr);*/
//
// Picasso.with(context).load(imgs[position]).into(hldr.img);
hldr.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// rowView.remove(position); //removing from your List
//Toast.makeText(context, "Delete",Toast.LENGTH_LONG).show();
int pid=Integer.parseInt(hldr.pid.getText().toString());
//Toast.makeText(context, "pid"+pid, Toast.LENGTH_LONG).show();
new ShopingCartHelper(context).delProduct(pid);
//PlaceOrder.lv.removeViewAt(position);
notifyDataSetChanged();
}
});
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// // TODO Auto-generated method stub
// cartAdapter.this.pIdz.remove[position];
// Toast.makeText(context, "hi",Toast.LENGTH_LONG).show();
}
});
return rowView;
// TODO Auto-generated method stub
}
}
You can't remove anything from an array - they're always fixed length. Once you've created an array of length 3, that array will always have length 3. And if you want to delete from the listview the array which you are used should be dynamic(Adding & Deleting). So make the pIdz; pNamz;pPrizs;userData into some Modal Object, and prepare the list of Modal Objects and pass it to adapter and make the life easier
You'd be better off with a List, e.g. an ArrayList:
hldr.delete.setTag(position);
hldr.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (int)v.getTag();
list.remove(position);
notifyDataSetChanged();
}
});
First.
You should make a object that stores all your different strings instead of using multiple arrays for each value.
Ej:
class MyClass
{
String id;
String name
String price;
public MyClass(String id, String name, String price)
{
this.id = id;
this.name = name;
this.price = price;
}
}
That way you wouldnt have to delete the position of each of the arrays.
And finally to answer your question, just delete the value of position you want to delete for each of the arrays.
Since they are Array and not ArrayList you will need to recreate it with the correct new dimension (Note it will be much easier with ArrayList).
Then just call notifyDataSetChanged() on your Adapter class.
Hope this helps.
This is the first time I am building a Listview with a custom layout, so in case I have missed something obvious please just point it out.
The problem I am having that I cannot get the listview to update itself with new information after the Oncreate(); has been used. So the list is very static.
I am trying to create a custom listview adapter that looks as such:
public class MainListCustomBaseAdapter extends BaseAdapter {
static ArrayList<ListItems> DataSomething;
static Context Cont;
public MainListCustomBaseAdapter (ArrayList<ListItems> data, Context c){
DataSomething = data;
Cont = c;
}
public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}
ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
TextView DetailItemView = (TextView)v.findViewById(R.id.DetailItem);
ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
DetailItemView.setText("ItemDetails: "+msg.ItemDetails);
return v;
}
public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();
}
}
My Oncreate looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecipeList = (ListView) findViewById(R.id.mainListView);
ShoppingItems = new ArrayList<ListItems>();
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));
ListItems Detail;
Detail = new ListItems();
Detail.setIcon(R.drawable.food);
Detail.setName("Food Stuff");
Detail.setItemDetails("ItemDetailsComp");
ShoppingItems.add(Detail);
}
and listitem looks like this:
public class ListItems {
public int icon ;
public String title;
public String ItemDetails;
public String getName() {
return title;
}
public void setName(String from) {
this.title = from;
}
public String getItemDetails() {
return ItemDetails;
}
public void setItemDetails(String ItemDetailsComp) {
this.ItemDetails = ItemDetailsComp;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
}
How do I get the listview to update dynamically? with maybe a SetInvalidatedViews() or notifyDatasetchanged()?
Any help is deeply appreciated.
Use the below
MainListCustomBaseAdapter adapter = new MainListCustomBaseAdapter(ShoppingItems, this)
RecipeList.setAdapter(adapter);
To refresh or update listview
adapter.notifyDataSetChanged();
public void notifyDataSetChanged ()
Added in API level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.
put this line after adding the element in the arraylist
RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));
I'm unable to find out why am I getting this exception despite initializing the array with a size. When I try to increase the size of the array this exception occurs.Here is my code. Can anybody suggest what am I doing wrong?
public class MsgAdapter extends BaseAdapter {
public String msgs[]=new String[150];
public Activity context;
public LayoutInflater inflater;
int count=16;
public MsgAdapter(Activity context,String[] msgs) {
super();
this.context = context;
this.msgs = msgs;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder
{
TextView msgView;
ImageView b;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
Log.v("count", ""+msgs.length);
return msgs.length+16;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
View v;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null){
convertView = inflater.inflate(R.layout.msg_list, null);
holder = new ViewHolder();
holder.msgView=(TextView)convertView.findViewById(R.id.msgtext);
holder.b=(ImageView)convertView.findViewById(R.id.sms);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", holder.msgView.getText().toString());
sendIntent.setType("vnd.android-dir/mms-sms");
v.getContext().startActivity(sendIntent);
}
});
holder.msgView.setText(msgs[position]);//the exception occurs here
return convertView;
}
}
I'll suggest you to use ArrayList<String> msg instead of using String []msg, because the size of ArrayList can be dynamically increased and decreased.
There is another bug (may be.)
you initialize your array in MsgAdapter like this.
public String msgs[]=new String[150];
and now you set another String array to msgs again here.
public MsgAdapter(Activity context,String[] msgs) {
...
this.msgs = msgs;
...
}
In getCount() method you are returning size of list msgs.length+16 . Therefore getView() method of adapter will be called more than value of msgs.length and this line
holder.msgView.setText(msgs[position]);
will throw exception when value of position is more that or equal to msgs.length .
exception is most probably on the line:
holder.msgView.setText(msgs[position]);
and its beczuse you are returning in getCount is msg.length+18, but array msg is still of length msg.length, hope its clear nw
I need to display my arraylist data in listview. My arraylist is of type public static ArrayList<ArrayList<String>> ourstringList1. In my listadapter class I am trying to get the data from arraylist and setting it to tesxtview. But since I need arr.get(i).get(j)...I am unable to proceed further.
Please help me regarding this...
My code:
public class testreview extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1;
private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listViewScore=(ListView)findViewById(R.id.list);
usernameArrLst = new ArrayList<ArrayList<String>>();
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArrLst.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(testreview.this);
rowView=layoutinflate.inflate(R.layout.listrow, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case);
textViewName.setText((CharSequence) usernameArrLst.get(position));
return rowView;
}
}
}
Thanks in advance
for this purpose i think you need to write your custom adapter and set to the list
Get The Idea
Hope this will help you.
Sorry, did not see you had imbricated ArrayList. To get the elements you need, use class casting to get to the inner ArrayList and iterate trough them.
ArrayList<ArrayList<String>> stringList;
stringList = ourStringList1;
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> innerStringList = (ArrayList<String>) stringList.get(i);
for (int j = 0; j < innerStringList.size(); j++) {
String value = (String) innerStringList.get(j);
// put the value in the textView
}
}
When you build your Adapter class, create a class attribute that will hold the array of ArrayList and initialize it in the constructor.
Hope this helps. If you need further explanation let me know.
sweety if u have showed us what you have tried then it should have been better.But as per my understanding ur code should look like this :
public class TestProjeectActivity extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
private String[] usernameArr = null;
private ArrayList<String> usernameArrLst = null;
//private Helper helper = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewScore=(ListView)findViewById(R.id.listViewScore);
//helper = new Helper(TestProjeectActivity.this);
//usernameArr = helper.getUserName();
usernameArr = new String[]{"Alan","Bob"};
usernameArrLst = new ArrayList<String>(Arrays.asList(usernameArr));//Changed line
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArr.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(TestProjeectActivity.this);
rowView=layoutinflate.inflate(R.layout.listviewtext, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.textViewName);
textViewName.setText(usernameArr.get(position));
return rowView;
}
}
}
I had done the program the display all the images from the sdcard dynamically. But now ,I want to display single image dynamically from the sdcard instead of display all images .
my coding is as follows
public class Gallery1Activity extends Activity {
// private ArrayList<String> imglist;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
ArrayList arr = new ArrayList();
super.onCreate(savedInstanceState);
setContentView(R.layout.gallerygrid);
GridView gv1=(GridView) this.findViewById(R.id.gridView1);
//gv1.setAdapter(new galleryImageAdapter(this));
arr = galldatabase();
gv1.setAdapter(new galleryImageAdapter(this,arr));
}
private ArrayList galldatabase() {
// TODO Auto-generated method stub
ArrayList ThumbsIDList = new ArrayList();
//Uri u=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
/*String[] projection =new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA,
Images.Thumbnails.IMAGE_ID};*/
Cursor galleryimagecursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA} , null, null, null);
if(galleryimagecursor!=null&&galleryimagecursor.moveToFirst()){
String thumbsID;
String thumbsImageID;
String thumbsData;
int num=0;
do{
thumbsID=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID));
thumbsData=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails.DATA));
Log.i("BMP","size "+thumbsID+" "+thumbsData);
num++;
/*if(thumbsImageID!= null) {*/
ThumbsIDList.add(thumbsID);
/*ThumbsImageIDList.add(galleryimagecursor.getString(thumbsImageIDcol));
ThumbsDataList.add(galleryimagecursor.getString(thumbsDataCol));
}*/
}
while(galleryimagecursor.moveToNext());
}
return ThumbsIDList;
}
}
then the adapter code follows
public class galleryImageAdapter extends BaseAdapter {
Context con;
private ArrayList<String> imgList;
private String thumbsID;
public galleryImageAdapter(Context c,ArrayList arr){
con=c;
imgList = arr;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if( convertView==null){
LayoutInflater li;
li = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.galleryadapter,null);
final ImageView iv1=(ImageView)v.findViewById(R.id.galimage);
TextView tv1=(TextView)v.findViewById(R.id.galimagtext);
tv1.setText("Image"+position);
Log.d("imagevalue",imgList.get(position));
iv1.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imgList.get(position)/*galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID)*//*imgList.get(position)*/));
}
else
v=convertView;
return v;
}
}
you have arraylist of thumb id of images using arr = galldatabase(); create another arraylist (newarr) which will have only 1 element. if you want to show 2nd image just copy thumb id of that image from arr arr and store it in new array list
example
ArrayList newarr = new ArrayList();
newarr.add(arr.get(random position));
Assign this list to adapter instead of assigning to original array list which contains list of all image
use
gv1.setAdapter(new galleryImageAdapter(this,newarr));