CODE:
private static final String CREATE_DETAILEDMEALS = "CREATE TABLE "
+ TABLE_DetailedMeals + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ mealsName + " TEXT unique," + categoryId + " INTEGER,"
+ desc + " TEXT," + size + " TEXT,"
+ price + " TEXT" + ")";
public void addDetailedMeals(String name, int catId, String d, String s, String p){
ContentValues values = new ContentValues(1);
values.put(mealsName, name);
values.put(categoryId, catId);
values.put(desc, d);
values.put(size, s);
values.put(price, p);
getWritableDatabase().insert(TABLE_DetailedMeals, null, values);
}
public Cursor getMeals2(int id)
{
Cursor cursor = getReadableDatabase().rawQuery("SELECT Name, Description, Size, Price FROM " + TABLE_DetailedMeals + " WHERE " + categoryId + "=" + String.valueOf(id), null);
return cursor;
}
// ID1 = MEALS
public ArrayList<DetailedMeals> GetDetailedMeals(int ID1)
{
ArrayList<DetailedMeals> list = new ArrayList<DetailedMeals>();
Cursor cursor = getMeals2(ID1);
int i = 0; // Iterator for 'do-while'. While it gets Categories names, it shall catch all the url's from array as well.
if (cursor.moveToFirst())
{
do
{
DetailedMeals cat = new DetailedMeals();
cat.setName(cursor.getString(cursor.getColumnIndex(mealsName))); //mealsName
cat.setDescription(cursor.getString(cursor.getColumnIndex(mealsName)));
cat.setSize(cursor.getString(cursor.getColumnIndex(mealsName)));
cat.setPrice(cursor.getString(cursor.getColumnIndex(mealsName)));
switch (ID1) {
case 1: cat.setImage(PIZZA_image_urls[i]);
break;
case 2: cat.setImage(BEER_image_urls[i]);
break;
default:
break;
}
list.add(cat);
i++;
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
Adapter:
public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
FragmentActivity c;
ArrayList<DetailedMeals> MealsList;
private final int MEAL = 0, DETAIL = 1;
public DifferentRowAdapter(FragmentActivity c, ArrayList<DetailedMeals> MealsList) {
this.c = c;
this.MealsList = MealsList;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case MEAL:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
return new mViewHolder(view);
case DETAIL:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.details_item, parent, false);
return new dViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DetailedMeals object = MealsList.get(position);
if (object != null) {
switch (object.getmType()) {
case MEAL:
((mViewHolder) holder).mNameTextView.setText(object.getName());
Glide.with(c)
.load(MealsList.get(position).getImage())
.diskCacheStrategy(DiskCacheStrategy.ALL) // Saves the media item after all transformations to cache and
// Saves just the original data to cache.
.placeholder(R.mipmap.ic_launcher)
.into(((mViewHolder) holder).img);
break;
case DETAIL:
((dViewHolder) holder).descTextView.setText(object.getDescription());
((dViewHolder) holder).sizeTextView.setText(object.getSize());
((dViewHolder) holder).priceTextView.setText(object.getPrice());
break;
}
}
}
#Override
public int getItemCount() {
if (MealsList == null)
return 0;
return MealsList.size();
}
#Override
public int getItemViewType(int position) {
if (MealsList != null) {
DetailedMeals object = MealsList.get(position);
if (object != null) {
return object.getmType();
}
}
return 0;
}
public class dViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView descTextView;
TextView sizeTextView;
TextView priceTextView;
detailsItemClickListener icl;
public dViewHolder(View itemView) {
super(itemView);
// Get references to desc, size, price
descTextView = (TextView) itemView.findViewById(R.id.desc);
sizeTextView = (TextView) itemView.findViewById(R.id.size);
priceTextView = (TextView) itemView.findViewById(R.id.price);
itemView.setOnClickListener(this);
}
public void setItemClickListener(detailsItemClickListener icl)
{
this.icl = icl;
}
#Override
public void onClick(View v){
this.icl.onItemClick(v, getPosition());
}
}
public class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView mNameTextView;
ImageView img;
mealsItemClickListener icl;
public mViewHolder(View itemView) {
super(itemView);
// Get references to image and name.
mNameTextView = (TextView) itemView.findViewById(R.id.name);
img = (ImageView) itemView.findViewById(R.id.category_image);
itemView.setOnClickListener(this);
}
public void setItemClickListener(mealsItemClickListener icl)
{
this.icl = icl;
}
#Override
public void onClick(View v){
this.icl.onItemClick(v, getPosition());
}
}
}
DetailedMeals.class
public class DetailedMeals {
private String name;
private String image;
private String description;
private String size;
private String price;
private int id;
private int mType;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getmType() {
return mType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Right now, GetDetailedMeals(..) returns only details name (not name/desc/price/size) ect. and also, it tries to return it before I get recyclerview with only Meal names.
My goal here is: https://img.exs.lv/l/a/lat-deels/fragm_1.png
Change your DifferentRowAdapter extends to RecyclerView.Adapter because the viewholder you are using is within the DifferentRowAdapter.
list.add(cat); in your code you are putting a DetailedMeals in a DiffRowAdapter list
So change the
public ArrayList<DifferentRowAdapter> GetDetailedMeals(int ID1)
to
public ArrayList<DetailedMeals> GetDetailedMeals(int ID1)
to have a same list and adapter.
then put result hear to know what happens.
Update:
after changing them you have just showing the name in your view so add more items like price or desc in mViewHolder:
public mViewHolder(View itemView) {
super(itemView);
// Get references to image and name.
mNameTextView = (TextView) itemView.findViewById(R.id.name);
// Add them hear
mPriceTextView = (TextView) itemView.findViewById(R.id.price);
mDescTextView = (TextView) itemView.findViewById(R.id.desc);
img = (ImageView) itemView.findViewById(R.id.category_image);
itemView.setOnClickListener(this);
}
Related
I am getting value from checking page adapter.If click the plus image in one list item the count value get increase.I want to update that particular value of child item in expandablelistview adapter.How to update that particular child value from checking page adapter.
Checkinapadater:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Movie1 m = movieItems.get(position);
if (m.getfood_totalprice() != null) {
if (!m.getfood_totalprice().equals("null")) {
holder.amount.setText(m.getfood_totalprice());
}
}
if (m.getfood_count() != null) {
if (!m.getfood_count().equals("null")) {
holder.item_count.setText(m.getfood_count());
}
}
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = Integer.parseInt(holder.item_count.getText().toString());
if(i>=0){
int count = i + 1;
holder.item_count.setText(Integer.toString(count));
value = Integer.toString(count);
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int i = Integer.parseInt(holder.item_count.getText().toString());
if(i>0){
int count = i - 1;
holder.item_count.setText(Integer.toString(count));
}
});
}
Exapanadablelistapater:
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Child child = (Child) getChild(groupPosition, childPosition);
final Child modelChild = groups.get(groupPosition).getItems().get(childPosition);
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
convertView = inflator.inflate(R.layout.detail_list, null);
viewHolder = new ViewHolder();
viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus);
viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus);
viewHolder.green = (ImageView) convertView.findViewById(R.id.green);
viewHolder.red = (ImageView) convertView.findViewById(R.id.red);
viewHolder.item_count = (TextView) convertView.findViewById(R.id.count);
//viewHolder.txtView = (TextView) ((AppCompatActivity)context).findViewById(R.id.checked_item);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(child.getName());
viewHolder.amount.setText(child.getPrice());
viewHolder.item_count.setText(Integer.toString(child.getCount()));
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(modelChild.getCount()>=0) // set your count default 0 when you bind data initially
{
int count = (modelChild.getCount()) + 1;
modelChild.setCount(count);
int s= Integer.parseInt(Detailpage.item.getText().toString());
itemname.setText(Integer.toString(count1));
// viewHolder.txtView.setText(Integer.toString(count1)+"items");
count1=s+1;
Detailpage.item.setText(Integer.toString(count1));
int foodprice=0;
foodprice=Integer.parseInt(child.getPrice());
total1=child.getPrice();
value=Integer.toString(modelChild.getcount());
//
name = modelChild.getName();
id=child.getId();
new AsyncT().execute(user_id,res_id,id,name,total1,value);
groups.get(groupPosition).getItems().set(childPosition, modelChild);
notifyDataSetChanged();
}
}});
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(modelChild.getCount()>0) // set your count default 0 when you bind data initially
{ int count = modelChild.getCount() - 1;
modelChild.setCount(count);
int s= Integer.parseInt(Detailpage.item.getText().toString());
count1=s-1;
Detailpage.item.setText(Integer.toString(count1));
name=modelChild.getName();
//
int foodprice=0;
foodprice=Integer.parseInt(child.getPrice());
int total = foodprice * child.getcount();
total1=child.getPrice();
// total1=Integer.toString(total);
value=Integer.toString(count);
id=child.getId();
if(count>0) {
new AsyncT().execute(user_id, res_id, id, name, total1, value);
}
}
modelChild.setChildName(modelChild.getChildName());
// set your other items if any like above
groups.get(groupPosition).getItems().set(childPosition, modelChild);
notifyDataSetChanged();
}
});
return convertView;
}
Movie1 model(checkin page):`
public class Movie1 {
public String food_id,food_count,food_item,food_price,food_resid,food_totalprice;
public void setfood_id(String food_id) {
this.food_id = food_id;
}
public void setfood_count(String food_count) {
this.food_count = food_count;
}
public void setfood_price(String food_price) {
this.food_price = food_price;
}
public void setfood_item(String food_item) {
this.food_item = food_item;
}
public String getfood_item() {
return food_item;
}
public String getfood_count() {
return food_count;
}
public String getfood_price() {
return food_price;
}
public void setfood_resid(String food_resid) {
this.food_resid = food_resid;
}
public String getfood_resid() {
return food_resid;
}
public String getfood_id() {
return food_id;
}
public void setfood_totalprice(String food_totalprice) {
this.food_totalprice = food_totalprice;
}
public String getfood_totalprice() {
return food_totalprice;
}
}
`
Child model:(Expandable listview)
public class Child {
private String Name,childName,item_status,image1,available_from,available_to,price,id,status;
private int Image,count;
public Object getstatus;
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public int getImage() {
return Image;
}
public void setImage(int Image) {
this.Image = Image;
}
public void setcount(int count) {
this.count = count;
}
public int getcount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getChildName() {
return childName;
}
public int getCount() {
return count;
}
public void setChildName(String childName) {
this.childName = childName;
}
public void setItem_status(String item_status) {
this.item_status = item_status;
}
public void setImage1(String image1) {
this.image1 = image1;
}
public void setAvailable_to(String available_to) {
this.available_to = available_to;
}
public void setAvailable_from(String available_from) {
this.available_from = available_from;
}
public void setPrice(String price) {
this.price = price;
}
public void setId(String id) {
this.id = id;
}
public String getPrice() {
return price;
}
public String getImage1() {
return image1;
}
public String getItem_status() {
return item_status;
}
public String getId() {
return id;
}
public void setstatus(String status) {
this.status = status;
}
public String getstatus() {
return status;
}
}
`checkin
Expandablelist adapter
i have Recycler view with radio group and its 4 radio buttons. but when i scroll down the list the middle of item is acting like first one . means if i have 10 items, if select first item button then the 6 button also got selecting with user selection.
what to do for issue in recycler view. Any Suggestions will be appreciable.
Thanks in Advance.
enter code here
public class ViewHolder extends RecyclerView.ViewHolder {
TextView quest, timestamp, answer;
RadioGroup rg;
RadioButton rb1, rb2, rb3, rb4;
Button submit;
public ViewHolder(View view) {
super(view);
quest = (TextView) itemView.findViewById(R.id.ques);
timestamp = (TextView) itemView.findViewById(R.id.tym);
answer = (TextView) itemView.findViewById(R.id.answer);
rb1 = (RadioButton)itemView.findViewById(R.id.rb1);
rb2 = (RadioButton)itemView.findViewById(R.id.rb2);
rb3 = (RadioButton)itemView.findViewById(R.id.rb3);
rb4 = (RadioButton)itemView.findViewById(R.id.rb4);
rb4 = (RadioButton)itemView.findViewById(R.id.rb4);
rg = (RadioGroup)itemView.findViewById(R.id.radio);
submit = (Button)itemView.findViewById(R.id.submit);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
int clickedPos= (int) group.getTag();
Log.v("currentpos",""+clickedPos);
messageArrayList.get(clickedPos).setSelectedRadioButtonId(checkedId);
int radioId = group.indexOfChild(radioButton);
RadioButton btn = (RadioButton) group.getChildAt(radioId);
final int currentPosition = getAdapterPosition();
if (messageArrayList.get(clickedPos).getSelectedRadioButtonId() != -1 && checkedId != -1) {
try {
String selection = btn.getText().toString();
if (selection.equalsIgnoreCase(messageArrayList.get(clickedPos).getAnswer().toString())) {
Log.v("TAG", "USER OPTION" + "\t" + selection + "\n" + "CORRECT ANSWER IS" + "\t" + messageArrayList.get(clickedPos).getAnswer());
// rg.setVisibility(View.INVISIBLE);
// answer.setVisibility(View.VISIBLE);
// answer.setText("Congrats, you are right. The answer is " + messageArrayList.get(clickedPos).getAnswer().toString());
Toast.makeText(group.getContext(), "YOu are Right", Toast.LENGTH_SHORT).show();
} else {
// rg.setVisibility(View.INVISIBLE);
// answer.setVisibility(View.VISIBLE);
// answer.setText("Oops, you're wrong, the correct answer is " + messageArrayList.get(clickedPos).getAnswer().toString());
Toast.makeText(group.getContext(), "You are Worng", Toast.LENGTH_SHORT).show();
}
Log.v("hello", selection);
String qusnn = quest.getText().toString();
Log.v("question", qusnn);
String read = "1";
try {
helper = new DBHelper(group.getContext());
database = helper.getWritableDatabase();
statement = database.compileStatement("update quiz set USERANS =? where QUESTION ='" + qusnn + "'");
statement.bindString(1, read);
statement.executeInsert();
database.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("exception for send data in table");
} finally {
database.close();
}
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
Log.v("hello", exceptionAsString);
}
Log.v("hello" + clickedPos, messageArrayList.get(clickedPos).getSelectedRadioButtonId() + "");
}
}});
}
}
public ChatRoomThreadAdapter( ArrayList<Message> messageArrayList) {
this.messageArrayList = messageArrayList;
Calendar calendar = Calendar.getInstance();
today = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == SELF) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_items, parent, false);
}else{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.secondlayout, parent, false);
}
return new ViewHolder(itemView);
}
#Override
public int getItemViewType(int position) {
Message message = messageArrayList.get(position);
if (message.getUsans().equals("0")) {
return SELF;
}
return position;
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder,final int position) {
final Message message = messageArrayList.get(position);
((ViewHolder) holder).quest.setText(message.getMessage());
((ViewHolder) holder).rb1.setText(message.getOpt1());
((ViewHolder) holder).rb2.setText(message.getOpt2());
((ViewHolder) holder).rb3.setText(message.getOpt3());
((ViewHolder) holder).rb4.setText(message.getOpt4());
((ViewHolder) holder).answer.setText(message.getAnswer());
String timestam= getTimeStamp(message.getCreatedAt());
((ViewHolder) holder).timestamp.setText(timestam);
// ((ViewHolder) holder).rg.clearCheck();
// ((ViewHolder) holder).rg.setTag(position);
// Log.e("select" + position, messageArrayList.get(position).getSelectedRadioButtonId() + "");
((ViewHolder) holder).rg.setTag(position);
if (message.getSelectedRadioButtonId()!= -1){
((ViewHolder) holder).rg.check(message.getSelectedRadioButtonId());
}else{
//your always need to write else condition
((ViewHolder) holder).rg.clearCheck();
}
}
#Override
public int getItemCount() {
return messageArrayList.size();
}
public static String getTimeStamp(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = "";
today = today.length() < 2 ? "0" + today : today;
try {
Date date = format.parse(dateStr);
SimpleDateFormat todayFormat = new SimpleDateFormat("dd");
String dateToday = todayFormat.format(date);
format = dateToday.equals(today) ? new SimpleDateFormat("hh:mm a") : new SimpleDateFormat("dd LLL, hh:mm a");
String date1 = format.format(date);
timestamp = date1.toString();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
}
enter code here
public class Message implements Serializable {
String id, message, opt1, opt2, opt3, opt4, answer, createdAt,usans;
public Message() {
}
public Message(String id, String message, String opt1, String opt2, String opt3, String opt4, String answer, String createdAt,String usans) {
this.id = id;
this.message = message;
this.opt1 = opt1;
this.opt2 = opt2;
this.opt3 = opt3;
this.opt4 = opt4;
this.answer = answer;
this.createdAt = createdAt;
this.usans = usans;
}
private int checkedId = -1;
public int getSelectedRadioButtonId() {
return checkedId;
}
public void setSelectedRadioButtonId(int checkedId) {
this.checkedId = checkedId;
}
public String getUsans() {
return usans;
}
public void setUsans(String usans) {
this.usans = usans;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setOpt1(String opt1) {
this.opt1 = opt1;
}
public String getOpt1() {
return opt1;
}
public void setOpt2(String opt2) {
this.opt2 = opt2;
}
public String getOpt2() {
return opt2;
}
public void setOpt3(String opt3) {
this.opt3 = opt3;
}
public String getOpt3() {
return opt3;
}
public void setOpt4(String opt4) {
this.opt4 = opt4;
}
public String getOpt4() {
return opt4;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getAnswer() {
return answer;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
}
Write this in your onBindViewHolder() of adapter class:
holder.setIsRecyclable(false);
I had the same issue and adding this code had helped me.
Edit:The issue is caused because when we scroll in Recycler View the cells gets recycled and in order to make sure the cells don't get recycled, the above method is used.
Looking at your code and comments I think you know how RecyclerView works especially re-using views.. The only thing you missed is setting view status on all scenarios, what I mean is - you always need to write else part of code
class MyViewHolder extends RecyclerView.ViewHolder {
RadioGroup vRadioGroup;
public MyViewHolder(View itemView) {
super(itemView);
vRadioGroup = (RadioGroup) itemView.findViewById(R.id.group1);
//your other stuff
vRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int clickedPos= (int) radioGroup.getTag();
messageArrayList.get(clickedPos).setSelectedRadioButtonId(radioButtonID);
}
});
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Message message = messageArrayList.get(position);
//your other stuff
holder.vRadioGroup.setTag(position);
if (message.getSelectedRadioButtonId()!= -1){
holder.vRadioGroup.check(message.getSelectedRadioButtonId());
}else{
//your always need to write else condition
holder.vRadioGroup.clearCheck();
}
}
hope this helps..
I am working on a screen where i am populating a list view using base adapter.Each row of list view contains a circular image view,Text View and Check box .On clicking single tick on toolbar ,i am displaying the id of user corresponding to checked button But it is displayed wrongly.I am implementing the following screen:
1.Bean_Friends
public class Bean_Friends {
private String name, url, extension, friendsID;
private String friendSelected = "false";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getFriendsID() {
return friendsID;
}
public void setFriendsID(String friendsID) {
this.friendsID = friendsID;
}
public String getFriendSelected() {
return friendSelected;
}
public void setFriendSelected(String friendSelected) {
this.friendSelected = friendSelected;
}
}
2.Adapter_Friends_Group.java
public class Adapter_Friends_Group extends BaseAdapter {
private Context context;
public List<Bean_Friends> listBeanFriends;
private LayoutInflater inflater;
private ApiConfiguration apiConfiguration;
private Bean_Friends bean_friends;
public Adapter_Friends_Group(Context context, List<Bean_Friends> listBeanFriends) {
this.context = context;
this.listBeanFriends = listBeanFriends;
}
#Override
public int getCount() {
return listBeanFriends.size();
}
#Override
public Object getItem(int i) {
return listBeanFriends.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.feed_item_friends, null);
}
//finding different views
ImageView pic = (ImageView) view.findViewById(R.id.friendsImage);
TextView txtName = (TextView) view.findViewById(R.id.nameFriends);
CheckBox chkFriends = (CheckBox) view.findViewById(R.id.chkFriends);
bean_friends = listBeanFriends.get(i);
String name = bean_friends.getName();
String url = bean_friends.getUrl();
String extension = bean_friends.getExtension();
apiConfiguration = new ApiConfiguration();
String api = apiConfiguration.getApi();
String absolute_url = api + "/" + url + "." + extension;
//loading image into ImageView iew
Picasso.with(context).load(absolute_url).error(R.drawable.default_avatar).into(pic);
//Setting name in the textview
txtName.setText(name);
//If check box is checked,then add true to bean else add false to bean
if (chkFriends.isChecked()) {
bean_friends.setFriendSelected("true");
} else {
bean_friends.setFriendSelected("false");
}
chkFriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
if (cb.isChecked()) {
bean_friends.setFriendSelected("true");
Toast.makeText(context, "Check Box is checked : " + cb.isChecked(), Toast.LENGTH_SHORT).show();
} else {
bean_friends.setFriendSelected("false");
}
}
});
return view;
}
}
3. Code of Activity containing view
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_new_group, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.createGroup:
createNewGroup();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void createNewGroup() {
Toast.makeText(NewGroupActivity.this, "clicked", Toast.LENGTH_SHORT).show();
List<Bean_Friends> listBeanFriends = new ArrayList<>();
//Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
listBeanFriends = adapter_friends_group.listBeanFriends;
// Log.e("Size of adapter_friends", String.valueOf(adapter_friends_group.listBeanFriends.size()));
Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
for (int i = 0; i < listBeanFriends.size(); i++) {
Log.e("Loop Working", String.valueOf(i));
Bean_Friends bean_friends = listBeanFriends.get(i);
String friendID = bean_friends.getFriendsID();
String friendSelected = bean_friends.getFriendSelected();
String friendName = bean_friends.getName();
Log.e("FriendsName", friendName);
Log.e("FriendID", friendID);
Log.e("friendSelected", friendSelected);
if (friendSelected.equals("true")) {
Toast.makeText(NewGroupActivity.this, friendID, Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(NewGroupActivity.this,"true",Toast.LENGTH_SHORT).show();
}
}
}
Updated code:
Solved the issue after doing the following changes:
Adapter
public class Adapter_Friends_Group extends BaseAdapter {
private Context context;
public List<Bean_Friends> listBeanFriends;
private LayoutInflater inflater;
private ApiConfiguration apiConfiguration;
private Bean_Friends bean_friends;
public Adapter_Friends_Group(Context context, List<Bean_Friends> listBeanFriends) {
this.context = context;
this.listBeanFriends = listBeanFriends;
}
private class ViewHolder {
ImageView imageView;
TextView txtName;
CheckBox chkFriend;
}
#Override
public int getCount() {
return listBeanFriends.size();
}
#Override
public Object getItem(int i) {
return listBeanFriends.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.feed_item_friends, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.friendsImage);
viewHolder.txtName = (TextView) view.findViewById(R.id.nameFriends);
viewHolder.chkFriend = (CheckBox) view.findViewById(R.id.chkFriends);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
bean_friends = listBeanFriends.get(i);
String name = bean_friends.getName();
String url = bean_friends.getUrl();
String extension = bean_friends.getExtension();
apiConfiguration = new ApiConfiguration();
String api = apiConfiguration.getApi();
String absolute_url = api + "/" + url + "." + extension;
//loading image into ImageView iew
Picasso.with(context).load(absolute_url).error(R.drawable.default_avatar).into(viewHolder.imageView);
//Setting name in the textview
viewHolder.txtName.setText(name);
//Setting boolean isSelected if the Checkbox is checked
viewHolder.chkFriend.setChecked(bean_friends.isSelected());
viewHolder.chkFriend.setTag(bean_friends);
viewHolder.chkFriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
Bean_Friends bean_friends = (Bean_Friends) cb.getTag();
Toast.makeText(context, "Clicked on Checkbox: " + bean_friends.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
bean_friends.setIsSelected(cb.isChecked());
}
});
return view;
}
}
Bean
public class Bean_Friends {
private String name, url, extension, friendsID;
boolean isSelected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getFriendsID() {
return friendsID;
}
public void setFriendsID(String friendsID) {
this.friendsID = friendsID;
}
public boolean isSelected() {
return isSelected;
}
public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
Method inside Activity
public void createNewGroup() {
StringBuffer responseText = new StringBuffer();
listBeanFriends = adapter_friends_group.listBeanFriends;
// Log.e("Size of adapter_friends", String.valueOf(adapter_friends_group.listBeanFriends.size()));
Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
for (int i = 0; i < listBeanFriends.size(); i++) {
Log.e("Loop Working", String.valueOf(i));
Bean_Friends bean_friends = listBeanFriends.get(i);
String friendID = bean_friends.getFriendsID();
String friendName = bean_friends.getName();
Log.e("FriendsName", friendName);
Log.e("FriendID", friendID);
Log.e("FriendSelected", String.valueOf(bean_friends.isSelected()));
if (bean_friends.isSelected()) {
responseText.append("\n" + bean_friends.getName() + " " + bean_friends.getFriendsID());
}
}
Toast.makeText(NewGroupActivity.this, responseText, Toast.LENGTH_SHORT).show();
}
I think the mistake is in this line :-
//If check box is checked,then add true to bean else add false to bean
if (chkFriends.isChecked()) {
bean_friends.setFriendSelected("true");
} else {
bean_friends.setFriendSelected("false");
}
Here you are setting 'setFriendSelected' based on whether the chkFriends is checked or not. In list View the views are reused and if say you have checked 'A' as friend and then you scrolls down then the view of 'A' maybe reused in 'C' and by this code 'C' now will be checked as friend. Here you instead want to inflate your checkBox view according to whether 'C' is your friend or not. Try this instead:-
//If check box is checked,then add true to bean else add false to bean
if (bean_friends.getFriendSelected().equals("true")) {
chkFriends.setChecked(true);
} else {
chkFriends.setChecked(false);
}
P.S : you can use view holder pattern here for better performance.
I made an app and so far i stored captured images in database. Then I split them in categories and showed them in different GridViews for each category. I managed to show them in differents ImageViews after i choose an image from each gridView. Now i was trying to take those choosen images and save them in database and then show them in ListView. The problem is that in the list there is nothing. I suppose that my mistake is in the part of the code that i am taking the images to save them.
Any help?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
handler2 = new DatabaseHandler(getApplicationContext());
Intent i = getIntent();
ImageView im1 = (ImageView) findViewById(R.id.im1);
im1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image1")));
ImageView im2 = (ImageView) findViewById(R.id.im2);
im2.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image2")));
ImageView im3 = (ImageView) findViewById(R.id.im3);
im3.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image3")));
ImageView im4 = (ImageView) findViewById(R.id.im4);
im4.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image4")));
ImageView im5 = (ImageView) findViewById(R.id.im5);
im5.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image5")));
ImageView im6 = (ImageView) findViewById(R.id.im6);
im6.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image6")));
ImageButton btn_save_outfit = (ImageButton)findViewById(R.id.btn_combine);
btn_save_outfit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Outfits outfits = new Outfits();
outfits.setImage1(i1);
outfits.setImage2(i2);
outfits.setImage3(i3);
outfits.setImage4(i4);
outfits.setImage5(i5);
outfits.setImage6(i6);
Boolean added = handler2.addOutfit(outfits);
if(added){
Toast.makeText(getApplicationContext() , "Outfit added." , Toast.LENGTH_LONG).show();
String log = "Id: "+ outfits.getID()+" ,Image1: " + outfits.getImage1() + " ,Image2: " + outfits.getImage2()
+ ",Image3:" + outfits.getImage3() + ",Image4:" + outfits.getImage5() + ",Image6:" +outfits.getImage6();
Log.d("Image1: ", log);
}else{
Toast.makeText(getApplicationContext(), "Outfit not added. Please try again", Toast.LENGTH_LONG).show();
}
}});
}
}
Outfits.class
public class Outfits {
private int _id;
private String _image1;
private String _image2;
private String _image3;
private String _image4;
private String _image5;
private String _image6;
public Outfits(){
}
public Outfits(String image1, String image2, String image3, String image4, String image5 ,String image6){
this._image1 = image1;
this._image2 = image2;
this._image3 = image3;
this._image4 = image4;
this._image5 = image5;
this._image6 = image6;
}
// Id
public int getID(){
return _id;
}
public void setID(int id){
this._id = id;
}
// Image1
public String getImage1(){
return this._image1;
}
public void setImage1(String image1){
this._image1 = image1;
}
// Image2
public String getImage2(){
return this._image2;
}
public void setImage2(String image2){
this._image2 = image2;
}
// Image3
public String getImage3(){
return this._image3;
}
public void setImage3(String image3){this._image3 = image3;}
// Image4
public String getImage4(){
return this._image4;
}
public void setImage4(String image4){this._image4 = image4;
}
// Image5
public String getImage5(){
return this._image5;
}
public void setImage5(String image5){
this._image5 = image5;
}
// Image6
public String getImage6(){
return this._image6;
}
public void setImage6(String image6){this._image6 = image6;
}
}
Adapter.class
public class OutfitsAdapter extends BaseAdapter {
private List<Outfits> items;
private Context context;
private LayoutInflater inflater;
public OutfitsAdapter(Context _context, List<Outfits> _items){
inflater = LayoutInflater.from(_context);
this.items = _items;
this.context = _context;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Outfits outfits = items.get(position);
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.outfit_item, null);
ImageView i1 = (ImageView) view.findViewById(R.id.image1);
ImageView i2 = (ImageView) view.findViewById(R.id.image2);
ImageView i3 = (ImageView) view.findViewById(R.id.image3);
ImageView i4 = (ImageView) view.findViewById(R.id.image4);
ImageView i5 = (ImageView) view.findViewById(R.id.image5);
ImageView i6 = (ImageView) view.findViewById(R.id.image6);
i1.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage1()));
i2.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage2()));
i3.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage3()));
i4.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage4()));
i5.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage5()));
i6.setImageBitmap(BitmapFactory.decodeFile(outfits.getImage6()));
}
return view;
}
}
Database.class
public List<Outfits> readAllOutfits(){
SQLiteDatabase db = this.getWritableDatabase();
List<Outfits> outfits = new ArrayList<Outfits>();
Cursor cursor = db.query(SQLITE_TABLE_2, columns_2, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Outfits out = new Outfits();
out.setID(Integer.parseInt(cursor.getString(0)));
out.setImage1(cursor.getString(1));
out.setImage2(cursor.getString(2));
out.setImage3(cursor.getString(3));
out.setImage4(cursor.getString(4));
out.setImage5(cursor.getString(5));
out.setImage6(cursor.getString(6));
cursor.moveToNext();
}
cursor.close();
return outfits;
}
And the class with the list
public class CurrentOutfits extends Activity {
private List<Outfits> outfits;
private DatabaseHandler handler2;
ListView lv_outfits;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_outfits);
handler2 = new DatabaseHandler(getApplicationContext());
lv_outfits = (ListView) findViewById(R.id.outfits_list);
loadOutfits();
}
private void loadOutfits() {
outfits = handler2.readAllOutfits();
OutfitsAdapter adapter = new OutfitsAdapter(this, outfits);
lv_outfits.setAdapter(adapter);
for (Outfits o : outfits) {
String record = "ID=" + o.getID() + " | Category=" + o.getImage1() + " | " + o.getImage2();
Log.d("Record", record);
}
}
}
The issue is in Database.class you are missing a line check this out
public List<Outfits> readAllOutfits(){
SQLiteDatabase db = this.getWritableDatabase();
List<Outfits> outfits = new ArrayList<Outfits>();
Cursor cursor = db.query(SQLITE_TABLE_2, columns_2, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Outfits out = new Outfits();
out.setID(Integer.parseInt(cursor.getString(0)));
out.setImage1(cursor.getString(1));
out.setImage2(cursor.getString(2));
out.setImage3(cursor.getString(3));
out.setImage4(cursor.getString(4));
out.setImage5(cursor.getString(5));
out.setImage6(cursor.getString(6));
outfits.add(out);//this is the line you missed
cursor.moveToNext();
}
cursor.close();
return outfits;
}
hope it works
I want to populate a ListView with different layouts for odd and even rows. It should look like this:
I use two objects "OddListItem" and "EvenListItem" to store/access the data. I do not know how to pass both objects to my custom listview adapter and get the correct view.
My object classes:
public class OddListItem {
private String time_start;
private String time_end;
private String location;
public OddListItem(String time_start, String time_end, String location) {
super();
this.time_start = time_start;
this.time_end = time_end;
this.location = location;
}
// getters and setters
void setTimeStart(String time_start) {
this.time_start = time_start;
}
void setTimeEnd(String time_end) {
this.time_end = time_end;
}
void setLocation(String location) {
this.location = location;
}
public String getTimeStart() {
return time_start;
}
public String getTimeEnd() {
return time_end;
}
public String getLocation() {
return location;
}
}
public class EvenListItem {
private String image;
private String location;
public EvenListItem (String image, String location) {
super();
this.image = image;
this.location = location;
}
// getters and setters
void setImage(String image) {
this.image = image;
}
void setLocation(String location) {
this.location = location;
}
public String getImage() {
return image;
}
public String getLocation() {
return location;
}
}
MyCustomAdapter:
public class MyCustomAdapter extends BaseAdapter {
// Tag for Logging
private static final String TAG = "MyCustomAdapter";
int type;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<OddListItem> mData = new ArrayList<OddListItem>();
private LayoutInflater mInflater;
//private TreeSet mSeparatorsSet = new TreeSet();
private Context context;
public MyCustomAdapter(Context context) {
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public void addItem(final OddListItem item) {
mData.add(item);
//The notification is not necessary since the items are not added dynamically
//notifyDataSetChanged();
}
public void addSeparatorItem(final OddListItem item) {
mData.add(item);
//The notification is not necessary since the items are not added dynamically
//notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
/*if ((position%2) == 0){
type = TYPE_ITEM;
} else {
type = TYPE_SEPARATOR;
}
return type;*/
return position%2;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public OddListItem getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
Log.d(TAG, "getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
//inflate the new layout
convertView = mInflater.inflate(R.layout.detail_list_row_odd, parent, false);
holder.tv_time_from = (TextView) convertView.findViewById(R.id.tv_time_from);
holder.tv_time_to = (TextView) convertView.findViewById(R.id.tv_time_to);
holder.tv_current_location_odd = (TextView) convertView.findViewById(R.id.tv_current_location_odd);
//fill the layout with values
/*holder.tv_time_from.setText("12:34");
holder.tv_time_to.setText("12:37");
holder.tv_current_location_odd.setText("Aktueller Standort");*/
holder.tv_time_from.setText(mData.get(position).getTimeStart());
holder.tv_time_to.setText(mData.get(position).getTimeEnd());
holder.tv_current_location_odd.setText(mData.get(position).getLocation());
break;
case TYPE_SEPARATOR:
//inflate the new layout
convertView = mInflater.inflate(R.layout.detail_list_row_even, parent, false);
holder.tv_current_location_even = (TextView) convertView.findViewById(R.id.tv_current_location_even);
holder.img_transport = (ImageView) convertView.findViewById(R.id.img_transport);
//fill the layout with values
holder.tv_current_location_even.setText("Hauptbahnhof");
holder.img_transport.setImageDrawable(context.getResources().getDrawable(R.drawable.rollator));
break;
default:
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
public TextView tv_time_from;
public TextView tv_time_to;
public TextView tv_current_location_odd;
public TextView tv_current_location_even;
public ImageView img_transport;
}
}
Here I generate the data and call the adapter:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_connection);
generateData();
//fill ListView with custom content from MyCustomAdapter class
mAdapter = new MyCustomAdapter(getApplicationContext());
for (int i = 1; i < odd_items.size(); i++) {
mAdapter.addItem(odd_items.get(i));
if (i % 1 == 0) {
mAdapter.addSeparatorItem(odd_items.get(i));
}
}
setListAdapter(mAdapter);
//set duration text
tv_duration = (TextView)findViewById(R.id.tv_duration);
tv_duration.setText("Dauer: 22 Minuten");
}
private void generateData() {
odd_items = new ArrayList<OddListItem>();
odd_items.add(new OddListItem("12:34", "", "Aktueller Standort"));
odd_items.add(new OddListItem("12:37", "12:37", "TUM"));
odd_items.add(new OddListItem("12:42", "12:42", "Hauptbahnhof Nord"));
odd_items.add(new OddListItem("12:48", "12:48", "Hauptbahnhof"));
even_items = new ArrayList<EvenListItem>();
even_items.add(new EvenListItem("R.drawable.rollator", "3 Minuten Fußweg"));
even_items.add(new EvenListItem("R.drawable.bus", "Richtung Hauptbahnhof Nord"));
even_items.add(new EvenListItem("R.drawable.rollator", "6 Minuten Fußweg"));
mData = new Data(odd_items, even_items);
}
Any help would be great. Maybe there is also a better approach then please let me know.
I would create a Single list of Items
public class Items {
private String time_start;
private String time_end;
private String location;
private int image;
private String locationeven;
private int oddoreven;
public String getTime_start() {
return time_start;
}
public void setTime_start(String time_start) {
this.time_start = time_start;
}
public String getTime_end() {
return time_end;
}
public void setTime_end(String time_end) {
this.time_end = time_end;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getLocationeven() {
return locationeven;
}
public void setLocationeven(String locationeven) {
this.locationeven = locationeven;
}
public int getOddoreven() {
return oddoreven;
}
public void setOddoreven(int oddoreven) {
this.oddoreven = oddoreven;
}
}
In onCreate of Activity call
generateData() ;
Then
ArrayList<Items> oddorevenlist = new ArrayList<Items>();
private void generateData() {
Items item1 = new Items();
item1.setTime_start("12:34");
item1.setTime_end("");
item1.setLocation("Aktueller Standort");
item1.setOddoreven(0);
oddorevenlist.add(item1);
Items item2 = new Items();
item2.setImage(R.drawable.ic_launcher);
item2.setLocationeven("3 Minuten Fußweg");
item2.setOddoreven(1);
oddorevenlist.add(item2);
Items item3 = new Items();
item3.setTime_start("12:37");
item3.setTime_end("12:37");
item3.setLocation("Tum");
item3.setOddoreven(0);
oddorevenlist.add(item3);
Items item4 = new Items();
item4.setImage(R.drawable.ic_launcher);
item4.setLocationeven("Richtung Hauptbahnhof Nord");
item4.setOddoreven(1);
oddorevenlist.add(item4);
Items item5 = new Items();
item5.setTime_start("12:42");
item5.setTime_end("12:42");
item5.setLocation("Hauptbahnhof Nord");
item5.setOddoreven(0);
oddorevenlist.add(item5);
Items item6 = new Items();
item6.setImage(R.drawable.ic_launcher);
item6.setLocationeven("R6 Minuten Fußweg");
item6.setOddoreven(1);
oddorevenlist.add(item6);
Items item7 = new Items();
item7.setTime_start("12:48");
item7.setTime_end("12:48");
item7.setLocation("HHauptbahnhof");
item7.setOddoreven(0);
oddorevenlist.add(item7);
MyCustomAdapter mAdapter = new MyCustomAdapter(this,oddorevenlist);
setListAdapter(mAdapter);
}
Adapter code
public class MyCustomAdapter extends BaseAdapter {
// Tag for Logging
private static final String TAG = "MyCustomAdapter";
int type;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Items> oddorevenlist ;
private LayoutInflater mInflater;
private Context context;
public MyCustomAdapter(Context context, ArrayList<Items> oddorevenlist) {
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.oddorevenlist = oddorevenlist;
}
#Override
public int getItemViewType(int position) {
if (oddorevenlist.get(position).getOddoreven()==0){
type = TYPE_ITEM;
} else if (oddorevenlist.get(position).getOddoreven()==1) {
type = TYPE_SEPARATOR;
}
return type;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return oddorevenlist.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
Log.d(TAG, "getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
//inflate the new layout
convertView = mInflater.inflate(R.layout.row_odd, parent, false);
holder.tv_time_from = (TextView) convertView.findViewById(R.id.tv_time_from);
holder.tv_time_to = (TextView) convertView.findViewById(R.id.tv_time_to);
holder.tv_current_location_odd = (TextView) convertView.findViewById(R.id.tv_current_location_odd);
holder.tv_time_from.setText(oddorevenlist.get(position).getTime_start());
holder.tv_time_to.setText(oddorevenlist.get(position).getTime_end());
holder.tv_current_location_odd.setText(oddorevenlist.get(position).getLocation());
break;
case TYPE_SEPARATOR:
//inflate the new layout
convertView = mInflater.inflate(R.layout.row_even, parent, false);
holder.tv_current_location_even = (TextView) convertView.findViewById(R.id.tv_current_location_even);
holder.img_transport = (ImageView) convertView.findViewById(R.id.img_transport);
//fill the layout with values
holder.tv_current_location_even.setText(oddorevenlist.get(position).getLocationeven());
holder.img_transport.setImageResource(R.drawable.ic_launcher);
break;
default:
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
public TextView tv_time_from;
public TextView tv_time_to;
public TextView tv_current_location_odd;
public TextView tv_current_location_even;
public ImageView img_transport;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
}
Snap