OnClickListner not called on the whole View - android

On an Android application I am developing i got this situation.
In a Fragment there is a ListView that contains a list of customers (stored in instances of the Customer_InputOld class) using a customized ArrayAdapter (named ArrayAdapter_ClientiInput). The single row contains a ViewFlipper with two TextViews and seven nearly identical TextViews.
I put a ClickListner and a TouchListner in the Adapter, so on the click (and the touch) of a line the application make something. This works, except for the fact that these events are not triggered on the whole row.
Only the rough 80% of the row is "clickable", while the last textView does not respond to the listner.
Sincerely I don't know what code to post cause I have no idea where the problem could be: I tried to toast the View.getWidth() on the Click of the View itself, but actually the width is 1280 px, the entire device's width.
The single row is inflated with this customer_row.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="#fff"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".MyActivity">
<ViewFlipper
android:id="#+id/capitalViewFlipper"
android:layout_width="75dp"
android:layout_height="75dp">
<TextView
android:id="#+id/customerLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABCDEF"
android:gravity="center"
android:text="D"
android:textColor="#fff"
android:textSize="28pt" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBB"
android:gravity="center"
android:src="#drawable/ic_selected_done" />
</ViewFlipper>
<TextView
android:id="#+id/ragioneSocialeViewList"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/capitalViewFlipper"
android:gravity="center_vertical"
android:padding="10dip"
android:singleLine="true"
android:text="Dummy Customer"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/canaleViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignLeft="#id/ragioneSocialeViewList"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:gravity="center"
android:padding="10dip"
android:text="Dummy type"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="#+id/sollecitiViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/ragioneSocialeViewList"
android:gravity="center"
android:padding="10dip"
android:text="2"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/tipoViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_toRightOf="#+id/sollecitiViewList"
android:gravity="center"
android:padding="10dip"
android:text="F/S"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/emailViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/telefonoViewList"
android:layout_alignRight="#+id/telefonoViewList"
android:layout_alignTop="#+id/capitalViewFlipper"
android:gravity="center"
android:padding="10dip"
android:text="dummymail#gmail.com"
android:textAlignment="center"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/telefonoViewList"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:layout_toRightOf="#+id/tipoViewList"
android:gravity="center"
android:padding="10dip"
android:text="tel. 035424344"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/ultimoOrdineViewList"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/emailViewList"
android:capitalize="words"
android:gravity="center_vertical|right"
android:padding="10dip"
android:singleLine="true"
android:text="21/12/2012"
android:textColor="#000"
android:textSize="22dp" />
</RelativeLayout>
While the Adapter Code is this:
public class ArrayAdapter_ClientiInput extends ArrayAdapter<CustomerInputOld> {
public EventLog.Event ImageTouched;
//public ListView customerListView;
private ArrayList<CustomerInputOld> list;
private Context context;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public ArrayAdapter_ClientiInput(Context context, int textViewResourceId, ArrayList<CustomerInputOld> rowDataList) {
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<CustomerInputOld>();
this.list.addAll(rowDataList);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
//creating the ViewHolder we defined earlier.
ViewHolder_CustomerRow holder = new ViewHolder_CustomerRow();
//creating LayoutInflater for inflating the row layout.
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflater.inflate(R.layout.customers_row, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.ragioneSocialeViewList);
holder.capital = (TextView) convertView.findViewById(R.id.customerLogo);
holder.flipper = (ViewFlipper) convertView.findViewById(R.id.capitalViewFlipper);
//holder.line = (RelativeLayout) convertView.findViewById(R.id.customer_whole_row);
holder.canale = (TextView) convertView.findViewById(R.id.canaleViewList);
holder.solleciti = (TextView) convertView.findViewById(R.id.sollecitiViewList);
holder.email = (TextView) convertView.findViewById(R.id.emailViewList);
holder.tipo = (TextView) convertView.findViewById(R.id.tipoViewList);
holder.numero = (TextView) convertView.findViewById(R.id.telefonoViewList);
holder.ultimoOrdine = (TextView) convertView.findViewById(R.id.ultimoOrdineViewList);
//define an onClickListener for the CheckBox.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getContext(),Integer.toString(v.getWidth()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), CustomerDetail.class);
intent.putExtra("name", list.get(position).getCompany());
getContext().startActivity(intent);
}
});
convertView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == android.view.MotionEvent.ACTION_DOWN) {
v.setBackgroundColor(0xFAAA);
} else if ( e.getAction() == android.view.MotionEvent.ACTION_UP ||
e.getAction() == android.view.MotionEvent.ACTION_CANCEL) {
isSelected(position, v);
}
//Ritorno false per permettere anche al OnClickListener di agire..
return false;
}
});
holder.flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomerListFragment.onRowIconClicked(position, (RelativeLayout) v.getParent(), (ViewFlipper) v, context);
}
});
//setting data into the the ViewHolder.
holder.title.setText(list.get(position).getCompany());
holder.capital.setText(list.get(position).getCompany().substring(0, 1).toUpperCase());
holder.capital.setBackgroundColor(list.get(position).getIconColor());
holder.canale.setText(list.get(position).getChannel());
holder.solleciti.setText(list.get(position).getReminders());
if (Integer.parseInt(list.get(position).getReminders()) >= 3)
holder.solleciti.setTextColor(0xffff0000);
holder.email.setText(list.get(position).getEmail());
holder.numero.setText("tel. " + list.get(position).number);
holder.ultimoOrdine.setText(new SimpleDateFormat("c d MMM ''yy").format(list.get(position).getLastOrderDate()));
if (list.get(position).getInvoiceCustomer() == null) {
if (list.get(position).getDeliveryCustomer() == null) {
holder.tipo.setText("F/S");
} else {
holder.tipo.setText("F");
}
} else {
holder.tipo.setText("S");
}
//modifiche nel caso in cui sia selezionato
if ( isSelected(position,convertView) ) {
holder.flipper.showNext();
}
//return the row view.
return convertView;
}
private boolean isSelected(int position, View v){
if (position == CustomerListFragment.customerSelected) {
v.setBackgroundColor(0xFFE6C86F);
return true;
} else {
v.setBackgroundColor(0xFFFFFFFF);
return false;
}
}
}

Solution by OP.
In the last TextView I put android:capitalize="words", and that is an EditText's attribute, not Textview's! So when I clicked on the last "20%" of screen - coincidentally, that TextBox- tha app thinks that the click was on the TextView and not the line!

Related

why CardView causing slow down the scrolling process?

I was using listView without with the custom layout which is having
Linear layout as parent layout. Scrolling was fine, Then I need to
implement CardView for elevation and round corner, After implementation
list Scrolling become slow.Moreover it is also slowing down the
RecyclerView Process.
here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/WhiteSmoke"
android:layout_margin="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="12dp"
card_view:cardElevation="6dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/listviewback"
android:orientation="vertical"
android:padding="5dip">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/custom_listviewIV"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:src="#drawable/avatar2"
/>
<TextView
android:id="#+id/custom_listviewMainTv"
android:layout_width="180dip"
android:layout_height="45dip"
android:layout_marginLeft="2dip"
android:layout_toLeftOf="#+id/custom_listviewLinearLay"
android:layout_toRightOf="#+id/custom_listviewIV"
android:maxLines="2"
android:paddingTop="2dip"
android:text="Goverment "
android:textColor="#color/GreyDark"
android:textSize="15sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/custom_listviewLinearLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="2dip"
android:orientation="vertical">
<TextView
android:id="#+id/custom_listviewRating"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:background="#drawable/tv_round"
android:backgroundTint="#color/primary_dark"
android:gravity="center"
android:text="4.2/5"
android:textColor="#color/white"
android:textSize="13sp" />
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="5dip"
android:background="#color/GreyLight" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingTop="1dip"
android:paddingRight="10dip"
>
<TextView
android:id="#+id/custom_listviewFirstTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:text="Fsc Pre Engineering1"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewSecondTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering2"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewThirdTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering3"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewFourthTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="more+"
android:textColor="#color/black"
android:textSize="13sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
here is my adapter code
public class CustomAdaptor extends BaseAdapter {
private ArrayList<Data> myDataList;
private Context context;
LinearLayout linearLayout;
public CustomAdaptor(ArrayList<Data> mylist, Context context) {
this.myDataList = mylist;
this.context = context;
}
public int getCount() {
return myDataList.size();
}
public Object getItem(int position) {
return myDataList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.custom_listview, null);
linearLayout= view.findViewById(R.id.layout);
TextView collegeName = view.findViewById(R.id.custom_listviewMainTv);
TextView rating = view.findViewById(R.id.custom_listviewRating);
ImageView country = view.findViewById(R.id.custom_listviewIV);
LinearLayout ll= view.findViewById(R.id.custom_listviewLinearLay);
TextView firstTv= view.findViewById(R.id.custom_listviewFirstTv);
TextView secondTv= view.findViewById(R.id.custom_listviewSecondTv);
TextView thirdTv= view.findViewById(R.id.custom_listviewThirdTv);
TextView fourthTv= view.findViewById(R.id.custom_listviewFourthTv);
collegeName.setText(myDataList.get(position).getName());
rating.setText("" + myDataList.get(position).getRating());
Double n=0.0;
if (n==myDataList.get(position).getRating()) {
ll.setVisibility(View.GONE);
}
if(!myDataList.get(position).getCourseList().isEmpty()) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName().equals("")) {
firstTv.setVisibility(View.VISIBLE);
firstTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName());
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>1) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName().equals("")) {
secondTv.setVisibility(View.VISIBLE);
secondTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName());
}
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>2) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName().equals("")) {
thirdTv.setVisibility(View.VISIBLE);
thirdTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName());
}
}
if (myDataList.get(position).getCourseList().size()>1) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}else if(myDataList.get(position).getCourseList().get(0).lst.size()>3) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}
String img = "" + myDataList.get(position).getImage();
img = img.replaceAll(" ", "%20");
Glide.with(context).load(img).into(country);
view.setTag(myDataList.get(position).getId());
}
else {
linearLayout.setVisibility(View.GONE);
}
return view;
}
}
Cardview is probably just the "breaking point" for how much graphic content that cna be used in an unoptimized way without lagging. As mentioned in some comments you should be using a ViewHolder, here is a simple example of using a viewholder:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
you can read more about it here
Additionally you should be using RecyclerView and not ListView, to optimize performance.
Try to minimize your view hierarchy and reduce complex operations from binding.

Get "No results" Message Custom Adapter ListView Android

I am having a problem to set a "No results" message when getCount() returns 0.
I have a custom layout for the listview, and I simply want to get a "No Results" textview when there is no data on the listview.
Any idea how to achieve this?
I saw something with a empty id, but that needs to be on a listview, since this is a custom layout, it doesn't have that "tag".
Custom Adapter Code:
public class ListScheduleAdapter extends BaseAdapter {
Context context;
protected List<Schedule> listSchedules;
LayoutInflater inflater;
public ListScheduleAdapter(Context context, List<Schedule> listSchedules) {
this.listSchedules = listSchedules;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return listSchedules.size();
}
public Schedule getItem(int position) {
return listSchedules.get(position);
}
public long getItemId(int position) {
return listSchedules.get(position).getCod_Horario();
}
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.txtTeacher = (TextView) convertView
.findViewById(R.id.txt_teacher);
holder.txtDayofWeek = (TextView) convertView
.findViewById(R.id.txt_dayofweek);
holder.txtSubject = (TextView) convertView
.findViewById(R.id.txt_subject);
holder.txtTime = (TextView) convertView
.findViewById(R.id.txt_time);
holder.txtRoom = (TextView) convertView
.findViewById(R.id.txt_room);
holder.txtClass = (TextView) convertView
.findViewById(R.id.txt_class);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Schedule sche = listSchedules.get(position);
String fullTeacher = sche.getProfessor();
String[] names = fullTeacher.split(" ");
holder.txtTeacher.setText(names[0] + " " + names[names.length-1]);
holder.txtDayofWeek.setText(sche.getDia_Semana());
holder.txtSubject.setText(sche.getDisciplina());
holder.txtTime.setText(sche.getT_Entrada() + "h-" + sche.getT_Saida()+"h");
holder.txtRoom.setText(sche.getSala());
holder.txtClass.setText(sche.getTurma());
return convertView;
}
private class ViewHolder {
TextView txtTeacher;
TextView txtDayofWeek;
TextView txtSubject;
TextView txtTime;
TextView txtRoom;
TextView txtClass;
}
}
Fragment Code (method called on a async OnPostExecute method):
private void populateListView() {
ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList);
listviewHorarios.setAdapter(adapter);
}
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<LinearLayout
android:id="#+id/layout_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_teacher"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_dayofweek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row1"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_room"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_class"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
</RelativeLayout>
add
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
to the same layout where you declared your ListView (wrap both in a FrameLayout)
on the ListView's instance call
listView.setEmptyView(findViewById(R.id.empty));
when the adapter is empty, you will see the TextView apper
add TextView to your listview layout and setVisibility of it to GONE and check the adapter:
if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
}
hope this will help.

Row in listview not preserve the makeover and appears repeated

I have a list that when you click on the row on the right side the layout with the icon is changed by other. This makes it well but when I scrolling in the list, I notice there are rows with the same icon without do click. Also, if I scroll quickly these same icons are lost and are built differently. I think it's on the reuse of cells but I can not find the solution. What I want is that the image stays active only in the row where clicka.
Thanks!!
Adapter class:
public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Votante> listaVotantes;
private int rowLayout;
private View mConverView;
public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
super(context, resource,objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
this.listaVotantes = objects;
this.rowLayout = resource;
}
public int getCount(){
return this.listaVotantes.size();
}
public Votante getVotante(int position){
return this.listaVotantes.get(position);
}
private static class ViewHolder {
public TextView lblName;
public TextView lblLastName;
public TextView lblDni;
public TextView lblVoterNumber;
public RelativeLayout lytVoted;
public RelativeLayout lytCanVote;
public RelativeLayout lytUndo;
}
public View getView(int position, View converView, ViewGroup parent) {
final ViewHolder viewHolder;
mConverView = converView;
if (mConverView == null){
viewHolder = new ViewHolder();
this.mConverView = inflater.inflate(this.rowLayout, parent, false);
if (mConverView != null){
viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
mConverView.setTag(viewHolder);
}
}else{
viewHolder = (ViewHolder) mConverView.getTag();
}
Votante votante = getVotante(position);
viewHolder.lblName.setText(votante.getNombre());
viewHolder.lblLastName.setText(votante.getApellidos());
viewHolder.lblDni.setText(votante.getDni());
viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());
if (votante.getVotado()){
viewHolder.lytVoted.setVisibility(View.VISIBLE);
viewHolder.lytCanVote.setVisibility(View.GONE);
}else{
viewHolder.lytVoted.setVisibility(View.GONE);
viewHolder.lytCanVote.setVisibility(View.VISIBLE);
}
mConverView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.lytUndo.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
return mConverView;
}
}
Layout Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_voter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<RelativeLayout
android:id="#+id/lytVoterNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:background="#color/lightBlueItemList">
<TextView
android:id="#+id/lblNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="1999"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lytVoterData"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/lytCanVote"
android:layout_toRightOf="#+id/lytVoterNumber"
android:layout_toStartOf="#+id/lytCanVote"
android:background="#color/whiteItemList"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/lblVoterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Suarez Garcia de la Serna"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblVoterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="José Carlos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblDni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="44950962S"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RelativeLayout
android:id="#+id/lytCanVote"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/yellowItemList"
android:minHeight="30dp"
android:minWidth="30dp">
<ImageView
android:id="#+id/imgVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/flecha" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lytVoted"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/grrenAcceptList"
android:minHeight="30dp"
android:minWidth="30dp"
android:visibility="gone">
<ImageView
android:id="#+id/imgAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aceptar"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:minHeight="30dp"
android:minWidth="30dp"
android:background="#color/redD3"
android:id="#+id/lytUndo"
android:layout_alignParentRight="true"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgUndo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/undo"/>
</RelativeLayout>
</RelativeLayout>
You need to follow below Idea
1) this line of code mConverView = converView; doesn't makes sense. directly use converView(View from getView param). Remove mConvertView from the adapter.
2) Add some mechanism which can tell you which row is clicked and save that variable.
Example:- Have an boolean Array of size Rows.size . and set them in onClick.
Boolean[] array = new Boolean[getSize()];
in onClick that you already have in your getview set this.
public void onClick(View v) {
array[position] = !array[position];
viewHolder.lytUndo.setVisibility(View.VISIBLE);
//You do not need to call notifyDatasetChange.
3) in getView(), when you are setting data in to row items/or just before onClick. have a check like below.
if(array[position])// this will tell you if you need to show or hid lytUndo thing
viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it
else
viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want
You might need to set some params as final
I have written this as simple as possible, comment back with your result.

How to populate a ListView with objects of a custom layout in a custom layout for ListFragment?

I am using ListFragment which by default inflates a full screen ListView. I can successfully populate this ListView with objects of a custom layout. My problem is, how do I populate these custom objects that have custom layout in a non-default layout in ListFragment?
This is the layout that I would like to inflate and place the objects in (id: "article_List").
<RelativeLayout
android:id="#+id/footer_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" >
<Button
android:id="#+id/left_footer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Back" />
<Button
android:id="#+id/right_footer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/left_footer_button"
android:text="Next" />
</RelativeLayout>
<ListView
android:id="#+id/article_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/footer_button_layout" >
</ListView>
Layout for my objects that populates the ListView:
<TextView
android:id="#+id/journalTitle_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:maxLines="1"
android:text="journal title"
android:textSize="12sp" />
<TextView
android:id="#+id/pubYear_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/journalTitle_textView"
android:maxLines="1"
android:text="pubYear"
android:textSize="12sp" />
<TextView
android:id="#+id/volume_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/pubYear_textView"
android:maxLines="1"
android:text="volume"
android:textSize="12sp" />
<TextView
android:id="#+id/issue_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/volume_textView"
android:maxLines="1"
android:text="issue"
android:textSize="12sp" />
<TextView
android:id="#+id/pageInfo_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/issue_textView"
android:maxLines="1"
android:text="pageInfo"
android:textSize="12sp" />
<TextView
android:id="#+id/title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/journalTitle_textView"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:maxLines="2"
android:text="artile title"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/authorString_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title_textView"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:maxLines="1"
android:text="authors"
android:textSize="12sp" />
My custom ArrayAdapter class:
private class ArticleAdapter extends ArrayAdapter<Article> {
public ArticleAdapter(ArrayList<Article> entries) {
super(getActivity(), 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// View of single article in ListView
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.articlelistfragment, null);
}
// This is the magic method that gets the article at position
// "position"
Article en = getItem(position);
// if attribute value is null, then set textView visibility GONE.
// Does not take up any layout space
TextView journalTitleView = (TextView) convertView
.findViewById(R.id.journalTitle_textView);
if (en.getJournalTitle() != null) {
journalTitleView.setText(en.getJournalTitle());
} else {
journalTitleView.setVisibility(View.GONE);
}
TextView pubYearView = (TextView) convertView
.findViewById(R.id.pubYear_textView);
if (en.getPubYear() != null) {
pubYearView.setText(en.getPubYear());
} else {
pubYearView.setVisibility(View.GONE);
}
TextView volumeView = (TextView) convertView
.findViewById(R.id.volume_textView);
if (en.getVolume() != null) {
volumeView.setText("vol." + en.getVolume());
} else {
volumeView.setVisibility(View.GONE);
}
TextView issueView = (TextView) convertView
.findViewById(R.id.issue_textView);
if (en.getIssue() != null) {
issueView.setText("(" + en.getIssue() + ")");
} else {
issueView.setVisibility(View.GONE);
}
TextView pageInfoView = (TextView) convertView
.findViewById(R.id.pageInfo_textView);
if (en.getPageInfo() != null) {
pageInfoView.setText("pp." + en.getPageInfo());
} else {
pageInfoView.setVisibility(View.GONE);
}
TextView titleView = (TextView) convertView
.findViewById(R.id.title_textView);
titleView.setText(en.getTitle());
TextView authorStringView = (TextView) convertView
.findViewById(R.id.authorString_textView);
if (en.getAuthorString() != null) {
authorStringView.setText(en.getAuthorString());
} else {
authorStringView.setVisibility(View.GONE);
}
return convertView;
}
}
I would like to stick with ArrayAdapter. Using the default implementation of ListFragment to populate the ListView with my custom adapter I did the following (which worked):
#Override
protected void onPostExecute(ArrayList<Article> result) {
getActivity().setTitle(R.string.title_bar);
ArticleAdapter adapter = new ArticleAdapter(articles);
setListAdapter(adapter);
}

Android ArrayAdapter, BaseAdapter not calling getView()

I am extending ListFragment to display data retrieved from a web server in an AsyncTask in another class. If I set a custom height (i.e. 400dp) on the #android:id/list widget in my xml, getView() does not get called at all. If I set it to wrap_content, getView() gets called for the list items but the list items don't show up! It seems that the listview does not expand to fit the item heights, even though the list item's xml specifies a layout height. When a fixed height is defined I can see the colored background of the list, but no data. getCount() never returns 0.
Part of the adapter class:
public class gAdapter extends ArrayAdapter<GroupObject> {
ArrayList<GroupObject> groups;
public gAdapter(Context context, int textViewResId, ArrayList<GroupObject> groups) {
super(context, R.layout.groups_listview_row, groups);
mInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
con = context;
this.groups = groups;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
View row = convertView;
if(convertView == null)
row = mInflater.inflate(R.layout.groups_listview_row, parent, false);
TextView name = (TextView) row.findViewById(R.id.groups_listview_name);
//Set name
try {
name.setText( groups.get(position).groupName);
Log.d("test", "Got name " + groups.get(position).groupName);
} catch(Exception e) {
e.printStackTrace();
name.setText("Could not load name");
}
return row;
}
#Override
public int getCount() {
Log.d("test", "Returning count " + (this.groups.size() != 0 ? this.groups.size() : 0));
return this.groups.size() != 0 ? this.groups.size() : 0;
}
#Override
public GroupObject getItem(int position) {
Log.d("test", "get item " + position);
return this.groups.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
groups_listview_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/groups_listview_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/avatar" />
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/groups_listview_imageview"
android:layout_alignTop="#+id/groups_listview_imageview"
android:layout_toRightOf="#+id/groups_listview_imageview" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/groups_listview_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groups_listview_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="5dp"
android:gravity="right|bottom"
android:singleLine="true"
android:text="zCode: 000000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/LinearLayout1"
android:layout_gravity="bottom"
android:gravity="bottom" >
<TextView
android:id="#+id/groups_listview_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:gravity="left|bottom"
android:text="Location" />
<TextView
android:id="#+id/groups_listview_numMembers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginRight="10dp"
android:layout_weight="0.2"
android:gravity="right|bottom"
android:text="0 Members" />
</LinearLayout>
</RelativeLayout>

Categories

Resources