Setting a List to include 2 TextViews - android

I have a list adapter that holds a TextView and an ImageView. I can set the image and the text fro each item on the list. The variable for TextView is tvFullName.
Can I have tvFullName to set an item in the list to a text and have tvFullNameOne
also set the item in the list to a text?
Like two variables which each create items in the list with text?
This is my layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:padding="5dp" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/profile" />
<TextView
android:id="#+id/tvFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="16sp"
android:paddingLeft="5dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Adapter........
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.relationship_inflater, null);
Holder holder = new Holder();
//connectOrDisconnectUser();
holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName);
holder.tvFullName.setText(usersInfo.get(position).get(
Relationship.TAG_USERNAME));
imageLoader.DisplayImage(
usersInfo.get(position).get(Relationship.TAG_PROFILE_PICTURE),
holder.ivPhoto);
return view;
}

What I tried made the two items I set as the same ListView therefore had the same onClick value
If you want the click events on the view elements within the list, you can use individual listeners.
For example, with your current code
holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
// holder.ivPhoto.setOnClickListener();
holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName);
// holder.tvFullName.setOnClickListener();
Or you can check which view was clicked in the item listener set on the listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
switch(viewId) {
case R.id.ivImage:
Toast.makeText(MainActivity.this, "Image clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.tvFullName:
Toast.makeText(MainActivity.this, "Full name clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
As for your question, yes, of course you can add whatever extra text you want.

Related

How to get a textView value from ListView adapter onClick button inside same view?

I have a list View which contains imageView working as button and a TextView.
what I want is when I click on the button for example for the third item in the list view I want to get the text that in the textview when I click on the button which is in the same position as the text view..
I'll try with
final TextView txt= (TextView) findViewById(R.id.txt);
String txt2 = txt.getText().toString();
That's work more and less.
my problem it's so strange, I will try to explain with images
When I click on the button irrespective of the position, allways it return the textview which display on the fisrt position on the screen (not on listview, on screen)
for example
On that situation, (red square its screen) if I clicked on the button from the item 1,2 or 3 it return tv_1.
other example (again red square its screen)
Now, if I click on the button from the item 2,3 or 4 it return tv_2
that is, always it displays the top of the list of visible item on screen
My code
public void btn(View view)
{
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
Any suggestions?
UPDATE
The problem its because I need to use final variable to get only that textview, then its report first one item is displayed on screen, but... why? or how to solve?
UPDATE 2 MY XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:layout_marginRight="26dp"
android:id="#+id/lay_fondo">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/cardview_notify"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="#color/primary_green"
android:id="#+id/lay_prioridad"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="580dp"
android:layout_height="match_parent"
android:layout_weight="0.54">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:id="#+id/tv_id"
android:layout_gravity="center_vertical"
android:textColor="#color/black"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="26dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/img_favorito"
android:src="#drawable/star"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_onClick="btn"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
Try to replace the below line
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
with
TextView tv_id = (TextView) ((View) view.getParent()).findViewById(R.id.tv_id);
The above line finds textview with in the given view,
OR
Add an OnItemClickListener() for and list view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
});
Try to set onItemClickListener to your listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView txt= (TextView) view.findViewById(R.id.txt);
String txt2 = txt.getText().toString();
Log.i(LOGTAG, "" + txt2);
}
});
OR
Set click listener for TextView inside your
getView(int position, View convertView, ViewGroup parent)
my suggestions;
tv_id is referencing only one item (R.if.tv_id). for this matter wherever you put your button, it will always call the code handled by tv_id.
I suggest you reference the list view and setonclicklistener on the listView.
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
Why do you want to get the text in so complicated way?
I assume you have your custom ListView adapter (e.g. MyAdapter) and your items (e.g. MyItem) with a getter (e.g. MyItem :: getText(), don't mind C++ style).
Then your approach could be:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Probably this check is not that necessary but better safe than sorry
if (listView.getAdapter() instanceof MyAdapter) {
MyAdapter adapter = (MyAdapter)listView.getAdapter();
String text = adapter.getItem(position).getText();
// Do whatever you need with the text
}
}
});

Optimizing ListView adapter to display dynamic content in individual ListView items

I'm trying to display a ListView where individual list items feature a dynamic number of elements. Each item list will have a TextView for a date. Following the date, there can be between 2-5 elements featured.
ListItemA
DateTextView
2-5 TextView
ListItemB
DateTextView
2-5 TextView
ListItemC
DateTextView
2-5 TextView
...
With this current method, it displays the blank TextViews at the bottom of the ListView's item. Would there be a better way to do this?
I thought that convertView would have had methods to add layout elements to it, but that's not the case.
Adapter's getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.layout_list_item, parent, false);
holder.text_date_cashout = (TextView) convertView.findViewById(R.id.text_date_cashout);
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout1));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout2));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout3));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout4));
holder.dynamic_views.add((TextView) convertView.findViewById(R.id.text_cashout5));
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
//get and set date from current cashOutHistory
CashOutHistory coh = cashOutHistory.get(position);
holder.text_date_cashout.setText(coh.getDate());
//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
holder.dynamic_views.get(i).setText(co.toString());
i++;
}
return convertView;
}
private class ViewHolder{
TextView text_date_cashout;
ArrayList<TextView> dynamic_views = new ArrayList<TextView>(5);
}
List item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_date_cashout" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/text_cashout5" />
</LinearLayout>
Edit #1
Loop showing and hidding the TextViews in my list item:
//loop individual cashOuts to display their values
int i = 0;
for (CashOut co : coh.getCashOutHistory() ){
holder.dynamic_views.get(i).setText(co.toString());
i++;
}
while(i < 5){
holder.dynamic_views.get(i).setVisibility(View.GONE);
i++;
}
You can hide each view you don't need showing for that particular item by using:
Boolean someCondition = true;
if (someCondition)
holder.dynamic_views.get(i).setVisibility(View.GONE);
Hope this helps.

how to add multiple imageview on listview row with horizontal scroll view

I want to display multiple imageview and text view on every row of listview and number of row is not fix (number of rows integer can be fetched from server)
How can i add horizontal scroll view in every row.
i am going to use for loop for number of imageview in getView method is it right way to do that.
My question is how can i inflate horizontal scroll with numbers image views in row in getview method
public View getView(final int position, View convertView,
ViewGroup parent)
{
final ViewHolder viewHolder;
viewHolder = new ViewHolder();
// View vi = convertView;
//if (vi == null)
//{
int Numberofimageview=2;
for(int i=0;i<Numberofimageview;i++)
{
// assume i want to display 2 imageview in every row
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_row_layout, parent,
false);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
Log.d("myapp", "position" + position);
convertView.setTag(viewHolder);
// }
// else
// {
// viewHolder=(ViewHolder)convertView.getTag();
// }
viewHolder.title.setText(data.get(position));
}
return convertView;
}
This is xml for row
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/wagon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/wagon" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/wagon"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:src="#drawable/video" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/imageView1"
android:text="title of file" />
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/title"
android:layout_alignRight="#+id/title"
android:layout_marginBottom="18dp"
android:text="type of file" />
Try to include your relativeLayout into a HorizontalScrollView
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout>[...]</RelativeLayout>
</HorizontalScrollView>
Create a custom listview. Create a xml file. within the main linear layout of the xml file put imageview and textview. Make sure the orientation of the linear layout is horizontal.. Make use of that xml file in getview methood

List view first element no clickable

I have a list of element in a fragment.
I want to disable the title of list
http://bugs.haploid.fr/file_download.php?file_id=856&type=bug
here are the code the cell of my list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/stations_listView_layout">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/header_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingBottom="5dip"
android:layout_marginTop="29dip"
android:text="A"
style="#style/FragmentTitleSecondary">
</com.infrabel.railtime.views.RobotoMediumTextView>
<View
android:id="#+id/header_bar"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#color/fragment_title_color">
</View>
<RelativeLayout
style="#style/stations_listView_inner_layout"
android:layout_width="fill_parent"
android:layout_height="60dip">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:textSize="#dimen/menu_list_item_primary_text_size"
android:textColor="#color/grey_normal">
</com.infrabel.railtime.views.RobotoMediumTextView>
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="25dip"
android:textSize="#dimen/fragment_title_text_size"
android:textColor="#color/grey_light">
</com.infrabel.railtime.views.RobotoMediumTextView>
</RelativeLayout>
</LinearLayout>
i tried to set android:clickable = false or android:focusable="false" and it doesn't work.
One way to make certain ListView item not clickable is:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// FIRST ITEM - DO NOTHING
} else {
// DO SOMETHING IF YOU WISH...
}
}
});
another way would be to modify your custom adapter:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.item,
listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView v = (TextView) super.getView(position,
convertView, parent);
if(position == 0) {
v.setClickable(false);
// havent tested this thou...
}
}
});
got it , i have in my cell.xml
so the text will be drawn every time when i add cell.
we must declare this in the main xml, than change the text from our adapter:
String firstLetter = station.getTitle().substring(0, 1);
if (position == _alphaIndexer.get(firstLetter)) {
TextView header = (TextView) activity.findViewById(R.id.header_textview);
header.setText(firstLetter);
}

How to bind YES image for multiple selectable custom list

I'm trying to create an imagelist in which i can select muliple items, I have one yes icon that I want to set visible for whatever items i select(like checkbox) and invisible on unselect... I have added image into xml file and set its visibility as GONE(as it should not be visible initially).. Please tell me possible solutions..
for the list I have set list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
And my custom BaseAdapter class contains,
public static class ViewHolder{
public TextView text;
public ImageView image,yesimage;
public TextView quantity;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.text=(TextView)vi.findViewById(R.id.text);
holder.quantity=(TextView)vi.findViewById(R.id.quantity);
holder.yesimage=(ImageView)vi.findViewById(R.id.selectedyes);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
// Here I wanted to know how can i set visibility of yesimege for individual selected items on list
holder.text.setText(items[position]);
holder.image.setTag(data[position]);
holder.quantity.setText(Integer.toString(qty[position]).trim());
imageLoader.DisplayImage(data[position], activity, holder.image);
return vi;
}
xml snippet item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/stub"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15dip"
android:typeface="sans"
android:textColor="#000000"
android:layout_marginLeft="10dip"/>
<TextView
android:id="#+id/quantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right|center_vertical"
android:gravity="right"
android:paddingRight="10dp"
android:textSize="18dip"
android:typeface="sans"
android:textColor="#000000"/>
<ImageView android:id="#+id/selectedyes"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone"
android:src="#drawable/itemselected"
android:layout_marginRight="8dip"
android:soundEffectsEnabled="false" ></ImageView>
</LinearLayout>
You have to save/store that your item is selected or not.
After that check if the item is selected or not and use one of the following:
holder.image.setVisibility(View.GONE); // to make it disappear
or
holder.image.setVisibility(View.VISIBLE); // to display it
I don't know exactly what's in your data array, but you can save the state there.
if (data[position].isVisible()) {
holder.image.setVisibility(View.VISIBLE); // to display it
else
holder.image.setVisibility(View.GONE); // to make it disappear
You can catch click events with setting an OnItemClickListener on your ListView.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
// Put your custom code here
}
});
Does it answer your question?

Categories

Resources