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".
}
});
Related
my problem is connected with listviews. From Menu im going to first activity where I am able to click on item, what let's me go to secondary activity where I have second listview. My problem ocures there because I am unable to clikc anything in second activity apart from Back button. I have custom made adapter, but it's regular row list adapter. After clicking on item on secondary listview I want to go to next third activity. Is there any solution? Help please.
Adapter:
public class listviewAdapter4 extends BaseAdapter {
public ArrayList<Operation4> productList2;
Activity activity;
public listviewAdapter4(Activity activity,ArrayList<Operation4> productList2){
super();
this.activity = activity;
this.productList2 = productList2;
}
#Override
public int getCount() {
return productList2.size();
}
#Override
public Object getItem(int position) {
return productList2.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
Textviews...
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_listview_row4, null);
holder = new ViewHolder();
holders...
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Operation4 item = productList2.get(position);
holders...
return convertView;
}
Click listeners:
1)
lvPositions.setOnItemClickListener(new AdapterView.OnItemClickListener () {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /*code starting new activity */ }
});
lvPositions2.setOnItemClickListener(new AdapterView.OnItemClickListener () {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /*code starting new activity */ }
});
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;
}
I'm pulling data and loading it into a BaseAdapter for my List View. Currently, the items in the listview are the same as the last item added, so the list looks like this:
Item 3
Item 3
Item 3
Instead of:
Item 1
Item 2
Item 3
I've checked over the data sources, and the data that's loaded from the source is unique each time. I think there's somehting wrong with my BaseAdapter, but I'm not sure what it is. Here's my BaseAdapter:
BaseAdapter.java
ArrayList<ThreadListData> myList = new ArrayList<>();
LayoutInflater inflater;
Context context;
public ThreadBaseAdapter(Context context, ArrayList<ThreadListData> myList) {
this.context = context;
inflater = LayoutInflater.from(this.context);
this.myList = myList;
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ThreadListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}
I'll start with my BaseAdapter, and can show more code if requested. All Help Is Appreciated!
You missed to return the position
Modifiy your getItemId as
#Override
public long getItemId(int position) {
return position;
}
There are 2 things you need to change:
1. You return wrong item position
#Override
public long getItemId(int position) {
return 0;
}
It should be
#Override
public long getItemId(int position) {
return position;
}
2. Your declared a LayoutInflater and create new instance in constructor. But in the getView(), you create another LayoutInflater. Look at updated code below
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// LayoutInflater inflater = LayoutInflater.from(this.context);
inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);
}
ThreadListData listData = getItem(position);
TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
TextView username = (TextView)convertView.findViewById(R.id.username_newthread);
name.setText(listData.getName());
username.setText(listData.getName());
ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);
BitmapTask task = new BitmapTask(listData.getImage());
task.execute(profile);
return convertView;
}
I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.
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