Android ListView item clicked not working - android

I don't understand what's going wrong, when clicking an item on the list nothing appends.
my environment is minsdk:21, maxsdk:27, java 8 (openjdk), android-studio 3.1.4, terminal android 6.0
note : using FragmentManager or SupportFragmentManager provides the same result.
the fragment list layout
<?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"
android:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list"
android:dividerHeight="1sp"
android:divider="#color/blue_serenity_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List row layout
<?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="horizontal"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
android:background="#color/blue_serenity_transparent"
android:columnCount="3"
android:rowCount="2"
>
<ImageView
android:id="#+id/notif_type"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_column="1"
android:layout_row="0"
android:textSize="18dp"
android:textColor="#color/black"
/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="1"
/>
</GridLayout>
</LinearLayout>
list setup
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<NotificationBean> data = new NotificationBean().list(getActivity().getBaseContext());
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
list = (ListView) v.findViewById(R.id.list);
listAdapter = new Adapter(getActivity().getBaseContext(), data);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "row clicked");
}
});
return v;
}
List adapter
class NotificationsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater = null;
private List<NotificationBean> notifications;
public NotificationsAdapter(Context context, List<NotificationBean> notifications){
this.context = context;
this.notifications = notifications;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return notifications.size();
}
#Override
public Object getItem(int position) {
return notifications.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.fragment_notifications_row, null);
NotificationBean n = notifications.get(position);
GridLayout layout = (GridLayout) v.findViewById(R.id.row_layout);
if (dark) layout.setBackgroundColor(getResources().getColor(R.color.blue_serenity_transparent));
else layout.setBackgroundColor(getResources().getColor(R.color.white));
dark = !dark;
ImageView notifType = (ImageView) v.findViewById(R.id.notif_type);
notifType.setImageResource(n.getIcon());
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(n.getTitle());
TextView date = (TextView) v.findViewById(R.id.date);
date.setText(Constants.DTF.format(n.getDate()));
return v;
}
}

Related

Android Custom Spinner unable to select an option and show it

I have a problem with spinner it do not let me select one item. I tried a lot of things and that still not working.
The picture shows that the spinner is in blank when the activity load
When I clicked the arrow it shows the items
but when I choose one, nothing happends.
<?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"
tools:context=".Activities.Inspeccion.DatosGeneralesActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
>
<TextView
android:id="#+id/tvSubestacionTitulo"
android:layout_below="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSubestacion"
android:textSize="18sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tvSubestacionDato"
android:layout_below="#+id/tvSubestacionTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Prueba"
/>
<Spinner
android:id="#+id/spinnerSubEstacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvSubestacionDato"
>
</Spinner>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the Layout of the activity.
<?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="horizontal">
<TextView
android:id="#+id/tvNumeroOpcion"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="1"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/tvDescriptionOption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:text="Guatemala"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
That is the custom layout for the spinner
Public class ComboAdapter extends BaseAdapter{
private List<Combo> combos;
private Activity activity;
private LayoutInflater inflater;
public ComboAdapter(List<Combo> combos, Activity activity) {
this.combos = combos;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return combos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, null);
TextView tvId = (TextView) view.findViewById(R.id.tvNumeroOpcion);
TextView tvDescripcion = (TextView) view.findViewById(R.id.tvDescriptionOption);
tvId.setText(combos.get(position).getId());
tvDescripcion.setText(combos.get(position).getDescripcion());
}
return view;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getView(position, convertView,parent);
}
}
That is my Adapter
And below is my activity.
public class DatosGeneralesActivity extends AppCompatActivity {
private TextView tvSubestacionDato;
private List<Combo> listaCombo;
private Spinner spinnerSubestacion;
private ArrayAdapter<Combo> adapterSubestacion;
String seleccion;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_generales);
//Inicializando textos
tvSubestacionDato = (TextView) findViewById(R.id.tvSubestacionDato);
//Inicializanco listas
listaCombo = new ArrayList<>();
//Inivializando spinners
spinnerSubestacion = (Spinner) findViewById(R.id.spinnerSubEstacion);
AppService service = API.getCombos().create(AppService.class);
Call<List<Combo>> subestacionCall = service.getSubestacion();
subestacionCall.enqueue(new Callback<List<Combo>>() {
#Override
public void onResponse(Call<List<Combo>> call, Response<List<Combo>> response) {
listaCombo.clear();
listaCombo.addAll(response.body());
}
#Override
public void onFailure(Call<List<Combo>> call, Throwable t) {
}
});
//final ComboAdapter adapter = new ComboAdapter(listaCombo, DatosGeneralesActivity.this);
final ArrayAdapter<Combo> adapter = new ArrayAdapter<Combo>(this, R.layout.support_simple_spinner_dropdown_item, listaCombo);
spinnerSubestacion.setAdapter(adapter);
spinnerSubestacion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
Toast.makeText(DatosGeneralesActivity.this, ""+position, Toast.LENGTH_SHORT).show();
tvSubestacionDato.setText(listaCombo.get(position).getDescripcion());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this changes:
Call adapter like:
ComboAdapter adapter = new ComboAdapter(DatosGeneralesActivity.this,
R.layout.combo_list_item, R.id.tvDescriptionOption, listaCombo);
now in adapter class:
public ComboAdapter(Activity context,int resouceId, int textviewId, List<Combo> list){
super(context,resouceId,textviewId, list);
this.combos = list;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Also inside your getView() method inflate layout like:
if (convertView == null){
view = inflater.inflate(R.layout.combo_list_item, parent , false);

How customize spinner in Android

I need to customize spinner in Android.
I have my simple Adapter:
public class MonthsSpinnerAdapter extends BaseAdapter {
private Activity context;
private String [] itemList;
public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
this.context=context;
this.itemList=monthArray;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return itemList[position];
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent,
false);
RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
make.setText(itemList[position]);
Log.d("SpinnerA", "getView");
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
false);
RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
month.setText(itemList[position]);
RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
year.setText(context.getString(R.string.year));
Log.d("SpinnerA", "getDropDownView");
return row;
}
}
But when I added it to my spinner and run the up, I don't see anything in spinner.
MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
, getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);
I think it will be work nice, but I dont know where I have mistake. This is example what I see, when run app:
And at this picture, what I need:
And this my xml files if you need:
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:typeface="roboto_medium"
android:padding="5dp"
android:textColor="#fff"
android:id="#+id/spinner_title"
android:text="month"
android:background="#color/primary_color"
xmlns:android="http://schemas.android.com/apk/res/android" />
And DropDown layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_medium"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:id="#+id/spinner_month"
android:text="month"
android:textColor="#545454"
android:background="#fafafa" />
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_regular"
android:layout_marginTop="16dp"
android:layout_marginRight="12dp"
android:layout_alignParentRight="true"
android:text="year"
android:id="#+id/spinner_year"
/>
</RelativeLayout>
Help me please with my problem
Try to return itemList.length in getCount function of your adapter.
#Override
public int getCount() {
return itemList.length;
}

Unconditional layout inflation from view adapter

I'm getting a warning like Unconditional layout inflation which is causing the layout to exit and app goes crashing.I need to inflate this layout without crashing.
the crashing is on the following code.
I have the code for the custom adapter set.
public class CustomAdapter extends BaseAdapter {
private List<Response.NodesEntity> mMovieitem;
private Context mContext;
private LayoutInflater inflater;
public CustomAdapter(Context mContext, List<Response.NodesEntity> mMovieitem) {
this.mContext = mContext;
this.mMovieitem = mMovieitem;
}
#Override
public int getCount() {
return mMovieitem.size();
}
#Override
public Object getItem(int position) {
return mMovieitem.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.s, parent, false);
Response.NodesEntity item = (Response.NodesEntity) getItem(position);
ImageView thumbnail = (ImageView) rowView.findViewById(R.id.thumbnail);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView rating = (TextView) rowView.findViewById(R.id.video_url);
String imageUrl = item.getVideoTumbnail().getSrc();
Picasso.with(mContext).load(imageUrl).into(thumbnail);
title.setText(item.getTitle());
rating.setText(item.getHlsVideo());
return rowView;
}
}
The Xml is in this format . I'm inflating this layout and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/video_list"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="info.androidhive.materialdesign.activity.FriendsFragment">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_material_light"
android:scaleType="centerCrop" />
<TextView
android:layout_marginBottom="10dp"
android:layout_gravity="bottom"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/thumbnail"
android:layout_toEndOf="#+id/thumbnail"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:typeface="sans"
android:textSize="22sp"
android:textColor="#ffffff" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/video_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_toEndOf="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:visibility="invisible"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
use View holder method for inflating each rows

Can't click on element in ListView Android

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.

Gridview numbering

I have this gridview. is it possible to have numbers on the side of gridview like in excel 1,2,3 or a, b, c ,d
my 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="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</RelativeLayout>
something i want to achieve
You'll have to add numbering to each of the cells and only display it when it is the leftmost cell.
Cell layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/row_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- The rest of your layout -->
</LinearLayout>
</LinearLayout>
In your Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.grid_cell);
TextView rowNumber = (TextView) v.findViewById(R.id.row_number);
if (position % colNum == 0) { // colNum: number of columns
int row = position / colNum + 1; // get the row number
rowNumber.setText(String.valueOf(row));
rowNumber.setVisibility(View.VISIBLE);
} else {
rowNumber.setVisibility(View.GONE);
}
return v;
}
Note: For the sake of simplicity I am not using the ViewHolder pattern. But you should!
// try this way hope this will help you...
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/txtGridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<ImageView
android:id="#+id/imgGridItem"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private GridView gridview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
ArrayList<String> images = new ArrayList<String>();
images.add("image1path");
images.add("image2path");
images.add("image3path");
images.add("image4path");
images.add("image5path");
images.add("image6path");
images.add("image7path");
images.add("image8path");
images.add("image9path");
images.add("image10path");
images.add("image11path");
images.add("image12path");
gridview.setAdapter(new GridAdapter(images,this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,String.valueOf(position+1),Toast.LENGTH_SHORT).show();
}
});
}
class GridAdapter extends BaseAdapter{
private Context context;
private ArrayList<String> images;
public GridAdapter(ArrayList<String> images,Context context){
this.context = context;
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public Object getItem(int position) {
return images.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.grid_item,null,false);
holder.imgGridItem = (ImageView) convertView.findViewById(R.id.imgGridItem);
holder.txtGridItem = (TextView) convertView.findViewById(R.id.txtGridItem);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
if(position%4 == 0){
holder.txtGridItem.setVisibility(View.VISIBLE);
holder.txtGridItem.setText(String.valueOf((position / 4)+1));
}else{
holder.txtGridItem.setVisibility(View.GONE);
}
holder.imgGridItem.setImageResource(R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView imgGridItem;
TextView txtGridItem;
}
}
}

Categories

Resources