I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
Related
I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}
I'm trying to create an AlertDialog that has many TextViews (so that scrolling in necessary) and an EditText at the end for users to enter a value that is not included in the list. When AlertDialog is first presented, things look good. However, when I scroll back to the top after scrolling to the bottom problems occur.
Here is the bottom of the list initially presented. All good.
But here is what it looks like after I scroll to the top. Not so good.
This is the Adapter class that I am using:
public class MultiSelectOtherDialog extends DialogFragment {
public static final String TAG = "MultiSelectOtherDialog";
private ArrayList<Bean> mList = new ArrayList<>();
public static MultiSelectOtherDialog newInstance(String aTitle, String[] aElems) {
MultiSelectOtherDialog frag = new MultiSelectOtherDialog();
Bundle args = new Bundle();
args.putString("title", aTitle);
args.putStringArray("elems", aElems);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// length of this list determines how many items to present - need an extra for the EditText
// at the end for "Other"
int mListLen = (getArguments().getStringArray("elems").length) + 1;
//int mListLen = (getArguments().getStringArray("elems").length);
for (int i = 0; i < mListLen; i++) {
mList.add(new Bean());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.scb_listview2, null);
ListView listViewItems = (ListView)view.findViewById(R.id.lvScb);
listViewItems.setAdapter(new MultiSelectOtherAdapter());
listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());
builder.setTitle(getArguments().getString("title")).setView(view);
AlertDialog diagFragDialog = builder.create();
return diagFragDialog;
}
public class MultiSelectOtherAdapter extends BaseAdapter {
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// needed for the "Other" EditText at the end
if (position < mList.size() - 1) {
convertView = View.inflate(getActivity(), R.layout.scb_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
Log.d(TAG, "1)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
Log.d(TAG, "2)");
}
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
Log.d(TAG, "3)");
}
final Bean bean = mList.get(position);
holder.cb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
bean.isChecked = isChecked;
}
});
// needed for the "Other" EditText at the end
if (position < getArguments().getStringArray("elems").length) {
String text = getArguments().getStringArray("elems")[position];
holder.tv.setText(text);
Log.d(TAG, "4)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
Log.d(TAG, "5)");
}
holder.cb.setChecked(bean.isChecked);
Log.d(TAG, "6)");
return convertView;
}
class ViewHolder {
SmoothCheckBox cb;
TextView tv;
}
}
public class OnItemClickListenerListViewItem implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bean bean = (Bean) parent.getAdapter().getItem(position);
bean.isChecked = !bean.isChecked;
SmoothCheckBox checkBox = (SmoothCheckBox) view.findViewById(R.id.scb);
checkBox.setChecked(bean.isChecked, true);
}
}
class Bean implements Serializable {
boolean isChecked;
}
}
And the layouts:
AlertDialog layout:
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvScb"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#0000"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_above="#+id/viewLineHoriz" />
<View
android:id="#+id/viewLineHoriz"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:layout_above="#+id/bottonRowScb"
android:background="?android:attr/dividerVertical" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:id="#+id/bottonRowScb"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:measureWithLargestChild="true"
android:paddingTop="0dip"
android:orientation="horizontal" >
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonOKScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/ok_string" />
<View
android:id="#+id/viewLineVert"
android:layout_height="fill_parent"
android:layout_width="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonCancelScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/cancel_string" />
</LinearLayout>
</RelativeLayout>
TextView item:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<TextView
android:id="#+id/tv"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp" />
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
And EditText item:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<EditText
android:id="#+id/et"
android:hint="#string/other_notes_string"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp"
android:textStyle="bold|italic"/>
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
In your adapter change this:
#Override
public long getItemId(int position) {
return position;
}
And, also in your getView() method, put this code:
converView = null;
before :
if(convertView == null){
// your codes
}
I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.
I do design. And i am not know how.
if I clik on the item, should appear red view in right side.
I am use GridView and my adapter public class BasketAdapter extends BaseAdapter
Item XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="142dp"
android:layout_height="wrap_content"
android:background="#eceff3"
android:paddingBottom="1dp">
<RelativeLayout
android:orientation="vertical"
android:layout_width="141.25dp"
android:layout_height="138.75dp"
android:background="#fff">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/goodImage"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_below="#+id/goodImage"
android:layout_marginTop="11dp"
android:id="#+id/linearLayout3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="28.75dp"
android:layout_marginRight="28.75dp">
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="bascetName"
android:id="#+id/bascetName"
android:textSize="12.50sp"
android:textStyle="bold"
android:textColor="#333333"
android:ellipsize="end"
android:singleLine="false"
android:gravity="center_horizontal" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$1.23"
android:id="#+id/bascetPrice"
android:textSize="13.75sp"
android:layout_gravity="center_horizontal"
android:textColor="#ffc600"
android:layout_below="#+id/linearLayout3"
android:layout_centerHorizontal="true"
android:layout_marginTop=" 8.75dp" />
<ImageView
android:layout_width="32dp"
android:layout_height="38.50dp"
android:id="#+id/countImage"
android:src="#drawable/count"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/basketQuantity"
android:textSize="12.50sp"
android:textStyle="bold"
android:layout_gravity="right"
android:autoText="false"
android:textColor="#ffffff"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>
</LinearLayout>
adapter
public class BasketAdapter extends BaseAdapter implements View.OnClickListener {
private List<ShoppingCart> items;
private AnimateFirstDisplayListener animateFirstDisplayListener = new AnimateFirstDisplayListener();
private DisplayImageOptions options = ILOptions.getOption();
private Typeface font;
public BasketAdapter(List<ShoppingCart> items) {
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ShoppingCart getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
font = Fonts.getBlockBertholdRegular(parent.getContext());
convertView = inflater.inflate(R.layout.basket_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.bascetName);
holder.price = (TextView) convertView.findViewById(R.id.bascetPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.goodImage);
holder.imageCount = (ImageView) convertView.findViewById(R.id.countImage);
holder.quantity = (TextView) convertView.findViewById(R.id.basketQuantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(items.get(position).getItem().getName().toUpperCase());
holder.name.setTypeface(font, Typeface.BOLD);
holder.price.setText("$" + items.get(position).getTotal());
holder.price.setTypeface(font, Typeface.NORMAL);
ImageLoader.getInstance().displayImage(items.get(position).getItem().getImage(), holder.imageView, options, animateFirstDisplayListener);
if (items.get(position).getItemCount() == 1) {
holder.imageCount.setVisibility(View.GONE);
holder.quantity.setVisibility(View.GONE);
} else {
holder.quantity.setText(String.valueOf(items.get(position).getItemCount()));
holder.quantity.setTypeface(font, Typeface.BOLD);
}
convertView.setOnClickListener(this);
return convertView;
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"cvcxvfdg",Toast.LENGTH_LONG).show();
}
private static class ViewHolder {
TextView name;
TextView price;
ImageView imageView;
ImageView imageCount;
TextView quantity;
}
}
I will add something else if necessary. may have a library ready?
Change your ImageView by click gridview item:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder holder = (ViewHolder)view.getTag();
//set your new image here
holder.imageview.setImageResource(R.id.yourImage);
... ...
//maybe also update items content in 'position' if need
}
});
Hope this help!
Good day to all.
I am currently working on a customized ListView which is not inflating (is not becoming visible) inside the activity.
Below are the xml and ListView adapter classes.
main.xml
<LinearLayout
android:id="#+id/llListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_below="#+id/rlFirstRow">
<ListView
android:id="#+id/lvErrorsReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"/>
</LinearLayout>
listview_row.xml
<?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="match_parent">
<LinearLayout
android:id="#+id/llError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txtErrorInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llError">
<TextView
android:id="#+id/txtStatus"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Status: "/>
<TextView
android:id="#+id/txtStatusInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llMaterials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llStatus">
<TextView
android:id="#+id/txtMaterials"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Materials: "/>
<TextView
android:id="#+id/txtMaterialsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llMaterials">
<Button
android:id="#+id/btnShowMaterials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Materials"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
BaseAdapter class
public class RepairReportListViewAdapter extends BaseAdapter
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;
public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
this.mContext = context;
this.errors = err;
this.statuses = stat;
this.materials = mat;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return 0;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.repairreport_listview, null);
holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.error.setText(this.errors.get(position).toString());
holder.status.setText(this.statuses.get(position).toString());
if(this.materials.get(position) == true)
{
holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
holder.showMaterials.setVisibility(Button.VISIBLE);
holder.showMaterials.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//LATER
}
});
}
else
{
holder.material.setText("No materials where used to fix this error.");
}
return convertView;
}
static class ViewHolder
{
TextView error;
TextView status;
TextView material;
Button showMaterials;
}
}
I've already tried changing the layouts as suggested here but the ListView is still not revealed.
Any ideas why it is not showing? Sorry for the code dump but I really cannot figure out what the heck is going on.
P.S. I did set the adapter to the ListView in the main activity.
adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
lvErrors.setAdapter(adapter);
your ListView does not inflate nothing because getCount is returning 0
change
#Override
public int getCount()
{
return 0;
}
with
#Override
public int getCount()
{
return (stat == null) ? 0 : stat.size();
}
Also, instead of keeping three differents ArrayList, you can create a class that holds all the info you need and an ArrayList of this class