Increment the value of TextView of child of particular Parent - android

This is the getChildView inside the Adapter class...
public View getChildView(int parent, int child, boolean lastChild, View view, ViewGroup viewGroup) {
ArrayList<String> itemDetails= (ArrayList<String>)getChild(parent,child);
if(view==null)
{
LayoutInflater inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.child_layout,viewGroup,false);
}
TextView textView =(TextView)view.findViewById(R.id.textView2);
textView.setText(itemDetails.get(0));
TextView textView1(TextView)view.findViewById(R.id.itemdescription);
textView1.setText(itemDetails.get(1));
TextView textView3= (TextView)view.findViewById(R.id.price);
textView3.setText(itemDetails.get(2));
final TextView textView2 = (TextView)view.findViewById(R.id.counterTextView);
ImageButton add = (ImageButton)view.findViewById(R.id.imageButton);
final ImageButton remove = (ImageButton)view.findViewById(R.id.imageButton1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString())+1));
remove.setVisibility(View.VISIBLE);
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Integer.parseInt(textView2.getText().toString())==0)
{
remove.setVisibility(View.INVISIBLE);
}
else {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString()) - 1));
}
}
});
return view;
}
This is the ChildLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/k_meals_rs"
android:id="#+id/imageView6" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_remove_black"
android:id="#+id/imageButton1"
android:background="#drawable/round_button"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#000000"
android:id="#+id/counterTextView" />
<ImageButton
app:srcCompat="#drawable/ic_add_black"
android:id="#+id/imageButton"
android:background="#drawable/round_button"
android:layout_width="29dp"
android:layout_height="29dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp">
<TextView
android:id="#+id/itemdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxEms="15"
android:textColor="#000000"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="₹150"
android:textColor="#000000"/>
</RelativeLayout>
</LinearLayout>
i want to increment & decrements the value of counterTextView as the the + and - buttons are pressed.. but as i do so it increment and decrements the value of counterTextView in other child in other parents with same index value...any idea how to overcome this problem.. thnx

Related

ExpandableRecyclerview Inside a Cardview Recyclerview

I am totally familiar with ExpandableRecyclerview Or ExpandableLayout But I have a design that is a little bit unique and I am seeking help to achieve this implementation.
I Am Attaching the design
When the user clicks the arrow shown in the image the CardView Recyclerview should expand or inflate a recyclerview in between comment button and response section. How to do that. I have tried the ExpandableRecyclerview from ThoughtBot and ExpandableListView all this components add a child to parent.
I am using ExpandableRecyclerview from Thoughtbot,
com.thoughtbot.expandablerecyclerview
My Adapter Looks like as below.
public class ReviewRecyclerAdapter extends ExpandableRecyclerViewAdapter<ReviewViewHolder, CommentsViewHolder> {
private Context context;
public ReviewRecyclerAdapter(Context context,List<? extends ExpandableGroup> groups) {
super(groups);
this.context = context;
}
#Override
public ReviewViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_review, parent, false);
return new ReviewViewHolder(view);
}
#Override
public CommentsViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_comments, parent, false);
return new CommentsViewHolder(view);
}
#Override
public void onBindChildViewHolder(CommentsViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
final CommentsModel comments = ((ReviewsModel) group).getItems().get(childIndex);
holder.setSubTitletName(comments.getComment());
}
#Override
public void onBindGroupViewHolder(ReviewViewHolder holder, int flatPosition, ExpandableGroup group) {
holder.setGenreTitle(context, group);
}
}
// View Holders
public class ReviewViewHolder extends GroupViewHolder {
private TextView titleName;
private ImageView arrow;
private ImageView icon;
public ReviewViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.list_item_genre_name);
arrow = (ImageView) itemView.findViewById(R.id.list_item_genre_arrow);
}
public void setGenreTitle(Context context, ExpandableGroup title) {
if (title instanceof ReviewsModel) {
titleName.setText(title.getTitle());
if (((ReviewsModel) title).getReview()!= null && !((ReviewsModel) title).getReview().isEmpty()){
}
}
}
#Override
public void expand() {
animateExpand();
}
#Override
public void collapse() {
animateCollapse();
}
private void animateExpand() {
RotateAnimation rotate =
new RotateAnimation(360, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
private void animateCollapse() {
RotateAnimation rotate =
new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
arrow.setAnimation(rotate);
}
}
public class CommentsViewHolder extends ChildViewHolder {
private TextView subTitleTextView;
public CommentsViewHolder(View itemView) {
super(itemView);
subTitleTextView = (TextView) itemView.findViewById(R.id.subtitle);
}
public void setSubTitletName(String name) {
subTitleTextView.setText(name);
}
}
//Review Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="24dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/review_details"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_avatar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:src="#drawable/profile_placeholder" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/list_avatar">
<TextView
android:id="#+id/list_item_genre_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" Guru natha"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxEms="15"
android:text=" From Mg Road"
android:layout_alignStart="#+id/list_item_genre_name"
android:layout_marginStart="10dp"
android:layout_below="#+id/list_item_genre_name" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text=" 3 Days ago"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/days"
android:layout_below="#+id/days"
android:maxEms="15"
android:text=" 448 Views"
android:textSize="8sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/review_details"
android:layout_margin="20dp">
<TextView
android:id="#+id/review"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Review"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:text="Rating"
android:textStyle="bold" />
<View
android:id="#+id/lineytf"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/review"
android:background="#color/black" />
<TextView
android:layout_marginTop="10dp"
android:id="#+id/review_conetent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/lineytf"
android:layout_below="#+id/lineytf"
android:maxLines="3"
android:maxEms="15"
android:text="#string/lorem_ipsum" />
</RelativeLayout>
<RelativeLayout
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/content">
<TextView
android:id="#+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:maxEms="15"
android:text="Response (2)"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:maxEms="15"
android:layout_above="#+id/line2"
android:src="#drawable/white_r_arrow"
android:text="arrow"
android:textStyle="bold"
android:id="#+id/list_item_genre_arrow" />
<View
android:id="#+id/line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/response"
android:background="#color/black" />
<!--Add expandable views-->
<Button
android:id="#+id/commentsbutton"
android:textSize="12sp"
android:textAllCaps="false"
android:text="Comments"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/line2"
android:layout_toStartOf="#+id/button2" />
<Button
android:textSize="12sp"
android:textAllCaps="false"
android:text="56"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/list_item_genre_arrow"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
//Comment Layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="#+id/list_item_genre_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="70dp"
android:layout_alignParentRight="true"
android:src="#drawable/android" />
<TextView
android:id="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:gravity="center_vertical"
tools:text="SubTitle" />
</FrameLayout>
Current Screenshot :
Please do shoot the closest solution if you have.

ListView Items changed automatically in Fragment android

when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);

Android custom ListView item setOnItemClickListener not working

I have a custom ListView item. setOnClickListener is not working. I've been searching for the solution for hours, but couldn't find anything. I think the problem is not 'set focusable false' things, because I've already done it. Please, help me.
payments_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:focusable="false"
android:focusableInTouchMode="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:paddingLeft="10dp">
<ImageView
android:layout_width="46dp"
android:layout_height="35.3dp"
android:textColor="#000000"
android:id="#+id/payment_item_name2"
android:layout_gravity="center_horizontal|start"
android:src="#drawable/payments_internet"
android:textSize="20sp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8c8c8c"
android:id="#+id/payment_item_name"
android:layout_gravity="center_horizontal|center"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:layout_marginTop="12dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:src="#drawable/arrow_right_red"
android:layout_gravity="center_vertical|right"
android:id="#+id/backButton"
android:textAllCaps = "true"
/>
</LinearLayout>
Here is my Activity onCreate:
ListView paymentsList = (ListView) findViewById(R.id.paymentsList);
paymentsList.setAdapter(testAdapter);
paymentsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Clicked23", "CLicked23");
}
});
`
My Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View payments_list_item = inflater.inflate(R.layout.payments_list_item, parent, false);
String payment = getItem(position);
TextView payment_item_name = (TextView) payments_list_item.findViewById(R.id.payment_item_name);
payment_item_name.setText(payment.toUpperCase());
return payments_list_item;
}
//EDIT
Finally solved my problem
Thanks to #mustafasevgi`s answer:
I added
android:descendantFocusability="blocksDescendants" to my custom ListView item layout.
Can you add this code?
android:descendantFocusability="blocksDescendants"
payment_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="12dp"
android:paddingLeft="10dp">
<ImageView
android:layout_width="46dp"
android:layout_height="35.3dp"
android:textColor="#000000"
android:id="#+id/payment_item_name2"
android:layout_gravity="center_horizontal|start"
android:src="#drawable/payments_internet"
android:textSize="20sp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8c8c8c"
android:id="#+id/payment_item_name"
android:layout_gravity="center_horizontal|center"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:text="Test"
android:textStyle="bold"
android:capitalize="words" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:layout_marginTop="12dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:src="#drawable/arrow_right_red"
android:layout_gravity="center_vertical|right"
android:id="#+id/backButton"
android:textAllCaps = "true"
/>
</LinearLayout>
check the following code:
public class CustomListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView paymentsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list);//your layout view
paymentsList = (ListView) findViewById(R.id.paymentsList);
paymentsList.setAdapter(testAdapter);
paymentsList.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//your logic
Toast.makeText(this, "Listview Clicked", Toast.LENGTH_SHORT).show();
}
}
Try this:
paymentsList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.w("Clicked23", "CLicked23");
}
});
Remove this from your xml
android:focusable="false"
android:focusableInTouchMode="false"
how about modifying your payments_list_item to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<ImageView
android:id="#+id/payment_item_name2"
android:layout_width="46dp"
android:layout_height="35.3dp"
android:src="#drawable/payments_internet"/>
<TextView
android:id="#+id/payment_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Test"
android:textColor="#8c8c8c"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/backButton"
android:layout_width="15.3dp"
android:layout_height="20.1dp"
android:layout_gravity="center_vertical|right"
android:src="#drawable/arrow_right_red"/>
</LinearLayout>
you have written very complex layout..

Listview displays only one row from the list

I am new to android listviews and I am trying to populate a listview in android by a list of items coming from a webservice. I know that the list contains more than one record coming from the webservice but my custom listview displays only the first record from the list. I checked the size of the list and there is always more than one records but the listview is showing only one of them. My custom adapter is like following:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<LItem> lstItems;
ListAdapter(Context context, ArrayList<LItem> objects) {
ctx = context;
lstItems = objects;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return lstItems.size();
}
#Override
public Object getItem(int position) {
return lstItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
LItem p = getProduct(position);
((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
((TextView) view.findViewById(R.id.tvBQuantity )).setText(p.getBQ() );
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
LItem getProduct(int position)
{
return ((LItem) getItem(position));
}
ArrayList<LItem> getBox() {
ArrayList<LItem> box = new ArrayList<LItem>();
for (LItem p : lstItems) {
// if (p.selected)
// box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//getProduct((Integer) buttonView.getTag())= isChecked;
}
};
}
I am binding the listview as:
protected void onPostExecute(List<LItem> li ) {
super.onPostExecute(lstItm);
if(lstItm.size()>0) {
Adp=new ListAdapter(myactivity,lstItm);
lvTasks.setAdapter(Adp);
Log.d("Size---------",Integer.toString(lstItm.size()) );//here it writes more than one as size of the list.
}
}
My xml file for displaying lists is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#FFF"
android:button="#drawable/custom_checkbox_design"
android:focusableInTouchMode="false"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task:"
android:id="#+id/textView2"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task"
android:id="#+id/tvTask"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material:"
android:id="#+id/textView1"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="material"
android:id="#+id/tvMaterial"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balanced Quantity:"
android:id="#+id/textView14"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bquantity"
android:id="#+id/tvBQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Balanced Quantity:"
android:id="#+id/textView25"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tvAQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
</LinearLayout>
My xml for listview is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Issue was with the scrollview which was createing the issue. I just removed
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</ScrollView>
and it worked for me :)

Catch onClick event in a listView, not onItemClick

Here is my code 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"
android:background="#ff0000"
>
<ListView
android:id="#+id/parkList"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="5.0dp"
android:textIsSelectable="false">
</ListView>
</LinearLayout>
And here my custom layout for items of listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/smooth_rectangle"
>
<TableRow
android:layout_height="70dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp"
android:weightSum="3"
>
<android.widget.ImageView
android:id="#+id/photo1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="left"
android:src="#drawable/ic_launcher"
android:layout_marginRight="3dp"/>
<android.widget.ImageView
android:id="#+id/photo1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="center_horizontal"
android:layout_marginRight="3dp"
android:src="#drawable/ic_launcher"/>
<android.widget.ImageView
android:id="#+id/photo1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:gravity="right"
android:src="#drawable/ic_launcher"/>
</TableRow>
<TextView
android:id="#+id/parkname"
android:layout_marginTop="3dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Parco"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:textColor="#111111"/>
<TextView
android:id="#+id/location"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Acquasparta via Tiberina 48"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:textColor="#111111"/>
<TextView
android:id="#+id/last_opinion"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Questo parco e' molto bello"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:textColor="#111111"/>
<LinearLayout
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/dirty"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="pulizia"
android:textColor="#111111"/>
<TextView
android:id="#+id/noisy"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="silenziosità"
android:textColor="#111111"/>
<TextView
android:id="#+id/green"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="verde"
android:textColor="#111111"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/canjog"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="can jogging"
android:textColor="#111111"/>
<TextView
android:id="#+id/withbar"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="there is a bar"
android:textColor="#111111"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/comment"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Live Comment"
android:textColor="#111111"
android:background="#ddddff"
android:clickable="true"/>
<View
android:layout_height="fill_parent"
android:layout_width="0.5dp"
android:background="#333333"
/>
<TextView
android:id="#+id/gotomaps"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Naviga"
android:textColor="#111111"
android:background="#ddddff"
android:clickable="true"/>
<View
android:layout_height="fill_parent"
android:layout_width="0.5dp"
android:background="#333333"
/>
<TextView
android:id="#+id/signal"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Segnala"
android:textColor="#111111"
android:background="#ddddff"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
Here my arrayadapter
public class ParkListAdapter extends ArrayAdapter<Park> {
private NearMeFragment fragment;
public ParkListAdapter(Context context, int resourceId, Park[] objects, NearMeFragment listner) {
super(context, resourceId, objects);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.small_park_container, null);
Park park = getItem(position);
((TextView) convertView.findViewById(R.id.parkname)).setText(park.getParkName());
((TextView) convertView.findViewById(R.id.location)).setText(park.getLocation());
((TextView) convertView.findViewById(R.id.last_opinion)).setText(park.getLastComment());
((TextView) convertView.findViewById(R.id.green)).setText(String.valueOf(park.getGreen()));
((TextView) convertView.findViewById(R.id.noisy)).setText(String.valueOf(park.getNoisy()));
((TextView) convertView.findViewById(R.id.dirty)).setText(String.valueOf(park.getDirty()));
((TextView) convertView.findViewById(R.id.canjog)).setText(String.valueOf(park.isJogging()));
((TextView) convertView.findViewById(R.id.withbar)).setText(String.valueOf(park.isBar()));
TextView interactive = (TextView) convertView.findViewById(R.id.signal);
interactive.setTag(String.valueOf(park.getParkID()));
interactive.setOnClickListener(fragment);
interactive = (TextView) convertView.findViewById(R.id.gotomaps);
interactive.setTag(String.valueOf(park.getLatitude()) + "!" + String.valueOf(park.getLongitude()));
interactive.setOnClickListener(fragment);
interactive = (TextView) convertView.findViewById(R.id.comment);
interactive.setTag(String.valueOf(park.getParkID()));
interactive.setOnClickListener(fragment);
return convertView;
}
}
And here my NearMeFragment:
public class NearMeFragment extends Fragment implements View.OnClickListener{
private FragmentManager fragmentManager;
#Override
public void onCreate(Bundle savedBundle){
super.onCreate(savedBundle);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
View v = inflater.inflate(R.layout.park_list, container, false);
View header = inflater.inflate(R.layout.sort_by_header, null);
//TODO remove
make_test(v, header);
return v;
}
public void openTest(View view){
new LiveComment().show(getChildFragmentManager(), "dialog");
}
#Override
public void onClick(View view) {
Log.i("clickevent",view.toString());
}
private void make_test(View v, View header) {
ListView listView = (ListView) v.findViewById(R.id.parkList);
listView.setFocusable(false);
listView.setFocusableInTouchMode(false);
listView.setClickable(false);
listView.setItemsCanFocus(true);
Park[] list = new Park[3];
list[0] = new Park("parco a", "acquasparta", "bello", false, false, 1, 2, 3, 1,2,3);
list[1]=new Park("parco b", "perugia", "bello", false, false, 1, 2, 3, 1, 2, 3);
list[2]=new Park("parco b", "perugia", "bello", false, false, 1, 2, 3, 1, 2, 3);
ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list, this);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
}
What I want to di is catch the onClick events fired on three TextView in my custom layout, and not onItemClick. The matter is that, also if I have been searching several answers, no one helped me. Events are not catched. What am I missing?
In your ParkListAdapter constructor ParkListAdapter() you are passing the listener NearMeFragment listner but you are not using it anywhere in the code. Your NearMeFragment fragment object in the adapter should be equal to the listener. So just do something like this in the constructor and check
fragment = listner;

Categories

Resources