My Custom ListView Adapter class like this.
I want to get the index number of an item once an item has been pressed(onclick). How can I get the index number and pass it to a int.. The code where I'll get the index number is after the onCreate method. Please help me!
// On holder.txtstormName_Nice button click i want to get selected item index.
public class ListViewCustomAlerts extends BaseAdapter {
ArrayList<HurricaneBeanClass> itemList;
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAlerts(Activity context,ArrayList<HurricaneBeanClass> arraylist_List)
{
super();
this.context = context;
this.itemList = arraylist_List;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return itemList.size();
}
#Override
public Object getItem(int position)
{
return itemList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
p =position;
final ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.hurricane_row, null);
holder.txtstormName_Nice=(TextView)convertView.findViewById(R.id.hurricaneNameRowTextView);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
holder.txtstormName_Nice.setText(bean.getStormName_Nice());
holder.txtstormName_Nice.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//here i want item index.................
HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
HurricaneActivity.curlat=bean.getCurentlat();
HurricaneActivity.curlon=bean.getCurentlon();
...
}
});
return convertView;
}
}
public static class ViewHolder
{
TextView txtstormName_Nice;
}
List View Item Position is already coming in argument list.
public View getView(int position, View convertView, ViewGroup parent) {
If you want de id of element you can write in getView:
getItemId(position)
In getView() method you can get it
public View getView(final int position, View convertView, ViewGroup parent) {
.........
.....
System.out.println("Index:"+position);
}
So, here position is your index
Related
I'm trying to make time table application. my layout is gridview.
My layout is now like this : https://user-images.githubusercontent.com/37032956/57445643-c2765180-728d-11e9-9f1a-52e2d93f7e9e.JPG
I can get data and draw in a line.
But i don't know how to locate this cells in particular location.
I want to place this 4 colored text in exact location using day, start time, end time.
This is my code.
public class GridviewAdapter extends BaseAdapter {
private ArrayList<GridViewItem> listData;
LayoutInflater layoutInflater;
public GridviewAdapter (Context context, ArrayList<GridViewItem> listData){
this.listData = listData;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position); // 수정
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.gridview_item, (ViewGroup) convertView, false);
gridViewHolder = new GridViewHolder();
gridViewHolder.subjectTitle=(TextView) convertView.findViewById(R.id.item_title);
convertView.setTag(gridViewHolder);
} else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
gridViewHolder.subjectTitle.setText(listData.get(position).getSub_title());
return convertView;
}
static class GridViewHolder{
TextView subjectTitle;
// day, start_time, end_time;
}
}
Here is my grid view adapter.
You can use OnItemClickListener which correspond to the item position.
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
//Selected item is at index "position".
}
});
I can't highlight all the selected items. Only one item is highlighted at a time. If next item is clicked the previous highlighted item goes back to normal.
Here is my CustomAdapter
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
//let android do the initializing :)
super(context, textViewResourceId, Strings);
}
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount()
{
return dataList.size();
}
#Override
public HashMap<String, Object> getItem(int position)
{
return dataList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
//class for caching the views in a row
private class ViewHolder {
TextView not, isRead;
LinearLayout noti_linear;
}
//Initialise
ViewHolder viewHolder;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//inflate the custom layout
convertView = inflater.from(parent.getContext()).inflate(R.layout.notification_layout, parent, false);
viewHolder = new ViewHolder();
//cache the views
viewHolder.noti_linear = (LinearLayout) convertView.findViewById(R.id.not_layout);
viewHolder.not = (TextView) convertView.findViewById(R.id.textview_noti);
viewHolder.isRead = (TextView) convertView.findViewById(R.id.textview_isRead);
//link the cached views to the convertview
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
//set the data to be displayed
viewHolder.not.setText(dataList.get(position).get("CheckList").toString());
if(selectedIndex!= -1 && position == selectedIndex)
{
viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
}
else
{
viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
}
// if (getItemId(position) == StringHolder.mSelectedItem) {
// viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
//
// } else {
// viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
// }
return convertView;
}
}
Here is my onItemClick
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cardAdapter.setSelectedIndex(position);
}
});
You can do it by below code instead of applying in adapter class,
Check below code,
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
//cardAdapter.setSelectedIndex(position);
}
});
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
In my application, the user orders a household product, and the application displays PRODUCT NAME & PRICE in list view, I want to set different background for out of stock products. Can anyone help me.
public class Products extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
boolean Connection;
public Products(Activity activity,
ArrayList<HashMap<String, String>> list) {
super();
this.activity = activity;
this.list = list;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView item_name,item_price,product_id,item_stock;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.submenutext, null);
if(ITEM_AVALIABLE_QUANTITY.equalsIgnoreCase("0")){
convertView.setBackgroundResource(R.drawable.heading);
}
/*else{
convertView.setBackgroundResource(R.drawable.sub_menu);
}*/
holder = new ViewHolder();
holder.product_id = (TextView) convertView.findViewById(R.id.hide_text);
holder.item_name = (TextView) convertView.findViewById(R.id.text);
holder.item_price = (TextView) convertView.findViewById(R.id.price);
//holder.item_stock = (TextView)convertView.findViewById(R.id.stock);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final HashMap<String, String> map = list.get(position);
holder.product_id.setText(map.get(PRODUCT_ID));
holder.item_name.setText(map.get(ITEM_NAME_COLUMN));
holder.item_price.setText(map.get(ITEM_PRICE_COLUMN));
holder.item_stock.setText(map.get(ITEM_AVALIABLE_QUANTITY));
/*if(holder.item_stock.equals("0")){
convertView.setBackgroundResource(R.drawable.heading);
}*/
return convertView;
}
}
You can send data to your custom adapter which having it's own customize layout.
Main.java
ListAdapter adapter = new ListAdapter(this, arrayList);//ListAdapter is custom Adapter
hotelList.setAdapter(adapter); // hotellist is instance of ListView
Here is your custom Adapter .java file. this class have one separate view i.e. list_row.
public class LazyAdapterForReminder extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater=null;
public LazyAdapterForReminder(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView lv = (TextView)vi.findViewByID(R.id.blabla);
if(stock==o)
lv.setBackgroundResource(R.color.red);
else
lv.setBackgroundResource(R.color.green);
return vi;
}
}
I hope that you familiar with writing Custom Adapter to set on List View. You manage your List items with out of stock product state along with product name and date. On CustomAdapter getView method you can check state and set background of you list item color or else set correponding image drawable.
For Eg:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(stock==0){
convertView.setBackgroundColor(R.color.grey);
}else{
convertView.setBackgroundColor(R.color.red);
}
return convertView;
}
You could set the background in your Adapter getView.
The code would look like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listsearchitem, null);
}
//.................
// then you could set the backgorun for the out of stock products
convertView.setBackgroundResource(resid)
//.................
}
I have a list view whose adapter is in a different class in a different package. Now I have to get the data from database then I set Adapter for my list view using this data. So I have created an ArryList and pass this in the constructor of Adapter while seeting it for the list view. But the problem is that the data is repeating. eg.- there are 12 distinct Strings in the arraylist but what I get is -first five elements in order and after that the same five are being repeated. The count of data is always right but the position will always be 0,1,2,3,4 . I can not understand what the problem is . here is the code -
public class CheckboxAdapter extends BaseAdapter{
LayoutInflater inflater ;
ArrayList<String> mData = new ArrayList<String>();
//constructor for lesion adapter
public CheckboxAdapter (Context context, ArrayList<String> data){
inflater = LayoutInflater.from(context);
mData = data;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
}//end of if condition
return convertView;
}
try this.. just write all code out of the condition expect inflate code..
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
} //end of if condition
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
return convertView;
}
i use the following code to list few items from array , while scrolling the list view items is append more with same data exit in array i don`t know what mistake i made.
Anyone pointed out where i made the mistake.
private static String array_spinner_subcategories[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.sub_categories);
setSubCat();
ListView sub_categories=(ListView)findViewById(R.id.sub_catlist);
sub_categories.setAdapter(new EfficientAdapter(this));
sub_categories.setAdapter(adapter);
sub_categories.setOnItemClickListener(subcatlistener);
cr=getContentResolver();
}
public String[] setSubCat(){
recordDB=new Viddatabase(this);
db=recordDB.getWritableDatabase();
array_spinner_subcategories=recordDB.subcategoriesList(db);
recordDB.close();
return array_spinner_subcategories;
}
private OnItemClickListener subcatlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
Toast.makeText(SubCategories.this,array_spinner_subcategories[position],
Toast.LENGTH_LONG).show();
}
};
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return array_spinner_subcategories.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.albumlist, null);
holder = new ViewHolder();
holder.subCategories = (TextView) convertView.findViewById(R.id.albumDetails);
holder.subCategories.setText(array_spinner_subcategories[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView subCategories;
}
}
Move the holder.subCategories.setText(array_spinner_subcategories[position]); line in getView() method below the else block
In your getView method, you are only setting data in the view when you create a new view.
The ListView recycles views, so you will very likely be passed in a a view to reuse, which is why it is referred to as a convertview.
You need to be calling setText on the view every time this is called, otherwise you are just handing back the convertview unchanged and thus you are getting the same values repeated.