Adding a single string from an array list to TextView - java

My Code: starts with after onCreate Override then the the bottom portion is my ArrayAdapter. Im trying to add individual strings from the array to a TextView. When I run the app it puts all the strings in the array on every TextView it makes. Thanks for taking a look.
MyListAdapter adapter = new MyListAdapter(mMyStringArrayArray());
setListAdapter(adapter);
...
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter(ArrayList<String> strings) {
super(getActivity(), 0, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView[] strings = new TextView[mMyStringArray.size()];
for (int i = 0; i < mMyStringArray().size(); i++) {
// Create TextView
strings[i] = new TextView(getActivity());
strings[i].setText(mMyStringsArray(i));
strings[i].setId(i + 5);
mLinearLayout.addView(strings[i]);
}
return convertView;
}
}

getView() is called once for each item in your Adapter. So, with your for loop, that is running through each item in mMyStringArray for every item in your Adapter. Try instead to use position to get the element you need. Something like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView[] strings = new TextView[mMyStringArray.size()];
// Create TextView
strings[position] = new TextView(getActivity());
strings[position].setText(mMyStringsArray(i));
strings[position].setId(position + 5);
mLinearLayout.addView(strings[position]);
}
return convertView;
}
Something like that should work. However, since you have the layout it would be much easier to just create a TextView inside your LinearLayout of my_list.xml and use that.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView tv = (TextView) convertView.findViewById(R.id.someID) //where someID is the id you give the TextView in your layout file
tv.setText(mMyStringsArray(position));
tv.setId(position + 5); // Shouldn't need this now
}
return convertView;
}
I also suggest watching The world of ListView

Related

unable to reference another layout in baseadapter android

i have an extended baseadapter class which i'm using for populating listview.
i want to change visibility of an element in another layout file(parent file containing listview element) but i'm unable to find a suitable solution.
i tried setting second inflater, executing another class and in fragment but to no avail. however click is detected fine since i'm able to log that in console
convertView = layoutInflater.inflate(R.layout.list_main, null);
final View convertView1 = layoutInflater.inflate(R.layout.tab_fragment_1, null);
that wont work obviously because we could return only one view so i tried declaring another class but still same result
for some reason i'm unable to register click in mainactivity. even that could help
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("ArrayList Of Vssalues :");
}
});
baseadapter getview class
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_main, null);
final View convertView1 = layoutInflater.inflate(R.layout.tab_fragment_1, null);
final RelativeLayout calendartoshow = (RelativeLayout) row.findViewById(R.id.calendarlay);
final RelativeLayout list_menu = (RelativeLayout) convertView.findViewById(R.id.list_menu);
Button list_but = (Button) list_menu.findViewById(R.id.book_button);
holder = new ViewHolder();
//getView1();
holder.icon = (ImageView)convertView.findViewById(R.id.image);
holder.icon.setImageResource(listData[position]);
holder.text = (TextView)convertView.findViewById(R.id.textv);
final Button holder_but = (Button)convertView.findViewById(R.id.book_button);
holder_but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calendartoshow.setVisibility(View.VISIBLE);
System.out.println("ArrayLissst Of Values :");
//new clickclass();
// getView1(position,convertView1,parent);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(TEXTS[position]);
return convertView;
}

How do I only show edittext for current user's listview item?

I've searched for this question already, and I couldn't find it anywhere, and I'm not even sure how to exactly word it.
I have a listview with images, text, buttons, etc. for each item. I also have an edittext for the items, but I only want the edittext to be visible for the current user. The same way you're only able to edit your own post on Facebook, not every other one as well.
Here's what I've tried:
holder.editcaption = (EditText) itemView.findViewById(R.id.editText1);
(...)
if(mCurrentUser.getUsername()!=currentItem.getSender())
holder.editcaption.setVisibility(View.INVISIBLE);
This is in the getView() of the adapter. The problem is that either all of the edittexts become invisible or visible when I only need every edittext except for the current user's to be invisible. I only need the edittext view of the current item to change.
Any help is very much appreciated.
private class MyListAdapter extends ArrayAdapter<GalleryItem>{
public MyListAdapter(){
super(getActivity(), R.layout.item_view, thelist);
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if(items.get(position).getUsername()!=mCurrentUser.getUsername())
return 2;
else return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
currentItem = adapter.getItem(position);
int type = getItemViewType(position);
ViewHolder holder;
View itemView = null;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) getListView().getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
if(type==0)
itemView = inflater.inflate(R.layout.item_view, parent, false);
else
itemView = inflater.inflate(R.layout.currentuser_item_view, parent, false);
holder = new ViewHolder();
holder.image = (ImageView)itemView.findViewById(R.id.imgAvatar);
holder.text = (TextView) itemView.findViewById(R.id.newtext);
holder.profilepicture = (ProfilePictureView) itemView.findViewById(R.id.profilePicture);
holder.timelabel = (TextView) itemView.findViewById(R.id.timeposted);
holder.post_caption = (TextView) itemView.findViewById(R.id.caption);
holder.editcaption = (EditText) itemView.findViewById(R.id.editText1);
itemView.setTag(holder);
}
else{
itemView = convertView;
holder = (ViewHolder) itemView.getTag();
}

Populate GridView from Array

I have an array of image urls and I want to download the images using Picasso and show them in a grid view. My current implementation works but it is putting the last image into every image view in the grid view.
public class GridViewAdapter extends ArrayAdapter {
Context context;
public GridViewAdapter(Context context) {
super(context, 0);
this.context = context;
}
public int getCount() {
return thumbnailURLS.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
ImageView gridImageView = (ImageView)row.findViewById(R.id.gridImageView);
for(String s : thumbnailURLS) {
Picasso.with(context)
.load(s)
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(gridImageView);
}
}
return row;
}
}
getView is called once for each item (ie the number of times it is called is equal to 'getCount'). Easiest thing to do would be to ditch the for loop and lookup the thumbnailUrl using the position argument.
ie
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
}
ImageView gridImageView = (ImageView) row.findViewById(R.id.gridImageView);
Picasso.with(context)
.load(thumbnailURLs.get(position))
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(gridImageView);
return row;
}
Your getView is actually loading every single image into the list item, and so only the last one becomes the visible one!
The correct solution would be as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
}
ImageView gridImageView = (ImageView) row.findViewById(R.id.gridImageView);
Picasso.with(context).load(thumbnailURLS.get(position)).placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder).into(gridImageView);
return row;
}
You should also look into using the ViewHolder pattern (http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder) such that you are not doing a findViewById every time getView is being called!
try this
// Get the image URL for the current position.
String url = getItem(position);
// Trigger the download of the URL asynchronously into the image view.
Picasso.with(context) //
.load(url) //
.placeholder(R.drawable.placeholder) //
.error(R.drawable.error) //
.fit() //
.into(view);

how do i change the image for each row in this list view

im new to android and java and i created a my own adapter and list view from a tutorial I was following online. I want to know how do i the image for each individual row because my adapter (from the tutorial) only has 1 image for all the rows. here is the code for my own adapter.
class HangarAdapter extends ArrayAdapter<String> {
public HangarAdapter(Context context, String[] values) {
super(context, R.layout.hangar_layout, values);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
String ship = getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.textView1);
theTextView.setText(ship);
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
theImageView.setImageResource(R.drawable.dot);
return theView;
}
}
theImageView.setImageResource(R.drawable.dot);
This line above is what you have to change in order to use a different images;
you could potentially do something like this;
if (position == 0){
theImageView.setImageResource(R.drawable.dot);
} else if (position == 1){
theImageView.setImageResource(R.drawable.circle);
}
It also depends on the backing data.

Current position on custom ListView

I have a custom ListView. This ListView contain of 1 Image and 3 TextView. How can I click on ImageView and get current position. (Do not click to other elements, only ImageView)
In your custom adapter class, you are havinggetView(.. .. ..) method :
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// >>> >>> ^^^^^^ This is your posotion = index
View row = convertView;
Your_Holder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resId, parent, false);
holder = new Your_Holder();
....
....
holder.yourImageView = (ImageView) row.findViewById(R.id.yourImageID);
....
....
row.setTag(holder);
}
else{
holder = (Your_Holder)row.getTag();
}
// Set here your images click event
holder.yourImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Position = " + position)
}
});
}

Categories