I am using slidingtablayout. There are 5 tabs and i am populating each of them with listview from different json requests. I need to get the name or the id of the tab on clicking on one of the items of the list view
You are going to need to supply us with your current code or implementation - that will give us some direction on how to help you.
But my first guess on your implementation: I am assuming when you say SlidingTabLayout you are referring to the code outlined on android developers: https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html#l199
If that is the case, you can get the tab name from the view pager when a tab is clicked and registered in the click listener:
private class TabClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
if (v == mTabStrip.getChildAt(i)) {
mViewPager.setCurrentItem(i);
//Get tab name from mViewPager - differs based on your implementation
return;
}
}
}
}
Yes is that one outlined in the android developers
Ok, here is the situation
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view=null;
if(position == 0){
view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,container, false);
homeList = (ListView) view.findViewById(R.id.home_list);
queryImp(view, "");
homeJSONAdapter = new JSONAdapter(getActivity(), getActivity().getLayoutInflater());
homeList.setAdapter(homeJSONAdapter);
homeList.setOnItemClickListener(this);
}else{
view = getActivity().getLayoutInflater().inflate(R.layout.other_pager_item,container, false);
otherList = (ListView) view.findViewById(R.id.other_list);
queryImp(view, "other");
otherJSONAdapter = new JSONAdapterOther(getActivity(), getActivity().getLayoutInflater());
otherList.setAdapter(ekonomiaJSONAdapter);
otherList.setOnItemClickListener(this);
}
container.addView(view);
view.findViewById(R.id.item_title);
return view;
}
this is the code that populates the tabs on SlidingTabsBasicFragment and the following is the onclick for the items of the lists
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0)
{
JSONObject jsonObject = (JSONObject) homeJSONAdapter.getItem(position);
String imageURL = jsonObject.optString("img_url");
String artText = jsonObject.optJSONObject("content").optString("rendered");
String artTitle = jsonObject.optJSONObject("title").optString("rendered");
Intent detailIntent = new Intent(getActivity(), SingleArticle.class);
detailIntent.putExtra("imgURL", imageURL);
detailIntent.putExtra("articleText", artText);
detailIntent.putExtra("articleTitle", artTitle);
startActivity(detailIntent);
}else{
JSONObject jsonObject1 = (JSONObject) otherJSONAdapter.getItem(position);
String imageURL1 = jsonObject1.optString("img_url");
String artText1 = jsonObject1.optJSONObject("content").optString("rendered");
String artTitle1 = jsonObject1.optJSONObject("title").optString("rendered");
y
Intent detailIntent1 = new Intent(getActivity(), SingleArticleOther.class);
detailIntent1.putExtra("imgURL1", imageURL1);
detailIntent1.putExtra("articleText1", artText1);
detailIntent1.putExtra("articleTitle1", artTitle1);
startActivity(detailIntent1);
}
which basically, all it does is click on the item of the list and gets to the full article to read. For some reason it all gets a mess. (when i do a log on the position, it gets the position of the item of the list and not that of the tab) When i try to click on an item of the homeJsonAdapter it goes to the article of the otherJsonAdapter. I dont know why.
So what i want to do is get the name of the tab in which the listview is and based on that name, create the JsonAdapter and get the article
Related
I can remove the item in holder but the price doesn't exchange. i had try to make the data remove but it's non sense too
i had try make the sub been minus but's still it's doesn't work on it i had the for list on the holder to made the price exchange it doesn't help through too
One Fragment.java
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View krnjg =inflater.inflate(R.layout.fragment_keranjang,container,false);
RecyclerView rec = krnjg.findViewById(R.id.rc1);
LinearLayoutManager aw1 =new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
rec.setLayoutManager(aw1);
madapter = new ProductAdapter(getContext(), example, getLayoutInflater());
rec.setAdapter(madapter);
madapter.notifyDataSetChanged();
total = krnjg.findViewById(R.id.vtotal);
btnpesan = krnjg.findViewById(R.id.btnpsn);
for (int i = 0;i < example.size();i++){
sub = sub + example.get(i).getPrice();
}
Locale locale = new Locale("in","ID");
NumberFormat formatrupiah = NumberFormat.getCurrencyInstance(locale);
total.setText(formatrupiah.format(sub));
Product adapter.java
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
Locale locale = new Locale("in","ID");
NumberFormat formatrupiah = NumberFormat.getCurrencyInstance(locale);
holder.nmPro.setText(mData.get(position).getTitle());
holder.imge.setImageResource(mData.get(position).getProductImage());
holder.hrg.setText(formatrupiah.format(mData.get(position).getPrice()));
holder.cd.setActivated(mData.get(position).selected);
holder.del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mData.remove(holder.getAdapterPosition());
double sub = 0;
for (int i = 1; i<example.size();i++){
sub -= example.get(i).getPrice();
}
notifyDataSetChanged();
Toast.makeText(mCon,"Barang "+holder.nmPro.getText()+" di hapus dari keranjang",Toast.LENGTH_SHORT).show();
}
});
}
i expect that the holder won't worked even the data that was holder it for just on that view not on the fragment that used to handle or maybe another ways to find it out because the total and the recycler is on different fragment that there is on adapter and fragment it's self
I have been solved this I just used a new classes to hold the click and price even I delete the item the price still same, which else item still on there. So this is I used for
public double getTotalPrice() {
double totalPrice = 0;
for (int i = 0; i < mData.size(); ++i) {
totalPrice = totalPrice + mData.get(i).getPrice();
}
return totalPrice;
}
this class to get the total price on adapter and I got the class on holder too. For when I deleted the item the price is still same which item still on there like this I used it for
holder.del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mData.remove(holder.getAdapterPosition());
notifyDataSetChanged();
tvTotal.setText(formatrupiah.format(getTotalPrice()));
Toast.makeText(mCon,"Barang "+holder.nmPro.getText()+" di hapus dari keranjang",Toast.LENGTH_SHORT).show();
}
});
mData is to get the data product from other activity I inputed and remove is to remove item from the cart and tvTotal is the price whenever the price inputed it can be deleted using this class on holder
i made a custom listView having two textViews..for example student name and roll no...
now i want to implement a selection...for example..i will click on few students in the listview and the object should get saved somewhere ...for example:
ClassList.add(new StudentList(99,"student1","code"));
ClassList.add(new StudentList(70,"student2","code"));
ClassList.add(new StudentList(20,"student3","code"));
ClassList.add(new StudentList(30,"student4","code"));
adapter = new StudentListViewAdapter(this, ClassList,"code");
listView.setAdapter(adapter);
now i should be able to set another method for example:
ClassList2=listView.getSelectedStudents();
this should return the selected students..for example if i select student 1,student 2 it should return Student List Objects for both of them so i can access there roll and Code too
And there is another problem...when i deselect it should remove that object from the list..
It would be really helpfull if someone will write a sample code
This is how i do it..but i don't think it is really a good method
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (ItemView == null) {
ItemView = inflater.inflate(R.layout.customvistview_atd, parent, false);
}
//find the hotels to work with
final studentlist currentList = totalClass_list.get(position);
//fill the view
final TextView textview_studentName = (TextView) ItemView.findViewById(R.id.textview_atd_studentName);
textview_studentName.setText((currentList.getName()));
final TextView textview_studentRollno = (TextView) ItemView.findViewById(R.id.textview_atd_studentRollno);
textview_studentRollno.setText((currentList.getRoll_no() + ""));
final CardView cardView = (CardView) ItemView.findViewById(R.id.card_view_atd);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (i = 0; i < presentStudents.size(); i++) {
if (presentStudents.get(i).getMindex() == position) {
System.out.println("m-index : "+presentStudents.get(i).getMindex());
System.out.println("position : "+position);
isChecked = true;
System.out.println("I am in the loop, I am at position: "+i);
me = i;
break;
}
}
if (isChecked) {
presentStudents.remove(me);
isChecked = false;
ColorDrawable[] color = {new ColorDrawable(Color.parseColor("#198b9f")), new ColorDrawable(Color.WHITE)};
TransitionDrawable trans = new TransitionDrawable(color);
cardView.setBackground(trans);
trans.startTransition(1500);
textview_studentName.setTextColor(Color.GRAY);
textview_studentName.setTextColor(Color.GRAY);
//cardView.setBackgroundColor(Color.WHITE);
System.out.println("I am in isChecked");
} else {
presentStudents.add(new tempclass(currentList.getRoll_no(),atdCode,position));
ColorDrawable[] colorCard = {new ColorDrawable(Color.WHITE), new ColorDrawable(Color.parseColor("#198b9f"))};
TransitionDrawable trans = new TransitionDrawable(colorCard);
cardView.setBackground(trans);
trans.startTransition(1500);
textview_studentName.setTextColor(Color.WHITE);
textview_studentName.setTextColor(Color.WHITE);
// cardView.setBackgroundColor(Color.BLUE);
System.out.println("I am in else, making it blue");
}
}
});
you can use this one
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
StudentLists studentList = listView.getItemAtPosition(i);//selected object
adapter.dismiss(); // If you want to close the adapter
}
});
I have list of contacts which has to be displayed in alphabetic under each alphabet as shown in the image shown
How can I do this in RecyclerView, please suggest a solution.thanks
Sort list with data by name
Iterate via list with data, and in place when current's item first letter != first letter of next item, insert special kind of object.
Inside your Adapter place special view when item is "special".
This is what I did following #divers's post:
as he mentioned I pass a team list to the the adapter which is sorted and alphabets are added before the next name.
this is he code used to set adapter
void updateUI(ArrayList<TeamMember> teamMembers) {
adapter = new TeamMemberActivityAdapter(this, addAlphabets(sortList(teamMembers)));
recList.setAdapter(adapter);
recList.setVisibility(View.VISIBLE);
spinningProgressView.setVisibility(View.GONE);
}
code to sort the team list obtained from server is given below:
ArrayList<TeamMember> sortList(ArrayList<TeamMember> list) {
Collections.sort(list, new Comparator<TeamMember>() {
#Override
public int compare(TeamMember teamMember1, TeamMember teamMember2) {
return teamMember1.getFullname().compareTo(teamMember2.getFullname());
}
});
return list;
}
while adding alphabets to the list I am setting a type value to know whether its alphabet or team name to check this inside the adapter for showing corresponding layout .the code for that is as shown below:
ArrayList<TeamMember> addAlphabets(ArrayList<TeamMember> list) {
int i = 0;
ArrayList<TeamMember> customList = new ArrayList<TeamMember>(); TeamMember firstMember = new TeamMember();
firstMember.setFullname(String.valueOf(list.get(0).getFullname().charAt(0)));
firstMember.setType(1);
customList.add(firstMember);
for (i = 0; i < list.size() - 1; i++) {
TeamMember teamMember = new TeamMember();
char name1 = list.get(i).getFullname().charAt(0);
char name2 = list.get(i + 1).getFullname().charAt(0);
if (name1 == name2) {
list.get(i).setType(2);
customList.add(list.get(i));
} else {
list.get(i).setType(2);
customList.add(list.get(i));
teamMember.setFullname(String.valueOf(name2));
teamMember.setType(1);
customList.add(teamMember);
}
}
list.get(i).setType(2);
customList.add(list.get(i));
return customList;
}
And finally inside your adapter check if the item is teamMember name or alphabet and display corresponding layout as shown below:
#Override
public int getItemViewType(int position) {
int viewType = 0;
if (mMembers.get(position).getType() == TYPE_LETTER) {
viewType = TYPE_LETTER;
} else if (mMembers.get(position).getType() == TYPE_MEMBER) {
viewType = TYPE_MEMBER;
}
return viewType;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case TYPE_LETTER:
ViewGroup vGroupImage = (ViewGroup) mInflater.inflate(R.layout.board_team_letter_item, viewGroup, false);
ViewHolderLetter image = new ViewHolderLetter(vGroupImage);
return image;
case TYPE_MEMBER:
ViewGroup vGroupText = (ViewGroup) mInflater.inflate(R.layout.board_team_member_item, viewGroup, false);
ViewHolderMember text = new ViewHolderMember(vGroupText);
return text;
default:
ViewGroup vGroupText2 = (ViewGroup) mInflater.inflate(R.layout.board_team_member_item, viewGroup, false);
ViewHolderMember text1 = new ViewHolderMember(vGroupText2);
return text1;
}
}
hope this could help you. all the best
compare your model and get first character from title ....
private void getHeaderListLatter(ArrayList<CountriesModel> usersList) {
Collections.sort(usersList, new Comparator<CountriesModel>() {
#Override
public int compare(CountriesModel user1, CountriesModel user2) {
return String.valueOf(user1.name.charAt(0)).toUpperCase().compareTo(String.valueOf(user2.name.charAt(0)).toUpperCase());
}
});
String lastHeader = "";
int size = usersList.size();
for (int i = 0; i < size; i++) {
CountriesModel user = usersList.get(i);
String header = String.valueOf(user.name.charAt(0)).toUpperCase();
if (!TextUtils.equals(lastHeader, header)) {
lastHeader = header;
mSectionList.add(new CountriesModel(header,true));
}
mSectionList.add(user);
}
}
and in your adapter getItemViewType Layout like this ....
#Override
public int getItemViewType(int position) {
if (mCountriesModelList.get(position).isSection) {
return SECTION_VIEW;
} else {
return CONTENT_VIEW;
}
}
for complete reference .
https://github.com/sayanmanna/LetterSectionedRecyclerView
I'm currently using this. It's very easy to implement, compatible with RecyclerView adapter, and so lightweight you'd barely call it a library!
You can achieve it with this library.
There is a full example here of how to add headers.
And if you want to implement the search functionality, there is also a full example here, this is the result:
https://github.com/emilsjolander/StickyListHeaders
I hope this is what You want.
I am developing an android app, in it i am displaying a ListView with a number of items. on Click of an item i am passing item name and ArrayList of all items .
Intent myIntent = new Intent(context, DetailActivity.class);
myIntent.putExtra("name", name);
myIntent.putStringArrayListExtra("arraylistofitems", arraylistofitems);
context.startActivity(myIntent);
In DetailActivity, i want to show the clicked items from list and on scroll left or right show the remaining item from the list. I am using following code -
Intent mIntent = getIntent();
name = mIntent.getStringExtra("name");
names = mIntent.getStringArrayListExtra("arraylistofitems");
viewPager = (ViewPager) findViewById(R.id.idofviewpager);
adapter = new ViewPagerAdapter(context, arraylistofitems);
viewPager.setAdapter(adapter);
and ViewPagerAdapter looks like following -
public ViewPagerAdapter(Context context,ArrayList<String> data) {
this.context = context;
this.data=data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((MyLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// item views declaration
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item_id, container,
false);
//layout item initialization
((ViewPager) container).addView(itemView);
ParseQuery<ParseObject> query = ParseQuery.getQuery("myclass");
query.whereEqualTo("colName", data.get(position));
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> itemList, ParseException arg1) {
for (int i = 0; i < itemList.size(); i++) {
ParseObject po = itemList.get(i);
//set the values from parse into views
}
}
});
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((MyLayout) object);
}
But all data being shown from starting (0th Position to length of arraylist).
Question - How do i implement scroll effect with clicked item being opened and on scrolling left left items should be display or vice-versa?
You can do one thing , pass position of item which is being clicked and get it on your detail activity same as you are getting item name and array of list. and then call following line -
viewPager.setCurrentItem(position);
So, now there is no need to manage position on adapter. adapter will take care of it.
First of all, I'm a beginner in android programming. So dont be too harsh :P
Anyway, I have a recycling ListView, containing an Image and a Text per List Item.
Let's say there are 100 different Items. Every Item clicked leads me to my Activity "DetailActivity.class" with the "detail_layout.xml". Now I want to configurate this layout depending on which Item was clicked.
TestActivity.java:
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i <= 99; i++) {
list.add(detail_array[i]);
//detail_array[] contains Strings which are used to add Items to the list.
}
private ListView myList = (ListView)findViewById(R.id.list);
myList.setAdapter(new MyCustomAdapter(TestActivity.this,list));
MyCustomAdapter.java:
public class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mListItemsTV;
private LayoutInflater mLayoutInflater;
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = mLayoutInflater.inflate(R.layout.list_item, null);
holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
String stringItem = mListItemsTV.get(position);
if (stringItem != null) {
if (holder.itemName != null) {
holder.itemName.setText(stringItem);
ImageView imageView = (ImageView) view.findViewById(R.id.list_item_image_view);
if(holder.itemName.getText() == "SomeName") {
imageView.setImageResource(R.drawable.somedrawable);
}
return view;
}
To be honest, I've got those from some kind of tutorial and I guess I know whats happening there^^
Now, how can I manage to get which Item was clicked? I tried it several times with "setOnClickListener" but it never seems to work. How do I use this? Should I use this? Where do I have to use it?
The basic idea is listview is generally dynamic so you will have a collection of an arbatrary number of items that you want to register with a listener. Oclicklistener will not work unless you register each item separate (not a good idea). Use onitemclicklistener to register the collection this link should help. http://www.ezzylearning.com/tutorial.aspx?tid=1351248&q=handling-android-listview-onitemclick-event
You'll want to use setOnItemClickListener. This is an example on how it's used. You'll need to adapt it to your code. But you can see how you would reference an individual item in the list.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
#Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
I'll fix the formatting when I get home. On mobile ATM. If anyone wants to edit the code feel free.
If you don't want to use an adapter then try this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch(position){
/*
* The case is the list position
*/
case 0:
break;
case 1:
break;
}
}