Updating TextView inside Header ListView - android

I'm trying to update my TextView when my onItenClick event fires, I have several TextViews, they are created as many I want inside my row template, but here goes the problem, when I try to set this TextView value it's acctually getting setted, but another textview, is also setted, with the same value I have on idea of the why to this behavior, but have no idea how to fix it.
My idea:
I my getRowView method (when I scroll the list), I'm seeing an
unusual behavior, once after I set some value to one of my itens, it
keep changing the text view that got this value. So I think here is
the core of the problem.
PS: I'm using the component HeaderListView, if you get another suggestion of framework to do the same thing, I would really appreciate.
PS2: Here goes my code:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import br.com.soutsapp.souts.R;
import br.com.soutsapp.souts.model.Menu;
import br.com.soutsapp.souts.model.Product;
import br.com.soutsapp.souts.model.modelview.OrderItem;
import br.com.soutsapp.souts.userInterface.activity.SectionAdapter;
import br.com.soutsapp.souts.userInterface.adapter.HeaderListView;
public class MenuFragment extends Fragment {
private Context mContext;
private List<OrderItem> itens;
private Menu menu;
public MenuFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_menu, container, false);
mContext = getContext();
itens = new ArrayList<>();
menu = new Menu();
final String[] sec1 = {"aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa","aaaaaaaa"};
final String[] sec2 = {"bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb","bbbbbbbb"};
final String[] sec3 = {"cccccccc","cccccccc","cccccccc","cccccccc","cccccccc","cccccccc","cccccccc"};
final String[] sec4 = {"dddddddd","dddddddd","dddddddd","dddddddd","dddddddd","dddddddd","dddddddd"};
HeaderListView lv = (HeaderListView) v.findViewById(R.id.lv_menu_items);
lv.setAdapter(new SectionAdapter() {
#Override
public int numberOfSections() {
return menu.getMenuSessions().size();
}
#Override
public int numberOfRows(int section) {
return menu.getMenuSessions().get(section).size();
}
#Override
public Object getRowItem(int section, int row) {
return menu.getMenuSessions().get(section).get(row);
}
#Override
public boolean hasSectionHeaderView(int section) {
return true;
}
#Override
public View getRowView(int section, int row, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(getResources().getLayout(R.layout.menu_item_row), null);
}
//Product product = (Product)getRowItem(section, row);
//convertView.setId(product.getId());
TextView tvMenuItemName = (TextView) convertView.findViewById(R.id.tv_menu_item_name);
TextView tvMenuItemPrice = (TextView) convertView.findViewById(R.id.tv_price);
String itemName = ((Product)getRowItem(section,row)).getName();
tvMenuItemName.setText(itemName);
String itemPrice = String.valueOf(((Product) getRowItem(section, row)).getPrice());
tvMenuItemPrice.setText(itemPrice);
final int mySection = section;
final int myRow = row;
final TextView tvQuantity = (TextView) convertView.findViewById(R.id.tv_quantity);
tvQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Product product = (Product) getRowItem(mySection, myRow);
if (tvQuantity.getText().equals("1") && !tvQuantity.getText().equals("")){
tvQuantity.setText("");
OrderItem itenToRemove = null;
for(OrderItem item: itens){
if(item.getProductId() == product.getId()){
itenToRemove = item;
break;
}
}
itens.remove(itenToRemove);
}
else{
for(OrderItem item: itens){
if(item.getProductId() == product.getId()){
int quantity = item.getQuantity();
item.setQuantity(--quantity);
tvQuantity.setText(String.valueOf(item.getQuantity()));
break;
}
}
}
}
});
return convertView;
}
#Override
public int getSectionHeaderViewTypeCount() {
return 1;
}
#Override
public int getSectionHeaderItemViewType(int section) {
return section % 1;
}
#Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (TextView) getActivity().getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
}
if (getSectionHeaderItemViewType(section) == 0) {
((TextView) convertView).setText("Header for section " + section);
} else {
((TextView) convertView.findViewById(android.R.id.text1)).setText("Header for section " + section);
}
switch (section) {
case 0:
convertView.setBackgroundColor(getResources().getColor(R.color.Blue_Jay));
break;
case 1:
convertView.setBackgroundColor(getResources().getColor(R.color.Yellow));
break;
case 2:
convertView.setBackgroundColor(getResources().getColor(R.color.Red_Wine));
break;
}
return convertView;
}
#Override
public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
super.onRowItemClick(parent, view, section, row, id);
//Product p = (Product)view.getTag();
TextView tvQuantity = (TextView) view.findViewById(R.id.tv_quantity);
Product p = menu.getMenuSessions().get(section).get(row);
//Product p = (Product) getRowItem(section, row);
boolean exist = false;
for(OrderItem item : itens){
if(item.getProductId() == p.getId()){
int actualQuantity = item.getQuantity();
item.setQuantity(++actualQuantity);
tvQuantity.setText(String.valueOf(item.getQuantity()));
exist = true;
}
}
if(!exist){
itens.add(new OrderItem(p.getId(), 1));
tvQuantity.setText("1");
}
}
});
return v;
}
}
Here goes on screenshot, I had touch the "Smirnoff dose" item, and both "Smirnoff dose" and "Água" have their textviews values changed.

Related

How to get gridview multiple positions and store into array variable

Now I am displaying images in grid-view, it working fine. In that grid-view i am going to select few images, i want store selected multiple images position in array variable (example: if i select position 1, 4 ,10. i want that particular position id and i want to store it array like 1,4,10,15,). I will put my activity and adapter code below. Thank you in advance.
Activity
public class EM_event_total_userSeats extends AppCompatActivity implements RestCallback,AdapterView.OnItemClickListener {
String user_id,first_name,last_name,name,emailid,contact_no,gender1,date_of_birth,country_id,postal_code,rolename,profession_response,Street_Address,City,photo;
GridView GridUserSeats;
;
TextView textView1,textView2,Tvposition;
ImageView Ivseats;
public static EM_event_total_userseatsAdapter adapter;
ArrayList<EM_event_total_UserSeatsModel> EMeventuserseatslist;
View savedView;
View previous = null;
String event_id = "EVEPRI62";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.em_event_total_user_seats);
initviews();
callSeatsApi();
GridUserSeats.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String a = String.valueOf(position);
Toast.makeText(EM_event_total_userSeats.this, a + "#Selected", Toast.LENGTH_SHORT).show();
v.setBackgroundColor(Color.GREEN);
adapter.notifyDataSetChanged();
}
});
}
private void initviews() {
GridUserSeats=(GridView)findViewById(R.id.GridUserSeats);
GridUserSeats.setOnItemClickListener(this);
textView1=(TextView) findViewById(R.id.textView1);
// textView2=(TextView) findViewById(R.id.textView2);
Intent intent = getIntent();
first_name = intent.getStringExtra("first_name");
last_name = intent.getStringExtra("last_name");
}
private void callSeatsApi() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("events", event_id);
RestService.getInstance(EM_event_total_userSeats.this).getUserSeats(map, new MyCallback<ArrayList<EM_event_total_UserSeatsModel>>(EM_event_total_userSeats.this,
EM_event_total_userSeats.this, true, "Finding seats....", GlobalVariables.SERVICE_MODE.EM_SEATS));
}
#Override
public void onFailure(Call call, Throwable t, GlobalVariables.SERVICE_MODE mode) {
}
#Override
public void onSuccess(Response response, GlobalVariables.SERVICE_MODE mode)
{
switch (mode)
{
case EM_SEATS:
EMeventuserseatslist = (ArrayList<EM_event_total_UserSeatsModel>)response.body();
adapter = new EM_event_total_userseatsAdapter(EMeventuserseatslist, getApplicationContext());
GridUserSeats.setAdapter(adapter);
break;
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
Adapter
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
import cfirst.live.com.R;
import cfirst.live.com.model.BasketModel;
import cfirst.live.com.model.EM_event_total_UserSeatsModel;
public class EM_event_total_userseatsAdapter extends ArrayAdapter<EM_event_total_UserSeatsModel> implements View.OnClickListener {
ArrayList<EM_event_total_UserSeatsModel> dataSet;
public ArrayList<EM_event_total_UserSeatsModel> EMeventuserseatslist = new ArrayList<EM_event_total_UserSeatsModel>();
Context mContext;
ViewHolder holder;
String user_seats;
private int[] tagCollection;
private String[] mobileValues;
private String[] mobileValuesD;
private static class ViewHolder {
TextView TvEmUserSeats;
ImageView IvUsreSeats,available,selctedimag;
}
private String[] strings;
List<Integer> selectedPositions = new ArrayList<>();
public EM_event_total_userseatsAdapter(ArrayList<EM_event_total_UserSeatsModel> data, Context context) {
super(context, R.layout.list_em_get_seats, data);
this.dataSet = data;
this.mContext=context;
}
public int getTagFromPosition(int position) {
return tagCollection[position];
}
#Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(tagCollection[position]);
EM_event_total_UserSeatsModel dataModel=(EM_event_total_UserSeatsModel) object;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
EM_event_total_UserSeatsModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_em_get_seats, parent, false);
viewHolder.TvEmUserSeats = (TextView) convertView.findViewById(R.id.TvEmUserSeats);
viewHolder.IvUsreSeats = (ImageView) convertView.findViewById(R.id.IvUsreSeats);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
String blue_available = "seat3.png";
String red_booked = "seat1.png";
String get_seat = dataModel.getBookedStatus();
viewHolder.TvEmUserSeats.setText(dataModel.getSeatName());
if(Integer.parseInt(get_seat) == 1){
Picasso.with(mContext).load("imageurl + red_booked).into(viewHolder.IvUsreSeats);
}else
{
Picasso.with(mContext).load("imageurl + blue_available).into(viewHolder.IvUsreSeats);
}
return convertView;
}
}
It's simple. You can use SparseBooleanArray for this purpose. Just add the following methods as is in your adapter:
public void toggleSelection(int item) {
if (selectedItems.get(item, false)) {
selectedItems.delete(item);
} else {
selectedItems.put(item, true);
}
notifyDataSetChanged();
}
public void setSelectedItems(List<Object> objects) {
if (objects != null) {
for (Object object : objects) {
toggleSelection(object.getId());
}
notifyDataSetChanged();
}
}
public void clearSelections() {
selectedItems.clear();
notifyDataSetChanged();
}
public int getSelectedItemCount() {
return selectedItems.size();
}
public List<Integer> getSelectedItems() {
List<Integer> items =
new ArrayList<Integer>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
items.add(selectedItems.keyAt(i));
}
return items;
}
Use the set selection method to store the selected status of your item.
Don't forget to initialize SparseBooleanArray in your constructor.
SparseBooleanArray SparseBooleanArray = new SparseBooleanArray();
After that, use toggleSelection(position); to change the selected status of an item, then after performing selections, call getSelectedItem() to get the selected items in an array.
// create an Arraylist and add the selected position in a list
List <int> selectedArray = new ArrayList<>();
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int click = (int) parent.getItemAtPosition(position);
selectedArray.add(click);
}

Dynamic ListView issue on Android

Currently I am working on an Android app and use parse as backend where I have created a ListView of nearby places dynamically. But I face the following design issue: When the user clicks on a place, a view must appear under the clicked item.
And I have faced a problem with grouping the place, as you can see in figure 1, there are many branches for HSBC bank. In which case they are under HSBC.
I have tried expandable ListView before, but it does not give much customization for child view , just simple one.
my BaseAdapter:
import java.util.ArrayList;
import java.util.List;
import com.parse.ParseGeoPoint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
//ImageLoader imageLoader;
private List<AnywallPost> AnywallPostlist = null;
private ArrayList<AnywallPost> arraylist;
public ListViewAdapter(Context context,
List<AnywallPost> AnywallPostlist) {
this.context = context;
this.AnywallPostlist = AnywallPostlist;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<AnywallPost>();
this.arraylist.addAll(AnywallPostlist);
}
public class ViewHolder {
TextView distance;
TextView name;
}
#Override
public int getCount() {
return AnywallPostlist.size();
}
#Override
public Object getItem(int position) {
return AnywallPostlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
final View row=view;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.user_custom, null);
// Locate the TextViews in listview_item.xml
holder.distance = (TextView) view.findViewById(R.id.disView);
holder.name = (TextView) view.findViewById(R.id.nameView);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.name.setText(AnywallPostlist.get(position).getText());
holder.distance.setText(AnywallPostlist.get(position).getDis());
//Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(context,AnywallPostlist.get(position).getText(), Toast.LENGTH_LONG).show();
}
});
return view;
}
}
.
import com.parse.ParseClassName;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
/**
* Data model for a post.
*/
#ParseClassName("places")
public class AnywallPost extends ParseObject {
public String getText() {
return getString("text");
}
public void setText(String value) {
put("text", value);
}
public String getBank() {
return getString("bank");
}
public void setBank(String value) {
put("bank", value);
}
public ParseUser getUser() {
return getParseUser("user");
}
public void setUser(ParseUser value) {
put("user", value);
}
public String getType()
{
return getString("type");
}
public void setType(String value)
{
put("type",value);
}
public ParseGeoPoint getLocation() {
return getParseGeoPoint("location");
}
public void setLocation(ParseGeoPoint value) {
put("location", value);
}
public String getDis()
{
return getString("dis");
}
public void setdis(String value)
{
put("dis",value);
}
public static ParseQuery<AnywallPost> getQuery() {
return ParseQuery.getQuery(AnywallPost.class);
}
}
.
switch (getItemViewType(position)) {
case 0:
Bank bank = (Bank) getItem(position);
holder.name.setText(bank.name);
String str = String.valueOf(bank.numBranches);
holder.cn.setText(str);
imageLoader.DisplayImage(bank.image,
holder.logo);
Toast.makeText(getActivity(), "img"+bank.image,Toast.LENGTH_LONG).show();
// ... set the image here
// ... set the number of branches here
break;
case 1:
branch = (AnywallPost) getItem(position);
holder.name.setText(branch.getText());
holder.distance.setText(branch.getDis());
holder.b.setText(branch.getbranch());
imageLoader.DisplayImage(branch.getimg(),
holder.logo);
break;
case 2:
branch = (AnywallPost) getItem(position);
holder.name.setText(branch.getText());
holder.distance.setText(branch.getDis());
holder.b.setText(branch.getbranch());
imageLoader.DisplayImage(branch.getimg(),
holder.logo);
// ... set values for all the expanded view widgets too
break;
}
I have used this code to get image from parse and I add it to AnywallPost
ParseFile image = (ParseFile) country.get("image");
AnywallPost map = new AnywallPost();
map.setimg(image.getUrl());
then, I used this code to load the image and it worked fine
imageLoader.DisplayImage(AnywallPostlist.get(position).getimg(),
holder.logo);
Here is my idea:
You will have two layouts, a brief one with the name & distance, and an expanded one with the name & distance plus all the details.
The brief layout will have a view type of 0, and the expanded layout will have a view type of 1.
When you click on the item, you set a flag in the list model to indicate that the view has switched from brief view to long view. So you should see the view switch. If you click on an expanded view, it will revert to brief view.
I don't have a way to test this, but here is how I think the code will look:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
//ImageLoader imageLoader;
private List<AnywallPost> AnywallPostlist = null;
private ArrayList<AnywallPost> arraylist;
/** this array holds a flag for each item to show if it is in brief view more or expanded view mode*/
private boolean[] expandedView;
public ListViewAdapter(Context context,
List<AnywallPost> AnywallPostlist) {
this.context = context;
this.AnywallPostlist = AnywallPostlist;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<AnywallPost>();
this.arraylist.addAll(AnywallPostlist);
this.expandedView = new boolean[AnywallPostlist.size()];
}
public class ViewHolder {
TextView distance;
TextView name;
// ... add all your expanded view fields here too
}
#Override
public int getCount() {
return AnywallPostlist.size();
}
#Override
public int getViewTypeCount() {
return 2; // brief & expanded
}
#Override
public int getItemViewType(int position) {
return expandedView[position] ? 1 : 0;
}
#Override
public Object getItem(int position) {
return AnywallPostlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
final View row=view;
if (view == null) {
holder = new ViewHolder();
int layout = expandedView[position] ? R.layout.user_custom_expanded : R.layout.user_custom;
view = inflater.inflate(layout, parent, false);
// Locate the TextViews in listview_item.xml
holder.distance = (TextView) view.findViewById(R.id.disView);
holder.name = (TextView) view.findViewById(R.id.nameView);
if (expandedView[position]) {
// ... locate all the expanded view widgets too
}
view.setTag(holder);
} else {
// because the view type is determined by expandedView[position], the correct layout will be recycled here
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.name.setText(AnywallPostlist.get(position).getText());
holder.distance.setText(AnywallPostlist.get(position).getDis());
if (expandedView[position]) {
// ... set all the values for your expanded view too
}
//Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (expandedView[position]) {
// this is expanded view so toggle it back to brief view
expandedView[position] = false;
} else {
// clear any expanded views elsewhere
expandedView = new boolean[AnywallPostlist.size()];
// toggle brief view to expanded
expandedView[position] = true;
}
// forces the ListView to refresh and convert the brief view into an expanded view and vice versa.
ListViewAdapter.this.notifyDataSetChanged();
// Toast.makeText(context,AnywallPostlist.get(position).getText(), Toast.LENGTH_LONG).show();
}
});
if (expandedView[position]) {
// ... add all your event listeners for the expanded view widgets
}
return view;
}
}
You can also add a toggle button with expand/collapse indicator to the view to control the expand/collapse so you don't click anywhere on the view to change it.
You can do this with an ExpandableListView if you want, you just need to change to an ExpandableListAdapter and do your layouts a little differently.
Here is some code to make a heterogeneous list:
Take a really good look at the constructor to see how I divided up the branches by bank them strung them all together into one big list.
The list is just an Object array, so you figure out what the item is like this:
If it's a Bank, then well, it's a bank.
If it's an AnywallPost and expandedView[position] == false then it's a branch, brief view
If it's an AnywallPost and expandedView[position] == true then it's a branch, expanded view
public class Bank {
private String name;
private String image; // maybe this needs to be URL?
private int numBranches;
}
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private Object[] masterList;
/** this array holds a flag for each item to show if it is in brief view more or expanded view mode*/
private boolean[] expandedView;
public ListViewAdapter(Context context,
List<AnywallPost> AnywallPostlist) {
this.context = context;
inflater = LayoutInflater.from(context);
/*
* Phase I -- get list of banks and organize the branches by bank
*/
int count = 0;
List<String bankNames = new ArrayList<String>();
List<Bank> banks = new ArrayList<Bank>();
Map<String, Bank> bankMap = new HashMap<String, Bank>();
Map<String, List<AnywallPost>> mappedBranches = new HashMap<String, List<AnywallPost>>();
Bank bank = null;
List<AnywallPost> branches = null;
for (AnywallPost branch : AnywallPostlist) {
// have we started a branch list for this bank?
if (! bankNames.contains(branch.getBank())) { // no we haven't
// remember the bank name
bankNames.add(branch.getBank());
// init a new Bank
bank = new Bank();
bank.name = branch.getBank();
bank.image = branch.getImg(); // if this is URL make image in Bank a URL
banks.add(bank);
bankMap.put(bank.name, bank);
count++;
// create a list for this bank's branches and index it by bank
branches = new ArrayList<AnywallPost>();
mappedBranches.put(branch.getBank(), branches);
} else { // yes we have
// get the bank for this name
// this fixes the problem with the branch counts
bank = bankMap.get(branch.getBank());
// get the branch list we already created for this bank
branches = mappedBranches.get(branch.getBank());
}
// remember the branch for this bank
branches.add(branch);
count++;
// increment the number of branches for this bank
bank.numBranches++;
}
/*
* Phase II -- sort the banks and branches
*/
// ... here you can order the banks by name, branches by distance, etc.
/*
* Phase III -- create the mixed list
*/
int index = 0;
masterList = new Object[count];
for (Bank bank : banks) {
// append a bank to the list
masterList[index] = bank;
index++;
for (AnywallPost branch : mappedBranches.get(bank.name)) {
// append a branch to the list
masterList[index] = branch;
index++;
}
}
this.expandedView = new boolean[count];
}
public class ViewHolder {
TextView distance;
TextView name;
ImageView logo;
// ... add all your expanded view fields here too
}
#Override
public int getCount() {
return masterList.length;
}
#Override
public int getViewTypeCount() {
return 4; // bank brief, bank expanded, branch brief & branch expanded
}
#Override
public Object getItem(int position) {
return masterList[position];
}
#Override
public int getItemViewType(int position) {
if (getItem(position) instanceof Bank) {
return expandedView[position] ? 1 : 0;
}
return expandedView[position] ? 3 : 2;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
final View row=view;
if (view == null) {
holder = new ViewHolder();
int layout = 0;
switch (getItemViewType(position)) {
case 0:
layout = R.layout.bank_brief;
break;
case 1:
layout = R.layout.bank_expanded;
break;
case 2:
layout = R.layout.branch_brief;
break;
case 3:
layout = R.layout.branch_expanded;
break;
}
view = inflater.inflate(layout, parent, false);
switch (getItemViewType(position)) {
case 0:
holder.name = (TextView) view.findViewById(R.id.nameView);
break;
case 1:
holder.name = (TextView) view.findViewById(R.id.nameView);
// ... locate all the bank expanded view widgets too
break;
case 2:
holder.name = (TextView) view.findViewById(R.id.nameView);
holder.distance = (TextView) view.findViewById(R.id.disView);
break;
case 3:
holder.name = (TextView) view.findViewById(R.id.nameView);
holder.distance = (TextView) view.findViewById(R.id.disView);
// ... locate all the expanded view widgets too
break;
}
view.setTag(holder);
} else {
// because the view type is determined by expandedView[position], the correct layout will be recycled here
holder = (ViewHolder) view.getTag();
}
AnywallPost branch = null;
switch (getItemViewType(position)) {
case 0:
Bank bank = (Bank) getItem(position);
holder.name.setText(bank.name);
imageLoader.DisplayImage(bank.image, holder.logo);
// ... set the number of branches here
break;
case 1:
holder.name.setText(bank.name);
imageLoader.DisplayImage(bank.image, holder.logo);
// ... set the number of branches here
break;
case 1:
branch = (AnywallPost) getItem(position);
holder.name.setText(branch.getText());
holder.distance.setText(branch.getDis());
break;
case 2:
branch = (AnywallPost) getItem(position);
holder.name.setText(branch.getText());
holder.distance.setText(branch.getDis());
// ... set values for all the expanded view widgets too
break;
}
//Listen for ListView Item Click
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (expandedView[position]) {
// this is expanded view so toggle it back to brief view
expandedView[position] = false;
} else {
// clear any expanded views elsewhere
expandedView = new boolean[expandedView.length];
// toggle brief view to expanded
expandedView[position] = true;
}
if (getItem(position) instanceof AnywallPost) { // only for branches
// AnywallPost branch = (AnywallPost) getItem(position);
// Toast.makeText(context, branch.getText(), Toast.LENGTH_LONG).show();
}
// forces the ListView to refresh and convert the brief view into an expanded view and vice versa.
notifyDataSetChanged();
}
});
if (expandedView[position]) {
// ... add all your event listeners for the expanded view widgets
}
return view;
}
}

Android: radio group in list view not working

When i click the radio group from the first item all the
corresponding items should be selected when the check box is checked
if the check box is not checked then the corresponding items radio
button should not be checked.
if i click in between then first item radio button should be
deselect else if i click the unchecked radio button the check box
should be checked and radio button also be checked. This is what i
want to achieve in this.
My problem from the below code is when i click the first item all the items getting selected but if i click another item in between the first item is getting deselected and the button which i clicked is re positioned
TeacherAssessmentFragmentChild.java
package com.example.playit;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
#SuppressLint("NewApi")
public class TeacherAssessmentFragmentChild extends Fragment {
static Context context;
ArrayList<TeacherAssessmentFragmentChildContainer> assessmentContainer=new ArrayList<>();
View rootView ;
ListView lstassessment;
TeacherAssessmentFragmentChildListAdapter listAdapter;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.teacher_asseessment_view_fragment_child,
container, false);
Initialize();
context = TeacherAssessmentFragment.context;
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chapter", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 1 Algebra", "1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 2 Trignomentry", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 3 Integration", "4"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 4 Differentiation", "3"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 5 Abacus", "2"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 6 Numbersystem", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 7 Algebra", "-1"));
assessmentContainer.add(new TeacherAssessmentFragmentChildContainer("Chp - 8 Algebra", "2"));
listAdapter=new TeacherAssessmentFragmentChildListAdapter(context, 0, assessmentContainer);
lstassessment.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
return rootView;
}
public void Initialize() {
lstassessment=(ListView) rootView.findViewById(R.id.teacher_assessment_view_fragment_clild_assessment_list);
}
}
TeacherAssessmentFragmentChildContainer.java
package com.example.playit;
public class TeacherAssessmentFragmentChildContainer {
String chapterName,moduleSelected;
Boolean ismoduleActive;
public TeacherAssessmentFragmentChildContainer(String chapterName,
String moduleSelected) {
super();
this.chapterName = chapterName;
this.moduleSelected = moduleSelected;
if(moduleSelected.equalsIgnoreCase("-1"))
ismoduleActive=false;
else
ismoduleActive=true;
}
public Boolean getIsmoduleActive() {
return ismoduleActive;
}
public void setIsmoduleActive(Boolean ismoduleActive) {
this.ismoduleActive = ismoduleActive;
}
public String getChapterName() {
return chapterName;
}
public String getModuleSelected() {
return moduleSelected;
}
public void setModuleSelected(String moduleSelected) {
this.moduleSelected = moduleSelected;
}
}
TeacherAssessmentFragmentChildListAdapter
package com.example.playit;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
#SuppressLint("NewApi")
public class TeacherAssessmentFragmentChildListAdapter extends
ArrayAdapter<TeacherAssessmentFragmentChildContainer> {
static Context context;
static ArrayList<TeacherAssessmentFragmentChildContainer> container = new ArrayList<>();
ViewHolder holder;
static int checkBoxCount = 0, radioCount = 0, selectedPosition = -1;
public TeacherAssessmentFragmentChildListAdapter(Context context,
int resource,
ArrayList<TeacherAssessmentFragmentChildContainer> container) {
super(context, resource, container);
this.context = context;
this.container = container;
}
#Override
public int getViewTypeCount() {
// Count=Size of ArrayList.
return container.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
public class ViewHolder {
TextView txvchapterName;
CheckBox ckbselectName;
RadioGroup rgassessment;
RadioButton assessment1, assessment2, assessment3, assessment4,
assessment5;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// System.out.println("getview:" + position + " " + convertView);
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater
.inflate(
R.layout.teacher_assessment_view_fragment_child_list_adapter,
parent, false);
holder = new ViewHolder();
holder.txvchapterName = (TextView) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_chapter);
holder.ckbselectName = (CheckBox) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_select);
holder.rgassessment = (RadioGroup) row
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment_group);
holder.assessment1 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment1);
holder.assessment2 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment2);
holder.assessment3 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment3);
holder.assessment4 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment4);
holder.assessment5 = (RadioButton) holder.rgassessment
.findViewById(R.id.teacher_view_fragment_child_list_adapter_assessment5);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
if (position == 0) {
holder.assessment1.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment2.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment3.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment4.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment5.setButtonDrawable(android.R.drawable.btn_radio);
holder.assessment1.setText("");
holder.assessment2.setText("");
holder.assessment3.setText("");
holder.assessment4.setText("");
holder.assessment5.setText("");
} else {
holder.assessment1.setButtonDrawable(android.R.color.transparent);
holder.assessment2.setButtonDrawable(android.R.color.transparent);
holder.assessment3.setButtonDrawable(android.R.color.transparent);
holder.assessment4.setButtonDrawable(android.R.color.transparent);
holder.assessment5.setButtonDrawable(android.R.color.transparent);
holder.assessment1.setText("M1");
holder.assessment2.setText("M2");
holder.assessment3.setText("M3");
holder.assessment4.setText("M4");
holder.assessment5.setText("M5");
}
holder.ckbselectName.setTag(position);
holder.rgassessment.setTag(position);
SetCheckedRbn(container.get(position).getModuleSelected());
SetCheckedChb(container.get(position).ismoduleActive);
holder.ckbselectName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = (Integer) v.getTag();
System.out.println("selectedPosition : "+selectedPosition);
System.out.println("container.get(selectedPosition).getIsmoduleActive() : "+container.get(selectedPosition).getIsmoduleActive());
if(selectedPosition==0){
for(int i=0;i<container.size();i++){
if(!container.get(i).getIsmoduleActive()){
container.get(i).setIsmoduleActive(true);
container.get(i).setModuleSelected("0");
}
else {
container.get(i).setIsmoduleActive(false);
container.get(i).setModuleSelected("-1");
}
}
}
else {
if(!container.get(selectedPosition).getIsmoduleActive()){
container.get(selectedPosition).setIsmoduleActive(true);
container.get(selectedPosition).setModuleSelected("0");
}
else {
container.get(selectedPosition).setIsmoduleActive(false);
container.get(selectedPosition).setModuleSelected("-1");
}
}
System.out.println("container.get(selectedPosition).getIsmoduleActive() : "+container.get(selectedPosition).getIsmoduleActive());
notifyDataSetChanged();
}
});
holder.txvchapterName.setText(container.get(position).getChapterName());
holder.rgassessment
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
selectedPosition = (Integer) group.getTag();
View radioButtonView = group.findViewById(checkedId);
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
int idx = group.indexOfChild(radioButtonView);
if (selectedPosition == 0 && checkedRadioButton.isChecked() ) {
for (int i = 0; i < container.size(); i++) {
if (container.get(i).getIsmoduleActive() || i==0 ){
container.get(i).setModuleSelected("" + idx);
}
else if(i!=0){
container.get(i).setModuleSelected("-1");
container.get(i).setIsmoduleActive(false);
}
}
}
else if(selectedPosition !=0){
container.get(selectedPosition).setIsmoduleActive(
true);
container.get(selectedPosition).setModuleSelected(
"" + idx);
}
checkrb();
}
});
return row;
}
public void SetCheckedRbn(String str) {
switch (str) {
case "0":
holder.assessment1.setChecked(true);
break;
case "1":
holder.assessment2.setChecked(true);
break;
case "2":
holder.assessment3.setChecked(true);
break;
case "3":
holder.assessment4.setChecked(true);
break;
case "4":
holder.assessment5.setChecked(true);
break;
default:
holder.assessment1.setChecked(false);
holder.assessment2.setChecked(false);
holder.assessment3.setChecked(false);
holder.assessment4.setChecked(false);
holder.assessment5.setChecked(false);
break;
}
}
public void SetCheckedChb(Boolean isactive) {
if (isactive)
holder.ckbselectName.setChecked(true);
else
holder.ckbselectName.setChecked(false);
}
public void checkrb() {
int rbcount=0,chcount=0;
String str="";
for(int i=0;i<container.size();i++) {
str=container.get(i).getModuleSelected();
if(str!="-1"){
break;
}
}
for(int i=1;i<container.size() && str!="";i++) {
if(str.equals(container.get(i).getModuleSelected()) && container.get(i).getIsmoduleActive()){
rbcount++;
str=container.get(i).getModuleSelected();
}
if(container.get(i).getIsmoduleActive())
chcount++;
}
if(rbcount!=chcount)
container.get(0).setModuleSelected("-1");
else
container.get(0).setModuleSelected(str);
if(rbcount==container.size())
container.get(0).setIsmoduleActive(true);
else
container.get(0).setIsmoduleActive(false);
notifyDataSetChanged();
}
public void checkchb() {
int chcount=0;
for(int i=0;i<container.size();i++) {
if(container.get(i).getIsmoduleActive())
chcount++;
}
if(chcount==(container.size()-1)){
container.get(0).setIsmoduleActive(true);
}
else
container.get(0).setIsmoduleActive(false);
notifyDataSetChanged();
}
}

Get text content in EditText ListView after click button issue

I have a Listview of EditText and I need to get the String values of each edited row when I click a confirm button, but I don't know how.
I have tried to adapt some sample with no success ( I get always the default values and not the edited values).
My attempt is this
public class MyActivity extends Activity {
static int nItems;
ImageButton confirmButton;
ListView myList;
ListViewAdapterEditText adapterG1, adapterG2, adapterG3;
#Override
public void onCreate(Bundle savedInstanceState) {
.....
myList = (ListView) findViewById(R.id.listaG1);
myList.setItemsCanFocus(true);
adapterG1 = new ListViewAdapterEditText();
myList.setAdapter(adapterG1);
}
OnClickListener mConfirmButtonListener = new OnClickListener() {
public void onClick(View v) {
ArrayList a1 = adapterG1.getItems();
for (int i = 0; i < nItems; i++) {
System.out.println(a1.get(i)
+ "\n\n");
}
};
public class ListViewAdapterEditText extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
ListItem listItem;
public ListViewAdapterEditText() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < nItems; i++) {
listItem = new ListItem();
listItem.caption = "Caption" + i;
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public ArrayList<String> getItems() {
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < nItems; i++) {
ListItem li = (ListItem) myItems.get(i);
items.add(li.getCaption());
}
return items;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item_row,
null);
holder.caption = (EditText) convertView
.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill EditText with the value you have in data source
holder.caption.setText(((ListItem) myItems.get(position)).caption);
// holder.caption.setText(myItems.get(position).caption);
holder.caption.setId(position);
// we need to update adapter once we finish with editing
holder.caption
.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
((ListItem) myItems.get(position)).caption = Caption
.getText().toString();
}
}
});
return convertView;
}
class ViewHolder {
EditText caption;
}
class ListItem {
String caption;
public String getCaption() {
return caption;
}
}
}
}
Could someone help me to solve this problem?
Are you sure your OnFocusChangeListener is called? If you edit the text in EditText then tap the confirm button, this listener will not be called in touch mode since the focus is still on the EditText.
Update: Consider the situation you edited the text in a EditText while didn't confirm and scrolled the ListView so that the item view is recycled, I'm not sure what is your preferred way, but if you want store the edited data, you can use setRecyclerListener(android.widget.AbsListView.RecyclerListener) to get notified when a item view is recycled so you can saved the edit result. To save the result of EditText showing on screen, you use methods like getChildAt to get item views visible on screen then get the EditText's text.
Update2: Another better and clean way is use TextWatcher and addTextChangeListener, this will notifies you when the text in EditText is changed.
Update3: I just write the following sample and test it, and it works on my phone. :)
Update4: I removed the previous code cause its performance is bad and creates a lot objects, you can check the following full sample instead:
Activity code :
package com.example.asynctasktest;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
/**
* #author Daniel Chow
*
* May 26, 2013 12:57:49 AM
*/
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TestAdapter adapter = new TestAdapter(this);
listView.setAdapter(adapter);
Button confirmButton = (Button) findViewById(R.id.confirm_button);
confirmButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
List<String> items = adapter.getItems();
for (int i = 0, n = items.size(); i < n; i++) {
Log.e("", items.get(i));
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Adapter code:
/**
*
*/
package com.example.asynctasktest;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
/**
* #author Daniel Chow
*
* May 26, 2013 1:13:02 AM */
public class TestAdapter extends BaseAdapter {
private List<String> items = new ArrayList<String>();
private Context context;
public TestAdapter(Context context) {
this.context = context;
for (int i = 0; i < 12; i++) {
items.add("caption " + i);
}
}
public List<String> getItems() {
return new ArrayList<String>(items);
}
#Override
public int getCount() {
return 12;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = new EditText(context);
holder = new ViewHolder();
holder.editText = (EditText) convertView;
holder.watcher = new EditTextWatcher();
holder.editText.addTextChangedListener(holder.watcher);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.watcher.setTarget(position);
holder.editText.setText(items.get(position));
return convertView;
}
private class EditTextWatcher implements TextWatcher {
private int target;
public void setTarget(int target) {
this.target = target;
}
#Override
public void afterTextChanged(Editable s) {
items.set(target, s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
private static class ViewHolder {
EditText editText;
EditTextWatcher watcher;
}
}
I usually follow a simpler technique
public class Item_Adapter extends BaseAdapter {
private String[] Val;
public Item_Adapter () {
Val= new String[nItems];
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
///bla bla
holder.caption
.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
((ListItem) myItems.get(position)).caption = Caption
.getText().toString();
Val[position]= Caption
.getText().toString();
}
}
});
///bla bla
return convertView;
}
/////most important returning your array so you can use it in the Activity
public String[] getVal() {
return Val;
}

Android unsorted List

I have created a listActivity with my own ListAdapter. The problem is that the list is viewed in order once launched. But When I scroll down, or go back from another activity, the listView is completely out of order.
I thought the problem was in ArrayList but no, the list is sorted and i'm sure of it because when I loop over all elements in the ArrayList they are printed in the log the same way I inserted them.
I pasted the adapter code below in case anyone would like to check it.
package com.anubis.mail;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class EmailAdapter extends BaseAdapter {
private ArrayList<EmailModel> elements;
private Context c;
public EmailAdapter(Context c, ArrayList<EmailModel> Emails) {
this.elements = Emails;
this.c = c;
}
public int getCount() {
return elements.size();
}
public Object getItem(int position) {
return elements.get(position);
}
public long getItemId(int id) {
return id;
}
public void Remove(int id) {
notifyDataSetChanged();
}
public void Add(EmailModel email) {
this.elements.add(email);
for (EmailModel e : elements){
Log.v("EmailAdapter", e.getSubject());
}
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout rowLayout;
EmailModel email = elements.get(position);
if (convertView == null) {
rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false);
TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());
String body_hint = " - " + email.getBodyHint();
TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);
String sender_name = get_sender_name(email.getSender());
TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);
TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview);
date_time_textview.setText(email.getTime());
} else {
rowLayout = (LinearLayout) convertView;
}
return rowLayout;
}
private String get_sender_name(String from) {
String[] sender = from.split("<");
String sender_name;
try {
sender_name = sender[0];
} catch (Exception e) {
sender_name = sender[1];
}
return sender_name;
}
}
You need to move code after the IF
if (convertView == null) {
rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false);
} else {
rowLayout = (LinearLayout) convertView;
}
TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());
String body_hint = " - " + email.getBodyHint();
TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);
String sender_name = get_sender_name(email.getSender());
TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);
TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview);
date_time_textview.setText(email.getTime());
return rowLayout;

Categories

Resources