Custom layout for each row in a ListView - android

I am trying to get a button to work in a ListView Row. I been Reading about Custom Adapter but can't get it to work. Basically what i have a List_item class that have 4 TextView and 1 button which fieds a ListView in another class. one of the textViews has id/Name. so what i want is when i press the button on that row. it will get that Value of TextView "name" and add it to a ArrayList and if i press another button on different row it will add the Value of "name" to the same arraylist. so basically will add all the ones that i pressed on the button to an ArrayList. is that possible??.
JUST TO ADD. the textView Are being feed in by my database ..thanks upfront for any replies.
List_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<TextView
android:id="#+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="3"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:focusable="false"
android:minHeight="0dp"
android:minWidth="50dp"
android:text="+"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
</TableRow>
</RelativeLayout>
my menu.xml has
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="697dp" >
</ListView>
now i want to get Values of TextView " name " when i press the button on its row.
that is how i used to fill my listView before i add the button and it was working fine
ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description});
setListAdapter(adapter);
but now after i added the button it doesn't get any values so i reaserched and i found that i have to change my sampleAdapter to a custom Adapter. and add the button inside getView() but i can't get that hang of it.
my button OnClickListener
Button order = (Button) view.findViewById(R.id.order);
order.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
Log.d("Selected : ", name);
}
});

You will need to create a custom adapter class for your ListView. Since you are getting data from a database, I suggest that you extend CursorAdapter and override newView() and bindView(). From my experience, how to do this correctly is poorly documented, so I'll try to give some pointers here.
In newView(), you need to use a LayoutInflator which is provided to you by calling getLayoutInflator() on your ListActivity. After you call inflate() on this LayoutInflator, you can then get the Button for that specific row and set its OnClickListener.
In bindView(), you do just about all the same things, except that you don't need to inflate a new view since Android is requesting that you reuse an existing View object. You might want to create some extra methods in order to reduce the amount of repeated code.
I hope this helps even though it is explained in English rather than with a code example.

Related

How to get value from a TextView which is in a ListView without any custom adapters?

I have a ListView with a TextView (which has an integer as string) and a Seekbar.
I need to extract the integer value and set it to the Seekbar.
Unfortunately, when I do this, I get a NullPointer exception because the content that populates the ListView item is not recognized by the onCreateView of the PlaceholderFragment.
String progressValue = percentileTextView.getText().toString();
bar.setProgress(Integer.parseInt(progressValue));
I have an activity layout for this activity, then a fragment layout which contains the listview and then I have a content.xml where the actual TextView is present. My question is how to reference this TextView? Am I doing this right, i.e. Activity layout contains a ConstraintLayout with Toolbars etc, then the Fragment Layout contains the ListView and I am using a separate content.xml and applying that to the ListView via a SimpleAdapter.
newAdapter = new SimpleAdapter(getContext(), newList,
R.layout.content, new String[] {
"name",
"percent"
},
new int[] {
R.id.name1,
R.id.percentile1
});
I hope you understand my problem.
When I do the following, it works (i.e. Android Studio understand that is the TextView) - but throws a null pointer exception upon build.
TextView percentileTextView = (TextView) rootView.findViewById(R.id.percentile1);
UPDATE 1 for pskink:
Newlist contents:
[{TA=Item1, IA=46}, {TA=Item2, IA=98}, {TA=Item3, IA=70}, {TA=Item4, IA=54}, {TA=Item5, IA=99}, {TA=Item6, IA=98}]
Row item layout (content.xml):
<?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="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:textColor="#color/colorAccent" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="#+id/greater_than"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/greater_than" />
<TextView
android:id="#+id/percentile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/greater_than"
android:layout_toEndOf="#id/greater_than"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/percentile1"
android:layout_toEndOf="#id/percentile1"
android:text="#string/percentile_text"/>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar2"
android:paddingTop="12dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|bottom"
android:paddingTop="12dip"
android:orientation="horizontal">
<ImageView
android:contentDescription="info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/someimg" />
</LinearLayout>
</LinearLayout>

android - set text for spinner in <include />

I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!

Android: ListView & customAdapter - List when clicked showes info of 2 items / editText not clickable

I did a custom Adapter for my List View which extends a basicadapter,
it was working fine until i added another row to my list_item.xml
so now when in my OnItemClickListner, on click should show the description of the item in the row under it which was hidden. that works fine except that when i click on any item it shows the row under of description and another's of item in the bottom of the list, although i only pressed on one item and when i scroll down and up again the row of the item a clicked on disappears but the other one that opened by itself stays opened. i searched and found that it might be because i dont use bindView and any List takes from 5-10 items then reuses them. if that the case how can i implement a bindVIew to my customAdapter. ataached s my on click listener and getView of my customAdapter
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
if ((((LinearLayout) view.findViewById(R.id.edit)).getVisibility()) == (View.VISIBLE))
{
((LinearLayout) view.findViewById(R.id.edit)).setVisibility(View.GONE);
}else
{
((LinearLayout) view.findViewById(R.id.edit)).setVisibility(View.VISIBLE);
((EditText) view.findViewById(R.id.itemn)).setFocusable(true);
new LoadCategories().execute();
}
2nd problem: i have my description row has an editText which shows the description which is visible under the item when i press on any item. but i cant press on it to change it or edit it when it shows up.
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(l, null);
holder = new ViewHolder();
holder.id = (TextView)convertView.findViewById(R.id.id);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.price = (TextView)convertView.findViewById(R.id.price);
holder.description = (TextView)convertView.findViewById(R.id.description);
holder.button = (Button) convertView.findViewById(R.id.update);
holder.btn = (Button) convertView.findViewById(R.id.order);
holder.en = (EditText)convertView.findViewById(R.id.itemn);
holder.ep = (EditText)convertView.findViewById(R.id.pri);
holder.ed = (EditText)convertView.findViewById(R.id.descript);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.id.setText(getId(getItem(position)));
holder.name.setText(getName(getItem(position)));
holder.price.setText("£ " + getPrice(getItem(position)));
holder.description.setText(getDesc(getItem(position)));
holder.btn.setVisibility(View.GONE);
holder.en.setText((holder.name.getText()).toString());
holder.ep.setText((holder.price.getText()).toString());
holder.ed.setText((holder.description.getText()).toString());
return convertView;
}
public void clear(){
itemsList.clear();
notifyDataSetChanged();
}
}
here is my list_item.xml. Baically first tableRow shows all the info passed from an array of hashMap. 2nd row not used yet. Third TableRow takes the info from the first row and set it to the editText which then can be modified so afterwards it will be used to update the database with the corrected information. third row is hidden and is showed when an item is pressed.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<TextView
android:id="#+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="3"
android:gravity="left"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:focusable="false"
android:minHeight="0dp"
android:minWidth="50dp"
android:text="+"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_below="#id/tableRow2"
android:gravity="center_horizontal" >
<LinearLayout
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
<EditText
android:id="#+id/itemn"
android:layout_width="match_parent"
android:layout_height="69dp"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top|left"
android:hint="NAME"
android:inputType="text"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<EditText
android:id="#+id/descript"
android:layout_width="match_parent"
android:layout_height="82dp"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top|left"
android:hint="DESCRIPTION"
android:inputType="textMultiLine"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<EditText
android:id="#+id/pri"
android:layout_width="match_parent"
android:layout_height="62dp"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top|left"
android:hint="£ PRICE"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<Spinner
android:id="#+id/spinne"
style="#style/spinner_style"
android:layout_width="352dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/dropdown"
android:gravity="center"
android:overScrollMode="always"
android:prompt="#string/orders"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<Button
android:id="#+id/update"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.55"
android:focusable="false"
android:text="update"
android:textAllCaps="true" />
</LinearLayout>
</TableRow>
</RelativeLayout>
although i only pressed on one item and when i scroll down and up again the row of the item a clicked on disappears but the other one that opened by itself stays opened.
You are fighting the way that ListView's recycle Views, watch Google's Turbo-Charge your UI for a detailed explanation of how this works. The solution is simple: inside your Adapter you must remember which rows have been expanded and set them accordingly in getView() or collapse every row by default.
but i cant press on it to change it or edit it when it shows up.
I noticed that you are making an EditText focusable, try changing setFocusableInTouchMode() instead. These methods are linked in subtle ways.
Addition
One observation I have is that you have some unnecessary layouts, if a layout only has one child then you can probably remove it. For instance, you can either remove the TableLayout or the RelativeLayout and you can either remove LinearLayout edit or the TableRow.
i can't repress on the item to close or hide the expandable part after i am done with it.
I don't see a specific reason why you cannot touch anything in row one or two to hide the third row again... Perhaps you should add a Button that hides / shows this row/
what i face is that the expanded part that opens when i press on an item disappears when i scroll down
I walked another user through this: Android: ListView bug
In my answer there pay attention to how I use expanded to track whether each row should show or hide the extra layout, and notice how expanded is controlled in mName.

Button Inside Android ListView doesnt respond to clicks

I have a default ListView that i have added my custom views for the list items, but the buttons inside of these views are not clickable most of the time. I output a Log.v when the button receives a click event, but i have to tap the button almost a dozen times before it will register the click.
The other problem related tot his that i am having is that when the button is pressed i want an animation to happen revealing a menu sliding out from beneath it. At the moment i have tried several different methods like making a custom class for the views versus just using a layout inflater to get the relativeLayout object for the view, but nothing is working properly. I have even tried using listview.getAdapter().notifyDataSetChanged(); but that only has a pop-into-place for the extended view when i want an animation.
I have searched everywhere and it seems like the only possible solutions are to either rewrite my own custom listview or to use a linearlayout with a scrollview. The latter seems easier but i dont think it is nearly as optimized as the listview is.
Any suggestions would be greatly appreciated, if you need to see some code, please let me know...
Thanks!
UPDATE:
the getView() contains this:
Holder hold;
convertView = friends.get(position);
hold = new Holder();
hold.pos = position;
convertView.setTag(hold);
return convertView;
basically i pass an ArrayList<RelativeLayout>, at the moment, to the Adapter so that i dont have to create a new view each time and so that the animation will stay animated when i scroll down...
inside the OnCreate() for this activity i set that ArrayList<RelativeLayout> with this next code, but this is only temporary as i plan to use another method later, like an Async task or something so that this view contains some data...
RelativeLayout temp;
for(int i=0; i<30; i++){
temp = (RelativeLayout) inflater.inflate(R.layout.list_item, null);
final LinearLayout extraListItemInfo = (LinearLayout) temp.findViewById(R.id.extraListItemInfo);
Button infoBtn = (Button) temp.findViewById(R.id.infoBtn);
infoBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.v("ListItem", "Button has been clicked... ");
extraListItemInfo .setVisibility(View.VISIBLE);
ExpandAnimation closeAnim = new ExpandAnimation(extraListItemInfo , animHeight);
closeAnim.setDuration(750);
closeAnim.setFillAfter(true);
if(extraListItemInfo .getTag() == null || !extraListItemInfo .getTag().equals("expanded")){
extraListItemInfo .getLayoutParams().height = 0;
friendInfoList.startAnimation(closeAnim.expand());
extraListItemInfo .setTag("expanded");
}else if(extraListItemInfo .getTag().equals("expanded")){
extraListItemInfo .startAnimation(closeAnim.collapse());
extraListItemInfo .setTag("closed");
}
//((BaseAdapter) listview.getAdapter()).notifyDataSetChanged(); i tried it here once but then left it
//as the only action inside the listview's onitemclick()
}
});
listItems.add(temp);
}
this is the list item that i am using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/darkgrey"
android:paddingBottom="5dp" >
<LinearLayout
android:id="#+id/extraListItemInfo "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/listItemInfo"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-10dp"
android:background="#color/grey"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height"
android:layout_marginTop="5dp" >
<ImageView
android:id="#+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView04"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView03"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView02"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/imageView1"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_list_height">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:src="#drawable/logo_d" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ImageView01"
android:text="TextView"
android:textColor="#color/black"
android:textSize="17dp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/listItemInfo"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:background="#drawable/friend_cell_background2x"
android:clickable="true" >
<RelativeLayout
android:id="#+id/leftLayout"
android:layout_width="90dp"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imgCompany"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scaleType="centerInside"
android:src="#drawable/user2x" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/online_indicator2s" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/leftLayout"
android:background="#android:color/transparent"
android:gravity="left|center"
android:orientation="vertical"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/lblCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/lblReawrdDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Played Offer"
android:textColor="#color/white"
android:textSize="17dp" >
</TextView>
</LinearLayout>
<ImageView
android:id="#+id/imageView4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:src="#drawable/facebook_btn2x" />
<Button
android:id="#+id/infoBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView4"
android:background="#drawable/info_btn2x"
android:clickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="13dp"
android:layout_toLeftOf="#+id/infoBtn"
android:text="Follows 30+"
android:textColor="#color/white"
android:textSize="11dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/textView2"
android:background="#drawable/fan_btn2x"
android:text="Fans 30+"
android:textColor="#color/white"
android:textSize="11dp" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imageView4"
android:src="#drawable/google_btn2x" />
</RelativeLayout>
</RelativeLayout>
sorry for any layout problems that may make things difficult to read... but i hope this helps you guys to understand my problem... Thanks
UPDATE 2:
All of these answers have been helpful in some way, but i think the main issue that i must first fix is why the buttons do not receive click events until i have first scrolled away from that listItem, then back to it, then clicked the button again... If someone can help find a solution to THAT i think that everything else will be much easier to solve...
Thanks...
A screenshot as requested, but remember that this shot was taken on a samsung galaxy tab 10.1 and due to me using the same layout for this larger screen, it looks much different from what it does on the phone i usually test with (Motorola droid x that isnt rooted and cant take screenshots...)
Another Update:
I managed to get the clicking and animation working nicely by Extending ArrayAdapter instead of base adapter. Sadly i am still experiencing problems as only the bottom half of the list is clickable. The top half of the list still behaves as before with the very glitchy click events... Any ideas as to what is happening this time? Thanks...
Well this isn't really an answer, but after rewriting this a few times I managed to fix it so that it functions exactly the way I wanted.
This time I left all of the data separate from each view and had each list item be a custom class inheriting RelativeLayout and also implementing its own OnClickListener for its specific infoBtn.
My adapter now simply extends ArrayAdapter<> and overrides this getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView != null && (convertView instanceof FriendListItem2))
((FriendListItem2) convertView).setFriendInfo(friends.get(position));
else
convertView = new FriendListItem2(getContext(), friends.get(position));
return convertView;
}
Finally, in the main activity for this page I simply set the ListView with an adapter that I passed the data to.
This is all much cleaner than I had before and I wish it hadn't taken several rewrites of the code to get it right. Hopefully someone can benefit from this, though I still have no clue why I was getting a problem before.
Thanks for all previous suggestions.
try using this property in the top level layout in which your child views are placed.
android:descendantFocusability="blocksDescendants"
Its a bit tricky but I would suggest that if the above line does not work, try toggling between the removing of focusable=true and focusable="false" from the buttons. It should work.
Cheers!
That is a complex layout to create for every row...
Anyway, when you use the new keyword you are creating a different scope. In short your onClickListener does not see the 30 different extraListItemInfo references you expect it to see.
Try something like this:
#Override
public void onClick(View v) {
LinearLayout extraListItemInfo = v.getParent();
...
add android:onClick="onClick" to your button xml, it'll make it call the onClick() method
I had the same problem. Try taking the "android:clicable="true" " from the Relative Layout. When you do that the activity expects you to do make setOnClickListener to that RelativeLayout . It worked for me
you can fire click event on button from getView() method of ListViewAdapter Class.
See this question
Android : How to set onClick event for Button in List item of ListView.

How to remove row in ListView using a SimpleCursorAdapter

I have three different activities sharing exactly the same layout (a ListView and a TextView). The ListView uses this layout for its SimpleCursorAdapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="2dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/locationName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Länsnamn"
android:textAppearance="?android:attr/textAppearanceMedium">
</TextView>
<LinearLayout
android:id="#+id/num_stores_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numStores_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal butiker: ">
</TextView>
<TextView
android:id="#+id/numStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numItems_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal artiklar: ">
</TextView>
<TextView
android:id="#+id/numItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
</LinearLayout>
The program goes from activity #1, user picks an item in the list, activity #2 starts, user picks list, activity #3 starts.
Now, all the list items turn up fine just like they should, but for activity #3 I'd like to remove the entire row with id num_stores_row, and I seem to have a hard time doing this. I cannot do this in the setViewBinder since the row is not targeted by the adapter, and I cannot do it outside since the ListView is not activated until the data arrives. I've tried findViewById(R.id.num_stores_row).setVisibility(View.GONE); here and there, but the simulator crashes every time.
How do I remove it?
To add to what Mark said, override SimpleCursorAdapter's getView and put your findViewById(R.id.num_stores_row).setVisibility(View.GONE); in there once you inflate the row's view.
Do not include that row in the Cursor that you wrap in the SimpleCursorAdapter. Or, create your own subclass of SimpleCursorAdapter that overrides a bunch of methods (e.g., getCount()) to filter out that row.

Categories

Resources