I have a ListView which extends BaseAdapter. I have a array list. The ListView inflates and populates correctly. I am using the flipper view to change the layout to another but the problem is when i clicked on the first item it rotate but the item at third or sixth row will also rotate
list view how can i fix it i only want the image is change of selected item
ArrayList<product_data> al=new ArrayList<product_data>();
class product_data
{
String post_title;
String newprice;
String oldprice;
String image;
String id;
product_data(String post_title,String newprice,String oldprice,String image,String id)
{
this.post_title=post_title;
this.newprice=newprice;
this.oldprice=oldprice;
this.image=image;
this.id=id;
}
}
to add data in arraylist
try {
JSONObject parentObject = new JSONObject(json.toString());
JSONObject userDetails = parentObject.getJSONObject("response");
JSONArray jarray=userDetails.getJSONArray("products_data");
for(int i=0;i<jarray.length();i++)
{
System.out.println("From the Dataaaa");
JSONObject c = jarray.getJSONObject(i);
String details=c.getString("details");
String image=c.getString("image");
String catagory=c.getString("catagory");
String new_price=c.getString("new_price");
String title=c.getString("title");
String id1=c.getString("id");
System.out.println("from ther first image"+image);
System.out.println("from the sdafdfadf"+details);
al.add(new product_data(title, new_price, new_price, image, id1));
Adapter class
class MyAdapter extends BaseAdapter
{
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View singleView, ViewGroup parent) {
// TODO Auto-generated method stub
final int ok=position;
if(singleView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
singleView = inflater.inflate(R.layout.product_ist_layout,parent,false);
System.out.println("from the if"+position);
viewAnimator = (ViewAnimator)singleView.findViewById(R.id.viewFlipper);
rootLayout = (View)singleView.findViewById(R.id.main_activity_root);
cardFace = (View)singleView.findViewById(R.id.main_activity_card_face);
cardBack = (View) singleView.findViewById(R.id.main_activity_card_back);
}
System.out.println("from the else");
System.out.println("Position in else"+position);
product_data a=al.get(position);
TextView tv1=(TextView)singleView.findViewById(R.id.textView2);
TextView tv2=(TextView)singleView.findViewById(R.id.textView3);
TextView tv3=(TextView)singleView.findViewById(R.id.textView1);
/**
* Bind a click listener to initiate the flip transitions
*/
viewAnimator.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// This is all you need to do to 3D flip
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
cardFace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("positon"+ok);
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
cardBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
imgview1=(ImageView)singleView.findViewById(R.id.imageView4);
imgview1.setTag(position);
imgview1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println(position1);
String id=al.get(position1).id;
System.out.println("id from the list"+id);
Intent in=new Intent(product_list_Activity.this,rating_dialog.class);
in.putExtra("id",id);
startActivity(in);
}
});
ImageView imgview2=(ImageView)singleView.findViewById(R.id.imageView5);
imgview2.setTag(position);
imgview2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println(position1);
String id=al.get(position1).id;
System.out.println("id from the list"+id);
Intent in=new Intent(product_list_Activity.this,product_review.class);
in.putExtra("id",id);
startActivity(in);
}
});
tv3.setTag(position);
tv3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println("from the review "+position1);
String id=al.get(position1).id;
globalvariables.product_id=id;
Intent in=new Intent(getApplicationContext(),review_product_tabbar.class);
startActivity(in);
}
});
tv1.setText(a.post_title);
tv2.setText("$"+a.oldprice);
tv1.setTypeface(tf);
tv2.setTypeface(tf);
tv3.setTypeface(tf);
int loader = R.drawable.ic_launcher;
ImageView image = (ImageView)singleView. findViewById(R.id.imageView1);
imgLoader.DisplayImage(a.image, loader, image);
Animation anim = new Rotate3dAnimation(90.0f, 0.0f, 100.0f, false, singleView);
anim.setDuration(1000l);
singleView.startAnimation(anim);
return singleView;
}
}
}
when i click on the first item it rotate and another view is coming
but when i scrolled the fourth item in below image will also rotate
i am using the on click listner on the relative views as in the code
I have one idea on click of image first try to get Position and when you get Position then set new image background at that position.
Related
i have two custom GridView, I want to set Onclick Listener for first GridView, i did it inside its adapter , working fine ... but when generate items in second GridView the onclick listener read values from second GridView when i press on item in first GridView!
The onclick listener working fine if the second GridView empty
I have no idea how to fix this
The problem with holder.tvgroup1 and holder.tvgroup_delete_icon
Thanks
First GridView Adapter
public class GroupGeneratedAdapter extends BaseAdapter {
ArrayList<String> result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
GroupGeneratedAdapter(GroupMakerG groupMakerG, ArrayList<String> UserNameinput) {
// TODO Auto-generated constructor stub
result = UserNameinput;
context = groupMakerG;
// imageId=prgmImages;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.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;
}
public class Holder {
TextView tvgroup, tvgroup1;
ImageView tvgroup_delete_icon;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
// View rowView;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_generated_original, parent, false);
}
holder.tvgroup = (TextView) convertView.findViewById(R.id.tvgroup);
holder.tvgroup1 = (TextView) convertView.findViewById(R.id.tvgroup1);
holder.tvgroup_delete_icon = (ImageView) convertView.findViewById(R.id.tvgroup_delete_icon);
// holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tvgroup.setText(String.valueOf(position + 1) + "-");
holder.tvgroup1.setText(result.get(position));
// holder.img.setImageResource(imageId[position]);
holder.tvgroup_delete_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GroupMakerG.GroupOfNames.remove(position);
GroupMakerG.MainGroup.invalidateViews();
if (GroupMakerG.arraySugestion.size() > 0) {
GroupMakerG.arraySugestion.clear();
GroupMakerG.makeSuggestions();
} else {
GroupMakerG.makeSuggestions();
}
GroupMakerG.GeneratedGroup.setAdapter(null);
GroupMakerG.No_Of_Groups.setText("");
GroupMakerG.numoftries = 0;
GroupMakerG.Group_num_tries.setText("0");
GroupMakerG.GroupNameLength.setText(String.valueOf(GroupMakerG.GroupOfNames.size()));
}
});
holder.tvgroup1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context );
builder.setTitle("Modify Item No." + " " + (position + 1));
// I'm using fragment here so I'm using getView() to provide ViewGroup
// but you can provide here any other instance of ViewGroup from your Fragment / Activity
View viewInflated = LayoutInflater.from(context).inflate(R.layout.modify_name_mainlist, (ViewGroup) parent.findViewById(android.R.id.content), false);
// Set up the input
final EditText input = (EditText) viewInflated.findViewById(R.id.client_name_input);
input.setText(GroupMakerG.GroupOfNames.get(position));
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
String m_Text = input.getText().toString();
if (m_Text.trim().equals("")) {
// Toast.makeText(context, "You should enter name", Toast.LENGTH_SHORT).show();
Toast toast = Toast.makeText(context,context.getString(R.string.group_you_should_enter_name), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
GroupMakerG.GroupOfNames.set(position, m_Text);
GroupMakerG.MainGroup.invalidateViews();
GroupMakerG.GeneratedGroup.setAdapter(null);
GroupMakerG.numoftries = 0;
GroupMakerG.Group_num_tries.setText("0");
Toast toast = Toast.makeText(context, context.getString(R.string.group_item_changed), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return false;
}
});
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "You Clicked "+ result.get(position), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
Second GridView Adapter
public class GroupGeneratedAdapterG extends BaseAdapter {
private ArrayList<String> resultg;
int[] imageId;
private static LayoutInflater inflaterg = null;
private static int generated_group_counter;
Context contextg;
GroupGeneratedAdapterG(GroupMakerG groupMaker, ArrayList<String> UserNameoutput) {
// TODO Auto-generated constructor stub
resultg = UserNameoutput;
contextg = groupMaker;
// imageId=prgmImages;
inflaterg = (LayoutInflater) contextg.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
generated_group_counter = 0;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return resultg.size();
}
#Override
public Object getItem(int positiong) {
// TODO Auto-generated method stub
return positiong;
}
#Override
public long getItemId(int positiong) {
// TODO Auto-generated method stub
return positiong;
}
public class Holder {
TextView tvgroupg, tvgroup1g;
// ImageView img;
}
#Override
public View getView(final int positiong, View convertViewg, ViewGroup parentg) {
// TODO Auto-generated method stub
generated_group_counter = generated_group_counter + 1;
Holder holderg = new Holder();
// View rowView;
if (convertViewg == null) {
convertViewg = inflaterg.inflate(R.layout.group_generated_generated, parentg, false);
}
holderg.tvgroupg = (TextView) convertViewg.findViewById(R.id.tvgroupg);
holderg.tvgroup1g = (TextView) convertViewg.findViewById(R.id.tvgroup1g);
// holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
String[] ary = resultg.get(positiong).split("!##%");
if (ary[1].length() > 4) {
if (ary[1].substring(0, 5).equals("Group")) {
holderg.tvgroupg.setText("");
holderg.tvgroup1g.setText(ary[1]);
} else {
holderg.tvgroupg.setText(ary[0]);
holderg.tvgroup1g.setText(ary[1]);
}
} else {
holderg.tvgroupg.setText(ary[0]);
holderg.tvgroup1g.setText(ary[1]);
}
convertViewg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(contextg, "You Clicked "+ String.valueOf(resultg.get(positiong)), Toast.LENGTH_LONG).show();
}
});
return convertViewg;
}
}
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.
how to know which checkbox is checked and how retrieve data of each row and save it into a file
public class MainActivity extends Activity {
ArrayList<Inforowdata> rowdata;
ArrayList<Inforowdatab> rowdatab;
ArrayList<Inforowdatac> rowdatac;
ListView l;
//String[] data={"Ram", "Shyam", "Deepak", "sabdhs", "dsbndj", "dshg", "dsnd", "dsavg", "dbash", "dbshd", "dshgd", "bsdhgs", "dsgfdsfdgsf", "sda", "sdchb", "AJdfg", "sdfjgh","data" , "sad", "dfax" };;
String result;
ArrayList<String> myArray;
Button b;
int s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l= (ListView)findViewById(R.id.list);
try {
File file = new File("/sdcard/report.csv");
if(!file.exists())
{
Toast.makeText(getApplicationContext(), "file Dont Exist", Toast.LENGTH_SHORT).show();
}
FileInputStream in = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(in));
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
l= (ListView)findViewById(R.id.list);
myArray= new ArrayList<String>();
try {
String row;
while((row= myReader.readLine())!=null)
{
result= Arrays.toString(row.split(",")).replace("[", "").replace("]", "");
myArray.add(result);
ArrayAdapter<String> a= new ArrayAdapter<String>(getApplicationContext(), R.layout.row,R.id.text,myArray);
l.setAdapter(a);
//String result=Arrays.toString(row).replace("[", "").replace("]", "").split(",");
//resultList.add(data);
//resultList.add(result);
//myStringArrayList.add(result);
//ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_xml,R.id.name, data);
//l.setAdapter(adapter);
}
myReader.close();
//Toast.makeText(getApplicationContext(), s.toString, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
rowdata= new ArrayList<MainActivity.Inforowdata>();
for(int i=0;i<myArray.size();i++)
{
rowdata.add(new Inforowdata(false,i));
}
l.setAdapter(new MyAdapter());
rowdatab= new ArrayList<MainActivity.Inforowdatab>();
for(int i=0;i<myArray.size();i++)
{
rowdatab.add(new Inforowdatab(false,i));
}
l.setAdapter(new MyAdapter());
rowdatac= new ArrayList<MainActivity.Inforowdatac>();
for(int i=0;i<myArray.size();i++)
{
rowdatac.add(new Inforowdatac(false,i));
}
l.setAdapter(new MyAdapter())
}
class Inforowdata{
public boolean isclicked=false;
public int index;
/*public String fanId;
public String strAmount;*/
public Inforowdata(boolean isclicked,int index/*,String fanId,String strAmount*/)
{
this.index=index;
this.isclicked=isclicked;
/*this.fanId=fanId;
this.strAmount=strAmount;*/
}
}
class Inforowdatab{
public boolean isclicked=false;
public int index;
public Inforowdatab(boolean isclicked,int index/*,String fanId,String strAmount*/) {
// TODO Auto-generated constructor stub
this.index=index;
this.isclicked=isclicked;
}
}
class Inforowdatac{
public boolean isclicked=false;
public int index;
public Inforowdatac(boolean isclicked,int index/*,String fanId,String strAmount*/) {
// TODO Auto-generated constructor stub
this.index=index;
this.isclicked=isclicked;
}
}
public class MyAdapter extends BaseAdapter
{
#Override
public int getCount() {
// TODO Auto-generated method stub
return myArray.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return myArray.get(arg0);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row= null;
row= View.inflate(getApplicationContext(), R.layout.row,null);
TextView tv= (TextView)row.findViewById(R.id.text);
tv.setText(myArray.get(position));
//datab= myArray.get(position);
CheckBox cba= (CheckBox)row.findViewById(R.id.a);
CheckBox cbb= (CheckBox)row.findViewById(R.id.b);
final CheckBox cbc= (CheckBox)row.findViewById(R.id.c);
cba.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rowdata.get(position).isclicked)
{
rowdata.get(position).isclicked=false;
}
else
rowdata.get(position).isclicked=true;
}}
);
if (rowdata.get(position).isclicked) {
cba.setChecked(true);
}
else {
cba.setChecked(false);
}
cbb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rowdatab.get(position).isclicked)
{
rowdatab.get(position).isclicked=false;
}
else
rowdatab.get(position).isclicked=true;
}}
);
if (rowdatab.get(position).isclicked) {
cbb.setChecked(true);
}
else {
cbb.setChecked(false);
}
cbc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rowdatac.get(position).isclicked)
{
rowdatac.get(position).isclicked=false;
}
else
rowdatac.get(position).isclicked=true;
}}
);
if (rowdatac.get(position).isclicked) {
cbc.setChecked(true);
}
else {
cbc.setChecked(false);
}
b= (Button)findViewById(R.id.button1);
return row;
}
}
To get all data from the list,
You need to create global adapter as
MyAdapter mAdapter;
onClick event of button by following code to get all data
for(Inforowdata row : mAdapter){
//All rows adata here....
}
or
for(int pos=0;mAdapter.getItemCount()-1;pos++)
mAdapter.getItem(pos)//All rows adata here....
I have a ListView which extends BaseAdapter. I have a array list. The ListView inflates and populates correctly. I am using the flipper view to change the layout to another but the problem is when i clicked on the first item it rotate but the item at third or sixth row will also rotate
list view how can i fix it i only want the image is change of selected item
ArrayList<product_data> al=new ArrayList<product_data>();
class product_data
{
String post_title;
String newprice;
String oldprice;
String image;
String id;
product_data(String post_title,String newprice,String oldprice,String image,String id)
{
this.post_title=post_title;
this.newprice=newprice;
this.oldprice=oldprice;
this.image=image;
this.id=id;
}
}
to add data in arraylist
try {
JSONObject parentObject = new JSONObject(json.toString());
JSONObject userDetails = parentObject.getJSONObject("response");
JSONArray jarray=userDetails.getJSONArray("products_data");
for(int i=0;i<jarray.length();i++)
{
System.out.println("From the Dataaaa");
JSONObject c = jarray.getJSONObject(i);
String details=c.getString("details");
String image=c.getString("image");
String catagory=c.getString("catagory");
String new_price=c.getString("new_price");
String title=c.getString("title");
String id1=c.getString("id");
System.out.println("from ther first image"+image);
System.out.println("from the sdafdfadf"+details);
al.add(new product_data(title, new_price, new_price, image, id1));
Adapter class
class MyAdapter extends BaseAdapter
{
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View singleView, ViewGroup parent) {
// TODO Auto-generated method stub
final int ok=position;
if(singleView==null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
singleView = inflater.inflate(R.layout.product_ist_layout,parent,false);
System.out.println("from the if"+position);
viewAnimator = (ViewAnimator)singleView.findViewById(R.id.viewFlipper);
rootLayout = (View)singleView.findViewById(R.id.main_activity_root);
cardFace = (View)singleView.findViewById(R.id.main_activity_card_face);
cardBack = (View) singleView.findViewById(R.id.main_activity_card_back);
}
System.out.println("from the else");
System.out.println("Position in else"+position);
product_data a=al.get(position);
TextView tv1=(TextView)singleView.findViewById(R.id.textView2);
TextView tv2=(TextView)singleView.findViewById(R.id.textView3);
TextView tv3=(TextView)singleView.findViewById(R.id.textView1);
/**
* Bind a click listener to initiate the flip transitions
*/
viewAnimator.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// This is all you need to do to 3D flip
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
cardFace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("positon"+ok);
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
cardBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT);
}
});
imgview1=(ImageView)singleView.findViewById(R.id.imageView4);
imgview1.setTag(position);
imgview1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println(position1);
String id=al.get(position1).id;
System.out.println("id from the list"+id);
Intent in=new Intent(product_list_Activity.this,rating_dialog.class);
in.putExtra("id",id);
startActivity(in);
}
});
ImageView imgview2=(ImageView)singleView.findViewById(R.id.imageView5);
imgview2.setTag(position);
imgview2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println(position1);
String id=al.get(position1).id;
System.out.println("id from the list"+id);
Intent in=new Intent(product_list_Activity.this,product_review.class);
in.putExtra("id",id);
startActivity(in);
}
});
tv3.setTag(position);
tv3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position1=(Integer)v.getTag();
System.out.println("from the review "+position1);
String id=al.get(position1).id;
globalvariables.product_id=id;
Intent in=new Intent(getApplicationContext(),review_product_tabbar.class);
startActivity(in);
}
});
tv1.setText(a.post_title);
tv2.setText("$"+a.oldprice);
tv1.setTypeface(tf);
tv2.setTypeface(tf);
tv3.setTypeface(tf);
int loader = R.drawable.ic_launcher;
ImageView image = (ImageView)singleView. findViewById(R.id.imageView1);
imgLoader.DisplayImage(a.image, loader, image);
Animation anim = new Rotate3dAnimation(90.0f, 0.0f, 100.0f, false, singleView);
anim.setDuration(1000l);
singleView.startAnimation(anim);
return singleView;
}
}
}
when i click on the first item it rotate and another view is coming
but when i scrolled the fourth item in below image will also rotate
i am using the on click listner on the relative views as in the code
Use ViewHolder for fill adapter. that may solve your issue
Example Code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = LayoutInflater.from(context);
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.news_by_categories_content, null);
holder = new ViewHolder();
holder.strNewsTitle = (TextView) convertView
.findViewById(R.id.txtNewsByCategories);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
NewsListByCategoriesItem obj = new NewsListByCategoriesItem(); // My Model class getting value from this
obj = newsArray.get(position);
holder.strNewsTitle.setText(obj.getStrNewsTitle());
return convertView;
}
class ViewHolder {
TextView strNewsTitle;
}
}
I have made an app in which i am using Two Tabs,
First Tab, to show list of Products [Menu Tab]
Second Tab, to show products added by you [Cart Tab]
Here i am allowing user to add products to cart, update quantity for product and show total amount of all the Items.
In my code i am facing very small problem but i am not getting any way for last two days to resolve this.
Problem:
Whenever i do click on Cart Tab to view products, those i have selected to buy and then again move back to Menu Tab to select few more products to buy and then again do click on Cart Tab to view all products i have selected, Here i am able to view Selected Product but not able to view updated quantity and not getting any change in Total Amount [Getting total of all previous added items only, not for those i have added second time...]
Please tell me where i am missing, to get updated quantity and to get change in total price, what code i need to add and where i need to add in my ViewCartActivity class
ViewCartActivity.Java:
public class ViewCartActivity extends Activity {
TextView mTxtViewGrandTotal;
String mGrandTotal ;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcartactivity);
ListView mLstView1 = (ListView) findViewById(R.id.listView1);
mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);
ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(ViewCartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);
if (Constants.mItem_Detail.size() > 0)
{
Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++) {
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}
mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);
ImageButton mImgViewCart = (ImageButton) findViewById(R.id.back_btn);
mImgViewCart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mViewCartIntent = new Intent(ViewCartActivity.this, com.version.bajrang.january.menu.ArrowsActivity.class);
startActivity(mViewCartIntent);
}
});
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("onResume list current size "+Constants.mItem_Detail.size());
super.onResume();
}
}
In Logcat, i am getting:
onResume list current size 3 [Number of Records]
TabActivity.Java:
private void setTabs()
{
addTab("Order", R.drawable.tab_order, CategoryActivity.class);
addTab("Cart", R.drawable.tab_cart, ViewCartActivity.class);
}
#Override
public void onBackPressed() {
// Code to update product Quantity
super.onBackPressed();
Constants.mItem_Detail.clear();
}
ViewCartAdapter.Java:
public class ViewCartAdapter extends BaseAdapter {
Activity activity;
LayoutInflater inflater;
public ViewCartAdapter(Activity a) {
// TODO Auto-generated constructor stub
activity = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return Constants.mItem_Detail.size();
}
public Object getItem(int position) {
//return position;
return Constants.mItem_Detail.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.viewcartlist, null);
TextView title = (TextView) vi.findViewById(R.id.title);
TextView qty = (TextView) vi.findViewById(R.id.qty);
TextView cost = (TextView) vi.findViewById(R.id.cost);
HashMap<String, String> item = new HashMap<String, String>();
item = Constants.mItem_Detail.get(position);
// Setting all values in listview
title.setText(item.get(SingleProductActivity.TAG_NAME));
qty.setText(item.get(SingleProductActivity.KEY_QTY));
cost.setText(item.get(SingleProductActivity.TAG_DURATION));
return vi;
}
}
Rakesh you have written everything needed in your program, just need to move from onCreate() to onResume() Method in ViewCartActivity
Like this:
public class ViewCartActivity extends Activity
{
TextView mTxtViewGrandTotal;
String mGrandTotal ;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcartactivity);
ListView mLstView1 = (ListView) findViewById(R.id.listView1);
mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);
ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(ViewCartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);
ImageButton mImgViewCart = (ImageButton) findViewById(R.id.back_btn);
mImgViewCart.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mViewCartIntent = new Intent(ViewCartActivity.this, com.version.bajrang.january.menu.ArrowsActivity.class);
startActivity(mViewCartIntent);
}
});
}
#Override
protected void onResume()
{
super.onResume();
System.out.println("onResume list current size " + Constants.mItem_Detail.size());
if (Constants.mItem_Detail.size() == 0)
{
return;
}
Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++)
{
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}
mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);
}
}