DIsplay first letter of string to another textview in Android - android

I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}

Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));

Related

how to implement two objects in a single ViewHolder?

I have got two lists, cart and promos. I want both to be displayed in a single viewholder. Is there any way I can do it? this is how I did, but it is not working.
public class CartAdapter extends BaseAdapter {
ArrayList<Cart> list1;
Context context;
public CartAdapter(Context context, ArrayList<Cart> list) {
this.context = context;
this.list1 = list;
}
#Override
public int getCount() {
return list1.size();
}
#Override
public Object getItem(int position) {
return list1.get(position);
}
#Override
public long getItemId(int position) {
return list1.get(position).getProductId();
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cart_item, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Cart cart = list.get(position);
final Promos promo = databaseHandler.getPromosForCart(cart);
holder.tvTitle.setText(cart.getProductName());
holder.tvPrice.setText(cart.getPrice());
holder.etQuantity.setText(cart.getQuantity());
holder.tvPromotion.setVisibility(View.GONE);
if(cart.hasPromotion){
final ViewHolder holder2;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cart_item, parent, false);
holder2 = new ViewHolder(row);
row.setTag(holder);
} else {
holder2 = (ViewHolder) row.getTag();
}
if(promo.type().equals("type1"){
holder2.tvTitle.setText(promo.getProductName());
holder2.tvPrice.setVisibility(View.GONE);
holder2.etQuantity.setVisibility(View.GONE);
holder2.tvPromotion.setText(promo.getPromoDetails());
}else{
holder2.tvTitle.setText(promo.getProductName());
holder2.tvPrice.setVisibility(View.GONE);
holder2.etQuantity.(promo.getQuantity());
holder2.etQuantity.setEnabled(false);
holder2.tvPromotion.setText(promo.getPromoDetails());
}
}
}
Class ViewHolder{
.....
ViewHolder(View v){}
}
}
I searched but all answers I got was about using two layouts, and using only one data list. Please help me.

App to choose layout according to situations

I am making a messaging android app, but my question is, how can I make the app choose either to pick the right hand layout or left hand layout? Like we see in messaging apps like WhatsApp, if I am the sender, my Message is wrapped in a right hand layout whilst the persons messages am chatting with are wrapped in a left handed layout, How can I achieve this type of Interface or layout I should say?
Below is my code, the app simple receives data from a remote database and puts it in a listview, using a custom adapter:
// the Data being set to the adapter
private ArrayList<ListItem> getListData() {
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
String[] username = mUsername.toArray(new String[mUsername.size()]);
String[] comments = Comment.toArray(new String[Comment.size()]);
for (int i = 0; i < username.length; i++) {
ListItem newsData = new ListItem();
newsData.setUsername(username[i]);
newsData.setmComment(comments[i]);
listMockData.add(newsData);
System.gc();
}
return listMockData;
}
And my custom adapter utilizing a single view:
public class CommentAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
ViewHolder holder;
public CommentAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.singlecomment, null);
holder = new ViewHolder();
holder.username = (TextView) convertView.findViewById(R.id.usernme);
holder.message = (TextView) convertView.findViewById(R.id.postCommentBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
holder.username.setText(newsItem.getUsername());
holder.message.setText(newsItem.getmComment());
return convertView;
}
static class ViewHolder {
TextView username;
TextView message;
}
}
Assuming that your ListItem object has a field named isSender to check whether he is the sender or not.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.singlecomment, null);
holder = new ViewHolder();
holder.username = (TextView) convertView.findViewById(R.id.usernme);
holder.message = (TextView) convertView.findViewById(R.id.postCommentBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
if(newsItem.getIsSender()) {
holder.usernameRight.setText(newsItem.getUsername());
holder.messageRight.setText(newsItem.getmComment());
holder.usernameLeft.setVisibility(View.GONE); // or something like that to hide
holder.messageLeft.setText(View.GONE);
} else {
holder.usernameLeft.setText(newsItem.getUsername());
holder.messageLeft.setText(newsItem.getmComment());
holder.usernameRight.setVisibility(View.GONE);
holder.messageRight.setText(View.GONE);
}
return convertView;
}

Android Listview with Image and Text

So I have a listview that has both text and images in it.
Everything about it works except when I am presented with the top row not having an image assigned to it.
rather than it using the placeholder image I have for rows that have no image assigned it takes the image of the current user.
The current user in this image is number 3, but his picture is also being shown for user 1.
here is my code for the CustomListAdapter, it grabs the PlayerItem which has players name and image url.
public class CustomListAdapter extends BaseAdapter {
private ArrayList<PlayerItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<PlayerItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.rosterrow, null);
holder = new ViewHolder();
holder.playerNameView = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PlayerItem playerItem = (PlayerItem) listData.get(position);
holder.playerNameView.setText(playerItem.getplayerName());
if ((holder.imageView != null) && (playerItem.getUrl() != null)) {
Picasso.with(null).load(playerItem.getUrl()).into(holder.imageView);
}
return convertView;
}
static class ViewHolder {
TextView playerNameView;
ImageView imageView;
}
}
Fixed it by adding
else {
holder.imageView.setImageResource(R.drawable.list_placeholder);
}

getview is not called in BaseAdapter

I have a problem with my custom adapter. the method getView() is not called. I searched on my friends "google" and "stackoverflow" but anything fix my problem.
public class custom_adapter extends BaseAdapter {
private List<Activities> listData;
private LayoutInflater layoutInflater;
public custom_adapter(Context context,
List<Activities> listActivity) {
this.listData = listActivity;
layoutInflater = layoutInflater.from(context);
Log.v("BaseAdapter", "custom_adapter");
}
#Override
public int getCount() {
Log.v("BaseAdapter - getCount", String.valueOf(listData.size()));
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("BaseAdapter", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities newsItem = (Activities) listData.get(position);
holder.name.setText(newsItem.getName());
holder.ratingBar.setRating(newsItem.getAverageMark());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
}
return convertView;
}
public static class ViewHolder {
TextView name;
RatingBar ratingBar;
ImageView imageView;
}
}
Furthermore, GetCount is not null !
I will be very glade if you can help me
Regards
K.L
For the purpose of handling List of some objects, extending generic ArrayAdapter may be better choice instead of just BaseAdapter. It will handle a lot of things for you. Your whole adapter class could look like.
public class ActivitiesAdapter extends ArrayAdapter<Activities>
{
private LayoutInflater layoutInflater;
public ActivitiesAdapter(Context context, List<Activities> objects)
{
super(context, 0, objects);
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("BaseAdapter", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities newsItem = getItem(position);
holder.name.setText(newsItem.getName());
holder.ratingBar.setRating(newsItem.getAverageMark());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
}
return convertView;
}
public static class ViewHolder {
TextView name;
RatingBar ratingBar;
ImageView imageView;
}
}
and of course you have to set this adapter to your ListView instance. Like
ActivitiesAdapter adapter = new ActivitiesAdapter(this, yourActivitiesList);
ListView listView = (ListView)findViewById(R.id.my_list_id);
listView.setAdapter(adapter);
or simply setListAdapter(adapter); if you are using ListActivity or ListFragment;
I had the same problem. Maybe, the problem is that you don't notify your adapter that you've changed data in the adapter. Try this code in your adapter:
#Override
public int getCount() {
return data.size();
}
And call notifyDataSetChanged(); when you change the data in Adapter.

content of listview does not appear

my code is running without error but the custom Listview does not display the values .
my code is running without error but the custom Listview does not display the values .
this is my code:
the Adapter
public class Adapter extends BaseAdapter{
ArrayList<mealItems> itemList;
public Activity context;
public LayoutInflater inflater;
public DiseasesAdapter(Activity context,int x,ArrayList<mealItems> itemList) {
super();
this.context = context;
this.itemList = itemList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
ImageView img;
TextView Title;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.disease_row, null);
holder.Title = (TextView) convertView.findViewById(R.id.Name);
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
Items bean = (Items) itemList.get(position);
holder.Title.setText(bean.getTitle());
holder.img.setImageResource(bean.getImage());
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
Your getCount() method returns a 0, this should return the size of the itemList instead.
i.e return itemList.size()
Try this :
this.itemList=new ArrayList<mealItems>();
this.itemList.addAll(itemList);
And of course, the getCount() method to return itemList.size()

Categories

Resources