How to convert Firebase child name into date and time - date

I have a basic app that has RecyclerView for getting data from a Firebase database, which looks like this:
I would like to have the 20170108_100 converted into data and time format (08/01/2017 10:00), which can then be compared to the actual date.
I'm planning to have some IF conditions in order to sort the database by date&time and I need to have something like this:
if (20170108_100 < CurrentDate&Time) then doSomething else doSomethingElse
Is there any way one can do that?
So far, I have this code that reads that database:
public class Hotarari extends Fragment {
View v;
ProgressDialog progress;
private FirebaseRecyclerAdapter < Variabile_primarie, ContactViewHolder > cc;
private RecyclerView mContactRV;
private DatabaseReference mPostRef;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_hotarari, container, false);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.v = view;
initialiseScreen();
}
private void initialiseScreen() {
mContactRV = (RecyclerView) v.findViewById(R.id.contact_recyclerview);
mContactRV.setLayoutManager(new LinearLayoutManager(getContext()));
mPostRef = FirebaseDatabase.getInstance().getReference("-DeciziiPrimari");
setupAdaptater();
mContactRV.setAdapter(cc);
}
private void setupAdaptater() {
cc = new FirebaseRecyclerAdapter < Variabile_primarie, ContactViewHolder > (
Variabile_primarie.class,
R.layout.item_hotarari,
ContactViewHolder.class,
mPostRef
) {
#Override
protected void populateViewHolder(ContactViewHolder viewHolder, final Variabile_primarie model, int position) {
viewHolder.setData(model.getData());
viewHolder.setNumar(model.getNumar());
viewHolder.setHotarare(model.getHotarare());
}
};
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
private TextView tv_hotarare;
private TextView tv_data;
private TextView tv_numar;
public ContactViewHolder(View itemView) {
super(itemView);
tv_hotarare = (TextView) itemView.findViewById(R.id.continut_hotarare);
tv_data = (TextView) itemView.findViewById(R.id.data_hotarare);
tv_numar = (TextView) itemView.findViewById(R.id.numar_hotarare);
}
void setHotarare(String hotarare) {
tv_hotarare.setText(String.valueOf(hotarare));
}
public void setData(String data) {
tv_data.setText(String.valueOf(data));
}
void setNumar(String numar) {
tv_numar.setText(String.valueOf(numar));
}
}
}

Related

problem with Nested recyclerview and LiveData observe

I have nested RecyclerView and two LiveData. one is parentList and another one is childList
I managed to use LiveData for ParentAdapter but when I try LiveData for ChildAdapter nothing showen in childAdapter. ParentAdapter is working.
Can someone help me?
Thanks?
this method is in MainActivity.class
private void sendAllDataToAdapter(){
CashFlowViewModel viewModel = ViewModelProviders.of(this).get(CashFlowViewModel.class);
viewModel.cashGroupByDate().observe(this, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
adapter = new MainAdapter(this, this);
recyclerView.setAdapter(adapter);
}
This is ParentAdapter
public class MainAdapter extends ListAdapter<CashFlow, MainAdapter.MainViewHolder>{
Context context;
List<CashFlow> cashFlowList = new ArrayList<>();
List<CashFlow> cashFlowListChild = new ArrayList<>();
CashflowRepository repository;
CashFlowViewModel viewModel;
LifecycleOwner lifecycleOwner;
public MainAdapter(Context context, LifecycleOwner lifecycleOwner) {
super(diffCallback);
this.context = context;
this.cashFlowList = cashFlowList;
this.cashFlowListChild = cashFlowListChild;
this.repository = repository;
this.lifecycleOwner = lifecycleOwner;
viewModel = ViewModelProviders.of((MainActivity) context).get(CashFlowViewModel.class);
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getBody().equals(newItem.getBody());
}
};
#NonNull
#Override
public MainViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_adapter, parent, false);
return new MainViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MainViewHolder holder, int position) {
holder.tvDate.setText(getItem(position).getAdded_date());
holder.tvIncome.setText(String.valueOf(getItem(position).getIncome()));
holder.tvExpense.setText(String.valueOf(getItem(position).getExpense()));
ChildAdapter adapter = new ChildAdapter(context);
holder.rvChild.setAdapter(adapter);
viewModel.cashGroupByDate().observe(lifecycleOwner, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
Log.d("Child", getItem(position).getAdded_date()+"");
}
public class MainViewHolder extends RecyclerView.ViewHolder {
TextView tvDate, tvIncome, tvExpense;
RecyclerView rvChild;
public MainViewHolder(#NonNull View itemView) {
super(itemView);
tvDate = itemView.findViewById(R.id.main_adapter_date);
tvIncome = itemView.findViewById(R.id.main_adapter_income);
tvExpense = itemView.findViewById(R.id.main_adapter_expense);
rvChild = itemView.findViewById(R.id.child_recyclerview);
}
}
This is ChildAdapter
public class ChildAdapter extends ListAdapter<CashFlow, ChildAdapter.ChildViewHolder> {
Context context;
public ChildAdapter(Context context) {
super(diffCallback);
this.context = context;
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getBody().equals(newItem.getBody())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getExpense() == newItem.getExpense()
&& oldItem.getIncome() == newItem.getIncome()
&& oldItem.getType().equals(newItem.getType());
}
};
#NonNull
#Override
public ChildViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.child_adapter, parent, false);
return new ChildViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ChildViewHolder holder, int position) {
holder.imageView.setImageResource(getItem(position).getImage_id());
holder.tvTitle.setText(getItem(position).getTitle());
if (getItem(position).getType().equals(BaseActivity.INCOME)){
holder.tvSum.setText(String.valueOf(getItem(position).getIncome()));
}
else if (getItem(position).getType().equals(BaseActivity.EXPENSE)){
holder.tvSum.setText(String.valueOf(getItem(position).getExpense()));
}
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView tvTitle, tvSum;
public ChildViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.child_adapter_image);
tvTitle = itemView.findViewById(R.id.child_adapter_title);
tvSum = itemView.findViewById(R.id.child_adapter_sum);
}
}
}
This is my ViewModel.class
public class CashFlowViewModel extends AndroidViewModel {
private CashflowRepository repository;
public CashFlowViewModel(#NonNull Application application) {
super(application);
repository = new CashflowRepository(application);
}
public void insert(CashFlow cashFlow){
repository.insert(cashFlow);
}
public void update(CashFlow cashFlow){
repository.update(cashFlow);
}
public void delete(CashFlow cashFlow){
repository.delete(cashFlow);
}
public LiveData<List<CashFlow>> cashGroupByDate(){
return repository.getCashGroupByDate();
}
public LiveData<List<CashFlow>> cashByDate(String addedDate){
return repository.getCashByDate(addedDate);
}
public void insertCategory(Category category){
repository.insertCategory(category);
}
public void updateCategory(Category category){
repository.updateCategory(category);
}
public void deleteCategory(Category category){
repository.deleteCategory(category);
}
public List<Category> allCategories(String type){
return repository.getAllCategories(type);
}

How to parse JSON using Retrofit correctly and implement it in RecyclerView?

I'm trying to build an app that has 2 tabs that displays Movies and TV Shows using the themoviedb.org API. It's supposed to use the MVVM model and i'm really confuse because it's just been 2 months since i started coding in Android. How to pass it correctly through each class and do i using the retrofit method correctly in the repository class?
The idea is accessing the API through Repository class and pass the data to the ViewModel class, and then display it in MovieFragment using RecyclerView, but it seems i failed to pass the data through each class.
+MovieRepository Class
public class MovieRepository {
private static MovieRepository instance;
private static ArrayList<Movie> movies = new ArrayList<>();
private final String API_KEY = "81d8f4353c2ade529071133972205017";
private String language;
private final String TAG = "repo";
public static MovieRepository getInstance() {
if (instance == null) {
instance = new MovieRepository();
}
return instance;
}
public static ArrayList<Movie> getMovies() {
return movies;
}
public MovieRepository() {
final ArrayList<Movie> movieJSON = new ArrayList<>();
language = "en_US";
ApiConfig api = new ApiConfig();
api.createInstance().loadMovieData(API_KEY, language).enqueue(new Callback<JSONResponse>() {
#Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
ArrayList<Movie> jsonResults = response.body().getResults();
for (int i = 0; i < jsonResults.size() ; i++){
Movie movie = new Movie();
movie.setId(jsonResults.get(i).getId());
movie.setTitle(jsonResults.get(i).getTitle());
movie.setOverview(jsonResults.get(i).getOverview());
movie.setReleaseDate(jsonResults.get(i).getReleaseDate());
movie.setPosterPath(jsonResults.get(i).getPosterPath());
movieJSON.add(movie);
}
Log.d(TAG, "onResponse: " + jsonResults.toString());
}
#Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
this.movies = movieJSON;
}
+MovieViewModel Class
public class MovieViewModel extends ViewModel {
private static MutableLiveData<ArrayList<Movie>> movieData = new MutableLiveData<>();
private static ArrayList<Movie> movies;
public static LiveData<ArrayList<Movie>> getMoviesData() {
return movieData;
}
public static MovieAdapter init(MovieAdapter adapter) {
if (movies == null) {
movies = MovieRepository.getInstance().getMovies();
adapter.setMovies(movies);
}
return adapter;
}
+MovieFragment Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
adapter = new MovieAdapter(getContext());
movieViewModel.init(adapter);
movieViewModel.getMoviesData().observe(this, new Observer<ArrayList<Movie>>() {
#Override
public void onChanged(#Nullable ArrayList<Movie> movies) {
adapter.notifyDataSetChanged();
}
});
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movie_list, container, false);
if (view instanceof RecyclerView) {
final Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
}
return view;
}
it always ends up NullPointer in the Adapter (probably because the data failed to be passed to adapter)

How to add timestamp for CardView

I am using FirebaseRecyclerAdapter my issue or problem is that I need to show timestamp for my posts card so I have searched for 3 months and I couldn't solve this problem so please brother I have done my best and it's time for getting help from others be in mind that I don't have any idea about timestamp ...
here's an example for how I want to show : 3 hours ago, 1 day, just now.
If some one solve me this problem by adding the solve for my JAVA fragment I will be proud for him.
Here's my fragment
public class Challenges extends Fragment{
private RecyclerView mPostList;
private DatabaseReference mDatabase,mDatabaseFriends,mDatabaseLike;
private SwipeRefreshLayout mRefreshLayout;
private boolean mProcessLike = false;
private String AA;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mListener;
private Query mQuery;
private FirebaseUser mCurrentUser;
public Challenges() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MultiDex.install(getActivity());}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.challenges, container, false);
mListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser User = firebaseAuth.getCurrentUser();
if (User == null) {
Intent A = new Intent(getActivity(), Login.class);
startActivity(A);}}};
mPostList=(RecyclerView)v.findViewById(R.id.PostList);
mPostList.setHasFixedSize(true);
mPostList.setItemAnimator(new DefaultItemAnimator());
mPostList.setLayoutManager(new LinearLayoutManager(getActivity()));
//Firebase
mDatabase= FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseFriends= FirebaseDatabase.getInstance().getReference().child("Friends");
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabase.keepSynced(true);
mDatabaseLike.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mRefreshLayout=(SwipeRefreshLayout)v.findViewById(R.id.RefreshLayout);
//mQuery = mDatabase.orderByChild("UID").equalTo(String.valueOf(mDatabaseFriends.child(mCurrentUser.getUid()).child("Accepted")));
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
onStart();}});
return v;}
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mListener);
mRefreshLayout.setRefreshing(true);
final FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mDatabase) {
#SuppressLint("SetTextI18n")
#Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {
final String Post_Key = getRef(position).getKey();
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent A = new Intent(getActivity(), com.pcsoftgroup.test.activities.Single_view.class);
A.putExtra("Key", Post_Key);
startActivity(A);}
});
viewHolder.setUsername(model.getUsername());
viewHolder.setProfile(getActivity().getApplicationContext(), model.getProfile());
viewHolder.setDescribe(model.getDescribe());
viewHolder.setWallpaper(getActivity().getApplicationContext(), model.getWallpaper());
viewHolder.setLike(Post_Key);
viewHolder.setLikeNum(Post_Key);
viewHolder.setCommentNum(Post_Key);
viewHolder.mPostTime.setText(model.getTime());
viewHolder.mLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(Post_Key).hasChild(mCurrentUser.getUid())) {
mDatabaseLike.child(Post_Key).child(mCurrentUser.getUid()).removeValue();
mProcessLike = false;
} else {
mDatabaseLike.child(Post_Key).child(mCurrentUser.getUid()).setValue("LIKED");
mProcessLike = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
viewHolder.mShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
//sendIntent.putExtra(Intent.EXTRA_TEXT,viewHolder.mComment.getText().toString());
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent, "Share via");
startActivity(sendIntent);
}
});
mRefreshLayout.setRefreshing(false);
mRefreshLayout.setEnabled(true);
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);}
public static class PostViewHolder extends RecyclerView.ViewHolder{
//Main
View mView;
ImageButton mLike;
ImageButton mCommentBtn;
ImageButton mShare;
//Firebase
FirebaseAuth mAuth;
FirebaseUser mCurrentUser;
DatabaseReference mDatabaseLike;
DatabaseReference mDatabaseComment;
public PostViewHolder(View itemView) {
super(itemView);
//Main
mView=itemView;
mLike=(ImageButton) mView.findViewById(R.id.PostLike);
mCommentBtn = (ImageButton) mView.findViewById(R.id.PostCommentBtn);
mShare = (ImageButton) mView.findViewById(R.id.PostShare);
//Firebase
mAuth =FirebaseAuth.getInstance();
mCurrentUser=mAuth.getCurrentUser();
mDatabaseComment = FirebaseDatabase.getInstance().getReference().child("Comments");
mDatabaseLike= FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabaseLike.keepSynced(true);
mDatabaseComment.keepSynced(true);
}
//Like
void setLike(final String Post_Key){
mDatabaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(Post_Key).hasChild(mCurrentUser.getUid())){
mLike.setImageResource(R.drawable.like_icon);
}else {mLike.setImageResource(R.drawable.unlike_icon);}}
#Override
public void onCancelled(DatabaseError databaseError) {}});}
void setLikeNum(final String Post_Key){
mDatabaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String A = String.valueOf(dataSnapshot.child(Post_Key).getChildrenCount());
TextView PostLikeNum = (TextView) mView.findViewById(R.id.PostLikeNum);
PostLikeNum.setText(A);}
#Override
public void onCancelled(DatabaseError databaseError) {}});}
void setCommentNum(final String Post_Key){
mDatabaseComment.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String C = String.valueOf(dataSnapshot.child(Post_Key).getChildrenCount());
TextView PostCommentNum = (TextView) mView.findViewById(R.id.PostCommentNum);
PostCommentNum.setText(C);}
#Override
public void onCancelled(DatabaseError databaseError) {}});}
//Username
public void setUsername(String Username){
TextView PostUsername = (TextView) mView.findViewById(R.id.PostUsername);
PostUsername.setText(Username);}
//Profile
void setProfile(Context ctx2, String Profile){
ImageView PostProfile = (ImageView) mView.findViewById(R.id.PostUserImage);
if (Profile == null){PostProfile.setVisibility(View.VISIBLE);}
else {PostProfile.setVisibility(View.VISIBLE);
Picasso.with(ctx2).load(Profile).resize(120,120).into(PostProfile);}}
//Describe
void setDescribe(String Describe){
TextView PostDescribe = (TextView) mView.findViewById(R.id.PostDesc);
PostDescribe.setText(Describe);}
//Image Load
void setWallpaper(Context ctx, String Image){
ImageView PostWallpaper = (ImageView) mView.findViewById(R.id.PostWallpaper);
if (Image == null){PostWallpaper.setVisibility(View.VISIBLE);}
else {
PostWallpaper.setVisibility(View.VISIBLE);
Picasso.with(ctx).load(Image).resize(1200,600).into(PostWallpaper);}}
}}
And here's my getter and setter java:
public class Getting_Posts {
private String Username;
private String Profile;
private String Describe;
private String Wallpaper;
public Getting_Posts() {
}
public Getting_Posts(String Username, String Profile, String Describe, String Wallpaper) {
this.Username = Username;
this.Profile = Profile;
this.Describe = Describe;
this.Wallpaper = Wallpaper;
}
public String getDescribe() {
return Describe;
}
public void setDescribe(String describe) {
Describe = describe;
}
public String getWallpaper() {
return Wallpaper;
}
public void setWallpaper(String wallpaper) {
Wallpaper = wallpaper;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getProfile() {
return Profile;
}
public void setProfile(String profile) {
Profile = profile;
}
}

Android ListView - ExpandableHeightListView add a limit of 10 records per page

My listview is found inside a ScrollView. The ListView was extended by ExpandableHeightListView class. When, 20 row items are loaded, the image library freeze the UI until all images are loaded. Then, user can scroll and select an item. To handle this issue, I tried to load every 10 records in the arraylist. Can i do it directly in the ExpandableHeightListView , if so how ?
public class ExpandableHeightListView extends ListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Calculate entire height by providing a very large height hint.
// MEASURED_SIZE_MASK represents the largest height possible.
//int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
//super.onMeasure(widthMeasureSpec, expandSpec);
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
The ListView adapter is as follows:
public class ResultatRechercheAdapter extends BaseAdapter {
public static final String IMAGE_CACHE_DIR = "images";
public static final String EXTRA_IMAGE = "extra_image";
public List<MRechercheResult> items;
public final OnClickListener itemButtonClickListener;
public final Context context;
public Activity activity;
public String agenda_date;
public String agenda_from;
public String agenda_to;
public ViewHolder holder;
public int agenda_from_hour=-1;
public int agenda_from_minute=-1;
public int agenda_to_hour;
public int agenda_to_minute;
public String from_am_pm;
public String to_am_pm;
private String monday;
private String tuesday;
private String wednesday;
private String thursday;
private String friday;
private String saturday;
private String sunday;
public ImageFetcher mImageFetcher;
public ResultatRechercheAdapter(Activity activity,Context context, List<MRechercheResult> resultat, OnClickListener itemButtonClickListener) {
this.activity = activity;
this.context = context;
this.items = resultat;
this.itemButtonClickListener = itemButtonClickListener;
//this.mImageFetcher = mImageFetcher;
try {
monday= activity.getResources().getString(R.string.monday);
tuesday= activity.getResources().getString(R.string.tuesday);
wednesday= activity.getResources().getString(R.string.wednesday);
thursday= activity.getResources().getString(R.string.thursday);
friday= activity.getResources().getString(R.string.friday);
saturday= activity.getResources().getString(R.string.saturday);
sunday= activity.getResources().getString(R.string.sunday);
} catch(Exception ex){}
}
#Override
public int getCount() {
return items.size();//items.size()
}
#Override
public MRechercheResult getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 1;//items.size()
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_card, null);
holder = new ViewHolder();
holder.txRowTitle = (TextView) convertView.findViewById(R.id.txRowTitle);
holder.imThumbnail= (RecyclingImageView) convertView.findViewById(R.id.imThumbnail);
holder.itemButton1 = (Button) convertView.findViewById(R.id.list_item_card_close);
holder.itemButton2 = (Button) convertView.findViewById(R.id.list_item_card_button_2);
holder.imCircleLundi= (ImageView) convertView.findViewById(R.id.imCircleLundi);
holder.imCircleMardi= (ImageView) convertView.findViewById(R.id.imCircleMardi);
holder.imCircleMercredi= (ImageView) convertView.findViewById(R.id.imCircleMercredi);
holder.imCircleJeudi= (ImageView) convertView.findViewById(R.id.imCircleJeudi);
holder.imCircleVendredi= (ImageView) convertView.findViewById(R.id.imCircleVendredi);
holder.imCircleSamedi= (ImageView) convertView.findViewById(R.id.imCircleSamedi);
holder.imCircleDimanche= (ImageView) convertView.findViewById(R.id.imCircleDimanche);
holder.rlListRecherche= (RelativeLayout) convertView.findViewById(R.id.rlListRecherche);
holder.llImage= (LinearLayout) convertView.findViewById(R.id.llImage);
holder.rlListClose= (RelativeLayout) convertView.findViewById(R.id.rlListClose);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
boolean no_record = items.get(position).no_record;
if (no_record){ // listview is empty
holder.llImage.setVisibility(View.GONE);
holder.rlListClose.setVisibility(View.GONE);
holder.rlListRecherche.setVisibility(View.VISIBLE);
} else {
Utils.shortWeek(activity, convertView);
holder.llImage.setVisibility(View.VISIBLE);
holder.rlListClose.setVisibility(View.VISIBLE);
holder.rlListRecherche.setVisibility(View.GONE);
if (itemButtonClickListener != null) {
//holder.itemButton1.setOnClickListener(itemButtonClickListener);
holder.itemButton1.setVisibility(View.GONE);
holder.itemButton2.setOnClickListener(itemButtonClickListener);
}
final String image_url = items.get(position).profil_photo.toString().replace("\\","").replace("hepigo", "helpigo");
String item_title = items.get(position).profil.toString();
if (item_title.equalsIgnoreCase("Julien Perez")){
if (true){
String display_pos = String.valueOf(position);
System.out.println(display_pos);
}
}
//searchRequest(position,image_url);
//if (!TextUtils.isEmpty(image_url))
// mImageFetcher.loadImage(image_url, holder.imThumbnail);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(items.get(position).profil_photo)) {
//no url
} else {
Picasso.with(activity)
.load(image_url)
.error(android.R.drawable.stat_notify_error)
.transform(transformation)
.placeholder(R.drawable.loading_image_placeholder)
.config(Config.RGB_565)
.into(holder.imThumbnail);
}
}
});
//if (!TextUtils.isEmpty(image_url))
// new DownloadImageTask(holder.imThumbnail).execute(image_url);
holder.txRowTitle.setText(item_title);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
List<Agenda> agenda = items.get(position).agenda;
Utils.displayAgenda(agenda,holder.imCircleLundi,monday);
Utils.displayAgenda(agenda,holder.imCircleMardi,tuesday);
Utils.displayAgenda(agenda,holder.imCircleMercredi,wednesday);
Utils.displayAgenda(agenda,holder.imCircleJeudi,thursday);
Utils.displayAgenda(agenda,holder.imCircleVendredi,friday);
Utils.displayAgenda(agenda,holder.imCircleSamedi,saturday);
Utils.displayAgenda(agenda,holder.imCircleDimanche,sunday);
}
});
}
return convertView;
}
public static class ViewHolder {
public RelativeLayout rlListClose;
public LinearLayout llImage;
public RelativeLayout rlListRecherche;
public ImageView imCircleSamedi;
public ImageView imCircleDimanche;
public ImageView imCircleVendredi;
public ImageView imCircleJeudi;
public ImageView imCircleMercredi;
public ImageView imCircleMardi;
public ImageView imCircleLundi;
public RecyclingImageView imThumbnail;
public TextView txRowTitle;
public Button itemButton1;
public Button itemButton2;
}

How to create ExpandableListView keeping some child fixed?

I want to create an expandable list view Keeping some child visible,where as I want to show rest on click.
Please suggest what is the best approach in such scenario,if any custom element or any tutorial as such.
Many Thanks,
This will be helpful to you.
Adapter class:-
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents,
ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
}
((CheckedTextView) convertView).setText(parentItems
.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return parentItems.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
MainActivity is here.
public class MainActivity extends ExpandableListActivity {
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is not really necessary as ExpandableListActivity contains
// an ExpandableList
// setContentView(R.layout.main);
ExpandableListView expandableList = getExpandableListView(); // you
// can
// use
// (ExpandableListView)
// findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
childItems);
adapter.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setGroupParents() {
parentItems.add("Android");
parentItems.add("Core Java");
parentItems.add("Desktop Java");
parentItems.add("Enterprise Java");
}
public void setChildData() {
// Android
ArrayList<String> child = new ArrayList<String>();
child.add("Core");
child.add("Games");
childItems.add(child);
// Core Java
child = new ArrayList<String>();
child.add("Apache");
child.add("Applet");
child.add("AspectJ");
child.add("Beans");
child.add("Crypto");
childItems.add(child);
// Desktop Java
child = new ArrayList<String>();
child.add("Accessibility");
child.add("AWT");
child.add("ImageIO");
child.add("Print");
childItems.add(child);
// Enterprise Java
child = new ArrayList<String>();
child.add("EJB3");
child.add("GWT");
child.add("Hibernate");
child.add("JSP");
childItems.add(child);
}
}
At start open the groups you want to be fixed then implement this , specify the the groups postion
expandableList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
if(groupPosition==your group position){
return true; // This way the expander cannot be collapsed
}else{
return false;
}
}
});
Override onGroupCollapsed and onGroupExpanded of the ExpandableListView based on your needs.
EDITED: In addition: implement the mentioned setOnGroupClickListener, store the groupIDs within your view, and suppress the collapsing in onGroupCollapsed.