Im doing Dream app on Android. I have a ListView view containing a title and description that are stored in separate TextView. I want the titles to be displayed in the listView view and only after clicking one of them to display the description. After pressing another title, I want the previous description to collapse and show another, attached to the given title.
I want to my notes look this way:
Android dream
This is my adapter class where I think I need to add a few lines to solve my problem
class SennikListAdapter extends BaseAdapter implements Filterable {
private ArrayList<Sennik> mOriginalValues; // Original Values
private ArrayList<Sennik> mDisplayedValues; // Values to be displayed
LayoutInflater inflater;
public SennikListAdapter(Context context, ArrayList<Sennik> mProductArrayList) {
this.mOriginalValues = mProductArrayList;
this.mDisplayedValues = mProductArrayList;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mDisplayedValues.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
RelativeLayout llContainer;
TextView titleOfDream, descriptionOfDream;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.customlayout, null);
holder.llContainer = (RelativeLayout) convertView.findViewById(R.id.llContainer);
holder.titleOfDream = (TextView) convertView.findViewById(R.id.titleTextView);
holder.descriptionOfDream = (TextView) convertView.findViewById(R.id.descriptionTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleOfDream.setText(mDisplayedValues.get(position).getTitleOfDream());
holder.descriptionOfDream.setText(mDisplayedValues.get(position).getDescriptionOfDream() + "");
return convertView;
}
I wanted to do something like this in getView method, but there is an error "Veriable 'holder' is accessed from within inner class, needs to be declered final". If i change to final still doesn't work
holder.titleOfDream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.descriptionOfDream.getVisibility() == View.GONE)
{
//expandedChildList.set(arg2, true);
holder.descriptionOfDream.setVisibility(View.VISIBLE);
}
else
{
//expandedChildList.set(arg2, false);
holder.descriptionOfDream.setVisibility(View.GONE);
}
}
});
Do you have any ideas how to do this? I checked so many tutorials but didn't find, and im sure it isn't difficult :(
Maybe this would be helpful
this is Fragment class
public class SennikFragment extends Fragment {
private List<String> fileName;
private List<String> fileContent;
private ArrayList<Sennik> sen = new ArrayList<Sennik>();
private SennikListAdapter adapter;
private EditText editText;
private ListView listView;
private AssetManager assets;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sennik, container, false);
// inicjalizacja pól
fileName = new ArrayList<>();
fileContent = new ArrayList<>();
editText=(EditText) view.findViewById(R.id.searchFilter);
listView=(ListView) view.findViewById(R.id.listView);
assets = getActivity().getAssets();
//dodanie do pola wyszukiwania obiektu nasłuchującego zmian w tekście
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (adapter != null) {
adapter.getFilter().filter(s.toString());
} else {
Log.d("filter", "no filter availible");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
//uzupełnienie listy tytułów i opisów snu z folderu assets
try {
//Pobranie nazw wszystkich plików
String[] paths = assets.list("");
String text="";
/* Usunięcie z nazw plików ich rozszerzenia formatu */
for (String path : paths) {
if (path.equals("images") || path.equals("sounds") || path.equals("webkit"))
continue;
fileName.add(path.replace(".txt", " "));
InputStream is= assets.open(path);
int size=is.available();
byte[]buffer=new byte[size];
is.read(buffer);
is.close();
text=new String(buffer);
fileContent.add(text);
}
} catch (IOException ex) {
Log.e(TAG, "Bład podczas ładowania plików", ex);
}
//dodanie do listy z modelu Sennik tytułów i opisów snu
for(int i=0; i<fileName.size(); i++){
sen.add(new Sennik(fileName.get(i), fileContent.get(i)));
}
adapter=new SennikListAdapter((Context)getActivity(),sen);
listView.setAdapter(adapter);
return view;
}
}
this is description class
public class Sennik {
private String titleOfDream;
private String descriptionOfDream;
public Sennik(String titleOfDream, String descriptionOfDream) {
this.titleOfDream = titleOfDream;
this.descriptionOfDream = descriptionOfDream;
}
public String getTitleOfDream() {
return titleOfDream;
}
public void setTitleOfDream(String titleOfDream) {
this.titleOfDream = titleOfDream;
}
public String getDescriptionOfDream() {
return descriptionOfDream;
}
public void setDescriptionOfDream(String descriptionOfDream) {
this.descriptionOfDream = descriptionOfDream;
}
}
and my layouts:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/llContainer">
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:textColor="#color/colorAccent"
android:text="TextView" />
<TextView
android:id="#+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/titleTextView"
android:layout_centerHorizontal="true"
android:textColorHighlight="#color/colorPrimary"
android:textStyle="italic"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:text="TextView" />
<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">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/searchFilter"></EditText>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/searchFilter">
</ListView>
EDIT
problem solved!
I just add a line to my Adapter class to hide the description
holder.descriptionOfDream.setVisibility(View.GONE);
so the whole Adapter class look like:
class SennikListAdapter extends BaseAdapter implements Filterable {
private ArrayList<Sennik> mOriginalValues; // Original Values
private ArrayList<Sennik> mDisplayedValues; // Values to be displayed
LayoutInflater inflater;
public SennikListAdapter(Context context, ArrayList<Sennik> mProductArrayList) {
this.mOriginalValues = mProductArrayList;
this.mDisplayedValues = mProductArrayList;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mDisplayedValues.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
RelativeLayout llContainer;
TextView titleOfDream, descriptionOfDream;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.customlayout, null);
holder.llContainer = (RelativeLayout) convertView.findViewById(R.id.llContainer);
holder.titleOfDream = (TextView) convertView.findViewById(R.id.titleTextView);
holder.descriptionOfDream = (TextView) convertView.findViewById(R.id.descriptionTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleOfDream.setText(mDisplayedValues.get(position).getTitleOfDream());
holder.descriptionOfDream.setText(mDisplayedValues.get(position).getDescriptionOfDream() + "");
holder.descriptionOfDream.setVisibility(View.GONE);
return convertView;
}
And in Fragment class i use setOnItemClickListener method on listView (setOnClickListener doesn't work)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView descriptionOfDream= (TextView) view.findViewById(R.id.descriptionTextView);
if(descriptionOfDream.getVisibility()==view.GONE){
descriptionOfDream.setVisibility(view.VISIBLE);
}
else
{
descriptionOfDream.setVisibility(view.GONE);
}
}
});
So the whole Fragment class look like:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sennik, container, false);
// inicjalizacja pól
fileName = new ArrayList<>();
fileContent = new ArrayList<>();
editText=(EditText) view.findViewById(R.id.searchFilter);
listView=(ListView) view.findViewById(R.id.listView);
assets = getActivity().getAssets();
//dodanie do pola wyszukiwania obiektu nasłuchującego zmian w tekście
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (adapter != null) {
adapter.getFilter().filter(s.toString());
} else {
Log.d("filter", "no filter availible");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
//uzupełnienie listy tytułów i opisów snu z folderu assets
try {
//Pobranie nazw wszystkich plików
String[] paths = assets.list("");
String text="";
/* Usunięcie z nazw plików ich rozszerzenia formatu */
for (String path : paths) {
if (path.equals("images") || path.equals("sounds") || path.equals("webkit"))
continue;
fileName.add(path.replace(".txt", " "));
InputStream is= assets.open(path);
int size=is.available();
byte[]buffer=new byte[size];
is.read(buffer);
is.close();
text=new String(buffer);
fileContent.add(text);
}
} catch (IOException ex) {
Log.e(TAG, "Bład podczas ładowania plików", ex);
}
//dodanie do listy z modelu Sennik tytułów i opisów snu
for(int i=0; i<fileName.size(); i++){
sen.add(new Sennik(fileName.get(i), fileContent.get(i)));
}
adapter=new SennikListAdapter((Context)getActivity(),sen);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView descriptionOfDream= (TextView) view.findViewById(R.id.descriptionTextView);
if(descriptionOfDream.getVisibility()==view.GONE){
descriptionOfDream.setVisibility(view.VISIBLE);
}
else
{
descriptionOfDream.setVisibility(view.GONE);
}
}
});
return view;
}
}
Works excelent <3
First you need to create a class Extending LinearLayout and give some Expanding/collapsing
capabilities to It. Then define your description Textview as a child of that.
public class ExpandableLinearLayout extends LinearLayout {
private boolean expanded;
private int duration;
public ExpandableLinearLayout(Context context) {
super(context);
}
public ExpandableLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ExpandableLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ExpandableLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(AttributeSet attributeSet) {
TypedArray customValues = getContext().obtainStyledAttributes(attributeSet, R.styleable.ExpandableLinearLayout);
duration = customValues.getInt(R.styleable.ExpandableLinearLayout_expandDuration, -1);
customValues.recycle();
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public void toggle() {
if (expanded)
expandView(this);
else
hideView(this);
}
private void expandView(final View view) {
view.measure(WindowManager.LayoutParams.MATCH_PARENT
, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
final int targetHeight = view.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
view.getLayoutParams().height = 1;
view.setVisibility(View.VISIBLE);
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = interpolatedTime == 1
? targetHeight : (int) (targetHeight * interpolatedTime);
view.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
if (duration == -1)
a.setDuration((int) (targetHeight / view.getContext().getResources().getDisplayMetrics().density * 1.5));
else
a.setDuration(duration);
view.startAnimation(a);
}
private void hideView(final View view) {
final int initialHeight = view.getMeasuredHeight();
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.setVisibility(View.GONE);
} else {
view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
view.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
if (duration == -1)
a.setDuration((int) (initialHeight / view.getContext().getResources().getDisplayMetrics().density * 1.5));
else
a.setDuration(duration);
view.startAnimation(a);
}
}
Just Inflate this class into your xml layout file and create descriotion textView inside of it.
Add The following Code snippet into your Adapter class to call that when An Item gets a Click. It will Expand/Collapse the LinearLayout with an Animaiton.
private void initializeClicks() {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (expandableLinearLayout.isExpanded()) {
expandableLinearLayout.setExpanded(false);
expandableLinearLayout.toggle();
orderList.get(getAdapterPosition()).setExpanded(false);
} else {
expandableLinearLayout.setExpanded(true);
orderList.get(getAdapterPosition()).setExpanded(true);
expandableLinearLayout.toggle();
if(lastExpandedCardPosition!=getAdapterPosition() && recyclerView.findViewHolderForAdapterPosition(lastExpandedCardPosition)!=null){
((ExpandableLinearLayout)recyclerView.findViewHolderForAdapterPosition(lastExpandedCardPosition).itemView.findViewById(R.id.expandedLinearLayout)).setExpanded(false);
orderList.get(lastExpandedCardPosition).setExpanded(false);
((ExpandableLinearLayout)recyclerView.findViewHolderForAdapterPosition(lastExpandedCardPosition).itemView.findViewById(R.id.expandedLinearLayout)).toggle();
}
else if(lastExpandedCardPosition!=getAdapterPosition() && recyclerView.findViewHolderForAdapterPosition(lastExpandedCardPosition)==null){
orderList.get(lastExpandedCardPosition).setExpanded(false);
}
lastExpandedCardPosition = getAdapterPosition();
}
}
});
}
Finally call the Method above in your Clicklistenr like this:
holder.titleOfDream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.descriptionOfDream.getVisibility() == View.GONE)
{
expandableLinearLayout.setVisibility(View.VISIBLE);
expandableLinearLayout.setExpanded(true);
}
else
{
expandableLinearLayout.setVisibility(View.GONE);
expandableLinearLayout.setExpanded(false);
}
}
});
For a more detailed Explanation about this Method see this post on Medium.com.
And dont forget to give a clap to this post.
You have to call this method in your code`
holder.descriptionOfDream.requestLayout();
So basically, your new code looks like this
holder.titleOfDream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.descriptionOfDream.getVisibility() == View.GONE)
{
//expandedChildList.set(arg2, true);
holder.descriptionOfDream.setVisibility(View.VISIBLE);
holder.descriptionOfDream.requestLayout();
}
else
{
//expandedChildList.set(arg2, false);
holder.descriptionOfDream.setVisibility(View.GONE);
holder.descriptionOfDream.requestLayout();
}
}
});
Hope that helps.
Keep in mind that the holder variable must be final like below
final ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.customlayout, null);
holder.llContainer = (RelativeLayout) convertView.findViewById(R.id.llContainer);
holder.titleOfDream = (TextView) convertView.findViewById(R.id.titleTextView);
holder.descriptionOfDream = (TextView) convertView.findViewById(R.id.descriptionTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleOfDream.setText(mDisplayedValues.get(position).getTitleOfDream());
holder.descriptionOfDream.setText(mDisplayedValues.get(position).getDescriptionOfDream() + "");
return convertView;
Related
I have problem with Exoplayer to stopping after the app is closed down, or the fragment is changed from the BottomNavigationView, I get the data from my API that is cast to a Recyclerview/Adapter that handle the data, then the data with the images and video is gettting showed with a PagerAdapter.
I also tried in the fragment to set a onDestroy, but not working.
FeedAdapter.java
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private final List<FeedData> feedList;
private Activity context;
private View.OnClickListener onClickListener;
private SimpleExoPlayer videoPlayer;
REST apiService =
RestClient.getClient().create(REST.class);
public FeedAdapter(Activity context, List<FeedData> feedList) {
this.context = context;
this.feedList = feedList;
this.onClickListener = onClickListener;
}
public List<FeedData> getFeedData(){
return feedList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_feed_photo, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.mBinding.setFeed(feedList.get(position));
Boolean isLiked = holder.mBinding.getFeed().getIsLiked();
Boolean isSaved = holder.mBinding.getFeed().getIsSaved();
String username = holder.mBinding.getFeed().getUsername();
if (holder.mBinding.getFeed().getAvatar().length() > 0) {
Glide.with(context).load(holder.mBinding.getFeed().getAvatar())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(holder.mBinding.userAvatar);
}
holder.mBinding.username.setText(username);
holder.mBinding.description.setHashtagModeColor(ContextCompat.getColor(context, R.color.fitnessm8_green));
holder.mBinding.description.addAutoLinkMode(AutoLinkMode.MODE_HASHTAG, AutoLinkMode.MODE_MENTION);
holder.mBinding.description.setAutoLinkText(holder.mBinding.getFeed().getDescription());
holder.mBinding.likecount.setText(holder.mBinding.getFeed().getLikes().toString());
holder.mBinding.likecounttext.setText(context.getResources().getString(R.string.likes));
holder.mBinding.commentcount.setText(holder.mBinding.getFeed().getComments().size() + " " + context.getResources().getString(R.string.comments));
holder.mBinding.viewMoreComment.setText(context.getResources().getString(R.string.view) + " " + holder.mBinding.getFeed().getComments().size() + " " + context.getResources().getString(R.string.comments));
holder.mBinding.viewMoreComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
if (isLiked) {
holder.mBinding.like.setLiked(true);
} else {
holder.mBinding.like.setLiked(false);
}
if (isSaved) {
holder.mBinding.fav.setLiked(true);
} else {
holder.mBinding.fav.setLiked(false);
}
if (holder.mBinding.getFeed().getType().equals("image")) {
if (holder.mBinding.getFeed().getMediaSet().size() > 1) {
holder.mBinding.image.setVisibility(View.GONE);
MultiMediaAdapter pagerAdapter = new MultiMediaAdapter(context, holder.mBinding.getFeed().getMediaSet());
holder.mBinding.viewpagerId.setAdapter(pagerAdapter);
ScrollingPagerIndicator pagerIndicator = holder.mBinding.indicator;
pagerIndicator.attachToPager(holder.mBinding.viewpagerId);
} else {
holder.mBinding.viewpagerId.setVisibility(View.GONE);
Glide.with(context).load(holder.mBinding.getFeed().getMediaSet().get(0).getFile())
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_placeholder)
.into(holder.mBinding.image);
}
}
if (holder.mBinding.getFeed().getType().equals("video")) {
holder.mBinding.image.setVisibility(View.GONE);
MultiMediaAdapter pagerAdapter = new MultiMediaAdapter(context, holder.mBinding.getFeed().getMediaSet());
holder.mBinding.viewpagerId.setAdapter(pagerAdapter);
ScrollingPagerIndicator pagerIndicator = holder.mBinding.indicator;
pagerIndicator.attachToPager(holder.mBinding.viewpagerId);
}
holder.mBinding.clickUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, UserProfileActivity.class);
i.putExtra("profileUserID", holder.mBinding.getFeed().getUserId());
view.getContext().startActivity(i);
}
});
}
#Override
public int getItemCount() {
return feedList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ListItemFeedPhotoBinding mBinding;
public ViewHolder(View itemView) {
super(itemView);
mBinding = DataBindingUtil.bind(itemView);
}
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
MultiMediaAdapter.java
public class MultiMediaAdapter extends PagerAdapter {
private PlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private ImageView image;
Context context;
List<FeedMediaSet> Media_list;
public MultiMediaAdapter(Context context, List<FeedMediaSet> Media_list) {
this.context = context;
this.Media_list = Media_list;
}
#Override
public int getCount() {
return Media_list.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
FeedMediaSet mediaObj = Media_list.get(position);
if (mediaObj.getType().equals("image")) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item_media_photo,container,false);
image = view.findViewById(R.id.image);
Glide.with(context).load(mediaObj.getFile())
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_placeholder)
.into(image);
container.addView(view);
return view;
} else if (mediaObj.getType().equals("video")) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item_media_video,container,false);
simpleExoPlayerView = view.findViewById(R.id.playerView);
image = view.findViewById(R.id.image);
image.setVisibility(View.GONE);
initializePlayer(mediaObj.getFile(), mediaObj.getExtra());
container.addView(view);
return view;
}
return null;
}
private void initializePlayer(String uri, String extra) {
player = ExoPlayerFactory.newSimpleInstance(context);
try {
Uri mp4VideoExtraUri = Uri.parse(uri);
InputStream inputStream = context.getContentResolver().openInputStream(mp4VideoExtraUri);
Drawable yourDrawable = Drawable.createFromStream(inputStream, extra);
simpleExoPlayerView.setDefaultArtwork(yourDrawable);
} catch (FileNotFoundException e) {
Drawable yourDrawable = context.getResources().getDrawable(R.drawable.image_attach);
simpleExoPlayerView.setDefaultArtwork(yourDrawable);
}
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getResources().getString(R.string.app_name)));
MediaSource videoSource = new ProgressiveMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(uri));
player.setPlayWhenReady(false);
player.prepare(videoSource);
if (simpleExoPlayerView != null) simpleExoPlayerView.setPlayer(player);
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
releasePlayer();
}
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
simpleExoPlayerView.setPlayer(null);
}
}
}
When scroll recyclerview some items mixes. After I add ads after every 15 items, holder get wrong data. Some items are vip items. I will change background color of these items. But when I scroll it dublicates mixes. How can I solve?
This is my adapter
private Context mCtx;
private List<Car> carList;
private RecyclerViewAnimator mAnimator;
private int AD_TYPE=1;
private int CONTENT_TYPE=2;
private int LIST_AD_DELTA=15;
public ProductAllCarAdapter(RecyclerView recyclerView,Context mCtx, List<Car> carList) {
this.mCtx = mCtx;
this.carList = carList;
mAnimator = new RecyclerViewAnimator(recyclerView);
}
#Override
public ProductAllCarAdapter.ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == AD_TYPE){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_add_item, null);
ProductAllCarAdapter.ProductViewHolder vh = new ProductAllCarAdapter.ProductViewHolder(itemView);
mAnimator.onCreateViewHolder(itemView);
return vh;
} else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_car_item, null);
ProductAllCarAdapter.ProductViewHolder vh = new ProductAllCarAdapter.ProductViewHolder(itemView);
mAnimator.onCreateViewHolder(itemView);
return vh;
}
}
#Override
public int getItemViewType(int position) {
if (position>0 && position % LIST_AD_DELTA == 0)
return AD_TYPE;
return CONTENT_TYPE;
}
#Override
public void onBindViewHolder(ProductAllCarAdapter.ProductViewHolder holder, int position) {
if (getItemViewType(position) == CONTENT_TYPE) {
final Car car = carList.get(holder.getAdapterPosition());
GlideApp.with(mCtx).load(car.getImg()).into(holder.imageView);
if (car.getVip() == 1) {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorVip));
holder.imageViewVIP.setVisibility(View.VISIBLE);
}
final String carid = String.valueOf(car.getCarid());
mAnimator.onBindViewHolder(holder.itemView, position);
} else {
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context mcontext = view.getContext();
Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(mcontext, android.R.anim.fade_in, android.R.anim.fade_out).toBundle();
Intent intent = new Intent(mcontext, AdsItem.class);
mcontext.startActivity(intent, bundle);
}
});
mAnimator.onBindViewHolder(holder.itemView, position);
}
}
private int getRealPosition(int position) {
if (LIST_AD_DELTA == 0) {
return position;
} else {
return position - position / LIST_AD_DELTA;
}
}
#Override
public long getItemId(int position) { return position; }
#Override
public int getItemCount() {
int additionalContent = 0;
if (carList.size() > 0 && carList.size() > LIST_AD_DELTA) {
additionalContent = ( carList.size() / LIST_AD_DELTA);
}
return carList.size() + additionalContent;
}
public static class ProductViewHolder extends RecyclerView.ViewHolder {
private View mView;
ImageView imageView, imageViewVIP;
RelativeLayout relativeLayout;
public ProductViewHolder(View itemView) {
super(itemView);
mView = itemView;
imageView = itemView.findViewById(R.id.imageView);
imageViewVIP = itemView.findViewById(R.id.imageViewVIP);
relativeLayout = itemView.findViewById(R.id.relativeLayoutpc);
}
public void setOnClickListener(View.OnClickListener listener) {
mView.setOnClickListener(listener);
}
}
I think problem onBindViewHolder function use wrong holder. ArrayList also return true value but on scroll it mixes.
You just have to add the corresponding else of the following if statement block.
if (car.getVip() == 1) {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorVip));
holder.imageViewVIP.setVisibility(View.VISIBLE);
} else {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorNormal));
holder.imageViewVIP.setVisibility(View.GONE);
}
This is inside your onBindViewHolder function where the view type is CONTENT_TYPE.
Hope that solves your problem.
I am a newbie in android app development and I am following this tutorial to create an image gallery.
https://deepshikhapuri.wordpress.com/2017/03/20/get-all-images-from-gallery-in-android-programmatically/.
It's working perfectly but now I want to display images in ViewPager for being able to display images by swiping left/right.I am using an adapter for doing it.
I have tried doing it but its not working I dont know why.I am not able to swipe images. Can anyone help me with it ? Am i using correct path for the images?
This project is available on github here :
https://github.com/avi003/MyApp0-master-master
ImageGallery.java:
public class ImageGallery extends AppCompatActivity {
public static ArrayList<Model_images> al_images = new ArrayList<>();
boolean boolean_folder;
Adapter_PhotosFolder obj_adapter;
GridView gv_folder;
private static final int REQUEST_PERMISSIONS = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
gv_folder = (GridView)findViewById(R.id.gv_folder);
gv_folder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), PhotosActivity.class);
intent.putExtra("value",i);
startActivity(intent);
}
});
if ((ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(ImageGallery.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) && (ActivityCompat.shouldShowRequestPermissionRationale( ImageGallery.this,
Manifest.permission.READ_EXTERNAL_STORAGE))) {
} else {
ActivityCompat.requestPermissions( ImageGallery.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
}else {
Log.e("Else","Else");
fn_imagespath();
}
}
public ArrayList<Model_images> fn_imagespath() {
al_images.clear();
int int_position = 0;
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
String absolutePathOfImage;
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
Log.e("Column", absolutePathOfImage);
Log.e("Folder", cursor.getString(column_index_folder_name));
for (int i = 0; i < al_images.size(); i++) {
if (al_images.get(i).getStr_folder().equals(cursor.getString(column_index_folder_name))) {
boolean_folder = true;
int_position = i;
break;
} else {
boolean_folder = false;
}
}
if (boolean_folder) {
ArrayList<String> al_path = new ArrayList<>();
al_path.addAll(al_images.get(int_position).getAl_imagepath());
al_path.add(absolutePathOfImage);
al_images.get(int_position).setAl_imagepath(al_path);
} else {
ArrayList<String> al_path = new ArrayList<>();
al_path.add(absolutePathOfImage);
Model_images obj_model = new Model_images();
obj_model.setStr_folder(cursor.getString(column_index_folder_name));
obj_model.setAl_imagepath(al_path);
al_images.add(obj_model);
}
}
for (int i = 0; i < al_images.size(); i++) {
Log.e("FOLDER", al_images.get(i).getStr_folder());
for (int j = 0; j < al_images.get(i).getAl_imagepath().size(); j++) {
Log.e("FILE", al_images.get(i).getAl_imagepath().get(j));
}
}
obj_adapter = new Adapter_PhotosFolder(getApplicationContext(),al_images);
gv_folder.setAdapter(obj_adapter);
return al_images;
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
fn_imagespath();
} else {
Toast.makeText(ImageGallery.this, "The app was not allowed to read or write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
}
}
}
}
}
}
PhotosActivity.java:
public class PhotosActivity extends AppCompatActivity {
int int_position;
private GridView gridView;
GridViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_gallery);
gridView = (GridView)findViewById(R.id.gv_folder);
int_position = getIntent().getIntExtra("value", 0);
adapter = new GridViewAdapter(this, al_images,int_position);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
i.putExtra("abc",abc);
startActivity(i);
}
});
}
}
GridViewAdapter.java:
public class GridViewAdapter extends ArrayAdapter<Model_images> {
Context context;
ViewHolder viewHolder;
ArrayList<Model_images> al_menu = new ArrayList<>();
int int_position;
public GridViewAdapter(Context context, ArrayList<Model_images> al_menu,int int_position) {
super(context, R.layout.activity_adapter__photos_folder, al_menu);
this.al_menu = al_menu;
this.context = context;
this.int_position = int_position;
}
#Override
public int getCount() {
Log.e("ADAPTER LIST SIZE", al_menu.get(int_position).getAl_imagepath().size() + "");
return al_menu.get(int_position).getAl_imagepath().size();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
if (al_menu.get(int_position).getAl_imagepath().size() > 0) {
return al_menu.get(int_position).getAl_imagepath().size();
} else {
return 1;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_foldern.setVisibility(View.GONE);
viewHolder.tv_foldersize.setVisibility(View.GONE);
Glide.with(context).load("file://" + al_menu.get(int_position).getAl_imagepath().get(position))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(viewHolder.iv_image);
return convertView;
}
private static class ViewHolder {
TextView tv_foldern, tv_foldersize;
ImageView iv_image;
}
}
Adapter_PhotosFolder.java:
public class Adapter_PhotosFolder extends ArrayAdapter<Model_images> {
Context context;
ViewHolder viewHolder;
ArrayList<Model_images> al_menu = new ArrayList<>();
public Adapter_PhotosFolder(Context context, ArrayList<Model_images> al_menu) {
super(context, R.layout.activity_adapter__photos_folder, al_menu);
this.al_menu = al_menu;
this.context = context;
}
#Override
public int getCount() {
Log.e("ADAPTER LIST SIZE", al_menu.size() + "");
return al_menu.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
if (al_menu.size() > 0) {
return al_menu.size();
} else {
return 1;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_foldern.setText(al_menu.get(position).getStr_folder());
viewHolder.tv_foldersize.setText(al_menu.get(position).getAl_imagepath().size()+"");
Glide.with(context).load("file://" + al_menu.get(position).getAl_imagepath().get(0))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(viewHolder.iv_image);
return convertView;
}
private static class ViewHolder {
TextView tv_foldern, tv_foldersize;
ImageView iv_image;
}
}
Model_images.java:
public class Model_images {
String str_folder;
ArrayList<String> al_imagepath;
public String getStr_folder() {
return str_folder;
}
public void setStr_folder(String str_folder) {
this.str_folder = str_folder;
}
public ArrayList<String> getAl_imagepath() {
return al_imagepath;
}
public void setAl_imagepath(ArrayList<String> al_imagepath) {
this.al_imagepath = al_imagepath;
}
}
FullImageActivity.java:
public class FullImageActivity extends AppCompatActivity {
ImageView images;
int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent i = getIntent();
images = (ImageView) findViewById(R.id.fullImage);
// Selected image id
position = i.getExtras().getInt("id");
Bundle extras = getIntent().getExtras();
String value = extras.getString("abc");
Glide.with(FullImageActivity.this)
.load(value)
.skipMemoryCache(false)
.into(images);
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new TouchImageAdapter(this,al_images));
}
}
activity_full_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/jazzy_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/fullImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
TouchImageAdapter.java:
class TouchImageAdapter extends PagerAdapter {
Context context;
String filename;
ArrayList<Model_images> al_menu = new ArrayList<>();
int position,int_position;
public TouchImageAdapter(Context context,ArrayList<Model_images> al_menu){
this.al_menu = al_menu;
this.context = context;
}
#Override
public int getCount() {
return al_menu.size();
}
#Override
public View instantiateItem(ViewGroup container, int position) {
ImageView img = new ImageView(container.getContext());
img.setImageDrawable(getImageFromSdCard(filename,position));
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public Drawable getImageFromSdCard(String imageName,int position) {
Drawable d = null;
try {
String path = al_menu.get(int_position).getAl_imagepath().get(position)
+ "/";
Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
+ ".jpeg");
d = new BitmapDrawable(context.getResources(),bitmap);
} catch (IllegalArgumentException e) {
// TODO: handle exception
}
return d;
}
}
Here you are not passing count of images inside that folder, So in TouchImageAdapter you have passing the count of folder like :
al_menu.size() // it always give you size of folder,
al_menu.get(int_position).getAl_imagepath().size();
// passing the folder position so that correct folder images are to be shown.
public TouchImageAdapter(Context context,ArrayList<Model_images> al_menu, int position){
this.al_menu = al_menu;
this.context = context;
this.int_position = position;
}
#Override
public int getCount() {
return al_menu.get(int_position).getAl_imagepath().size();
}
Here is the updated code I pushed on
https://github.com/redviper00/game
try this
String[] filenames = new String[0];
File path = new File(Environment.getExternalStorageDirectory() + "/your folder");
if (path.exists()) {
filenames = path.list();
}
for (int i = 0; i < filenames.length; i++) {
imagesPathArrayList.add(path.getPath() + "/" + filenames[i]);
Log.e("FAV_Images", imagesPathArrayList.get(i));
adapter = new ImageAdapter(FavActivity.this, imagesPathArrayList);
myviewpager.setAdapter(adapter);
}
create adapter class like this
public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> arrayList;
ImageAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
viewPagerImageView = new ImageView(context);
viewPagerImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap bitmap = BitmapFactory.decodeFile(arrayList.get(position));
viewPagerImageView.setImageBitmap(bitmap);
((ViewPager) container).addView(viewPagerImageView, 0);
return viewPagerImageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
It seems like you are showing Imageview above view pager
images = (ImageView) findViewById(R.id.fullImage);
that is why when you are trying to swipe imageview handle your tocuh action instead of view pager. Remove it.
I'm in a bind here, I wanted to integrate OnItemClickListener in my CustomAdapter, so when an item is click, it'll show some message or something. But I am not sure where to set it. I've look at bunch of tutorials and some post here but it only make me more confuse. So I need guidance on how and where would I need to set it to make it work. Here is my code:
MainActivity.java
ListView listView;
listView = (ListView) findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();
ReadRSS.java
#Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (feedItems != null) {
CustomAdapter customAdapter = new CustomAdapter(context,R.layout.activity_listview,feedItems);
listView.setAdapter(customAdapter);
}
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
}
Here is not the direct answer to your question, but my own code example so you can have a look at it and get the idea;
ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listword,R.id.wordView1, companies);
ListView listView = (ListView) findViewById(R.id.secondListView1);
listView.setAdapter(adapter);
// Item Click listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> ok, View v, int position, long id){
// Do your stuff here
}
});
You don't set it inside the adapter, but inside your activity where you define you ListView :
ListView listView = (ListView)findViewById(R.id.list_item);
new ReadRSS(MainActivity.this, listView, "http://malaysiakini.com/en/news.rss").execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code here
}
}
You can perform task on individual component of CustomAdapter class. Check below code.
public class CustomAdapter extends ArrayAdapter<FeedItem> {
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
tt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform your task
});
tt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform your task
});
}
return v;
}
}
Just like this,hope it helps:
public class CustomAdapter extends ArrayAdapter<FeedItem> implements OnClickListener{
private Context mContext;
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
}
public CustomAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.activity_listview, null);
}
FeedItem p = getItem(position);
v.setTag(p);
v.setOnClickListener(this);
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.title_text);
TextView tt2 = (TextView) v.findViewById(R.id.date_text);
if (tt1 != null) {
tt1.setText(p.getTitle());
}
if (tt2 != null) {
tt2.setText(p.getPubDate());
}
}
return v;
}
#Override
public void onClick(View v){
FeedItem p = (FeedItem) v.getTag();
// TODO,do something with p and mContext
}
}
But I think you should use listview.setOnItemClickListener(listener) to do this work.
Write this code on your activity below of your set adapter method.
Set Adapter Method
private void setAdapter() {
try {
DoctorAppointmentListAdapter doctorAppointmentListAdapter = new DoctorAppointmentListAdapter(getContext(), doctorAppointmentObjectArrayList, selectedItemClickListener, screen);
appointmentListView.setAdapter(doctorAppointmentListAdapter);
}catch (Exception e){}
}
Costume Click Methods
//ListAdapter Click Listener
DoctorAppointmentListAdapter.SelectItemClickListener selectedItemClickListener = new DoctorAppointmentListAdapter.SelectItemClickListener() {
#Override
public void itemClickedAtIndex(int index, String operation) {
if (operation.equalsIgnoreCase("00")) {
m_handler.removeCallbacks(m_handlerTask);
viewDetailsScreen(index);
} else if (operation.equalsIgnoreCase("11")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("33")) {
m_handler.removeCallbacks(m_handlerTask);
//Video Call Operation
patirntId = doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id();
videocallCompletedService(doctorAppointmentObjectArrayList.get(index).getAppointment_patient_id(), doctorAppointmentObjectArrayList.get(index).getAppointment_id());
} else if (operation.equalsIgnoreCase("22")) {
m_handler.removeCallbacks(m_handlerTask);
//Phone Call Operation
if (doctorAppointmentObjectArrayList.get(index).getPatient_contact_no().length() > 0) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + doctorAppointmentObjectArrayList.get(index).getPatient_contact_no()));
startActivity(intent);
} else {
}
}
}
};
This is Adapter Class
public class DoctorAppointmentListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private Context context;
private int screen;
private ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList;
private SelectItemClickListener itemClickListener;
public DoctorAppointmentListAdapter(Context context, ArrayList<DoctorAppointmentObject> doctorAppointmentObjectArrayList, SelectItemClickListener listener, int screen) {
this.context = context;
this.screen = screen;
this.doctorAppointmentObjectArrayList = doctorAppointmentObjectArrayList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
if (itemClickListener != null) {
itemClickListener = null;
}
itemClickListener = listener;
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getCount() {
return doctorAppointmentObjectArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
public interface SelectItemClickListener {
public void itemClickedAtIndex(int index, String operation);
}
private class ViewHolder {
private TextView doctorNameTextView, numberTextView, datetimeTextView, enteryTextView;
private TextView viewDetailTextView, videoCallTextView, phoneCallTextView;
private CircleImageView profile_image;
private ImageView onlineStatusImageView;
private CircularProgressView progress_view;
}
ViewHolder holder = null;
#Override
public View getView(final int i, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.adapter_doctor_appointment_list, parent, false);
holder = new ViewHolder();
holder.doctorNameTextView = (TextView) convertView
.findViewById(R.id.doctorNameTextView);
holder.numberTextView = (TextView) convertView
.findViewById(R.id.numberTextView);
holder.datetimeTextView = (TextView) convertView
.findViewById(R.id.datetimeTextView);
holder.enteryTextView = (TextView) convertView
.findViewById(R.id.enteryTextView);
holder.viewDetailTextView = (TextView) convertView
.findViewById(R.id.viewDetailTextView);
holder.videoCallTextView = (TextView) convertView
.findViewById(R.id.videoCallTextView);
holder.phoneCallTextView = (TextView) convertView
.findViewById(R.id.phoneCallTextView);
holder.profile_image = (CircleImageView) convertView
.findViewById(R.id.profile_image);
holder.progress_view = (CircularProgressView) convertView.findViewById(R.id.progress_view);
holder.onlineStatusImageView = (ImageView) convertView.findViewById(R.id.onlineStatusImageView);
holder.onlineStatusImageView.setVisibility(View.INVISIBLE);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (screen == 1) {
holder.videoCallTextView.setVisibility(View.GONE);
}
try {
Picasso.with(context).load(doctorAppointmentObjectArrayList.get(i).getPatient_profile_pic()).into(holder.profile_image, new Callback() {
#Override
public void onSuccess() {
holder.progress_view.setVisibility(View.GONE);
}
#Override
public void onError() {
holder.progress_view.setVisibility(View.GONE);
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
}
});
} catch (Exception e) {
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
holder.progress_view.setVisibility(View.GONE);
e.printStackTrace();
}
holder.doctorNameTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_display_name());
holder.numberTextView.setText(doctorAppointmentObjectArrayList.get(i).getPatient_contact_no());
if (doctorAppointmentObjectArrayList.get(i).getAppointment_time().equalsIgnoreCase("")) {
holder.numberTextView.setText("");
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date testDate = null;
try {
testDate = sdf.parse(doctorAppointmentObjectArrayList.get(i).getAppointment_time());
} catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy hh:mm a");
String newFormat = formatter.format(testDate);
holder.numberTextView.setText("" + newFormat);
}
holder.datetimeTextView.setText(doctorAppointmentObjectArrayList.get(i).getAppointment_reason());
if (screen==0){
holder.enteryTextView.setText("Entry Time :" + doctorAppointmentObjectArrayList.get(i).getAppointment_entry_time());
}else {
holder.enteryTextView.setText("");
}
holder.viewDetailTextView.setId(i);
holder.viewDetailTextView.setOnClickListener(viewDetailSelectedListener);
holder.videoCallTextView.setId(i);
holder.videoCallTextView.setOnClickListener(videoCallSelectedListener);
holder.phoneCallTextView.setId(i);
holder.phoneCallTextView.setOnClickListener(phoneCallSelectedListener);
return convertView;
}
View.OnClickListener viewDetailSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "00");
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener videoCallSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
if (screen == 2) {
itemClickListener.itemClickedAtIndex(index, "33");
} else {
itemClickListener.itemClickedAtIndex(index, "11");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
View.OnClickListener phoneCallSelectedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int index = (Integer) v.getId();
itemClickListener.itemClickedAtIndex(index, "22");
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
I Declared
HeaderListView list;
I try to implement search bar. all are fine but this line show error. I am not using listview I used list
here is the line
list.setTextFilterEnabled(true);
What I have to do here?..
Thanks in advance
list = new HeaderListView(this);
list.setAdapter(new SectionAdapter() {
#Override
public int numberOfSections() {
return aStatesDetails.size();
}
#Override
public int numberOfRows(int section) {
return aStatesDetails.get(section).getCoursesDetails().size();
}
#Override
public Object getRowItem(int section, int row) {
return null;
}
#Override
public boolean hasSectionHeaderView(int section) {
return true;
}
#Override
public View getRowView(int section, int row, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null ) {
//Log.i("section Details","value : "+ section + " data : " + si + " view created");
convertView = SearchGolfCourseActivity.this.getLayoutInflater().inflate(R.layout.searchcourse_adapter, null);
holder = new ViewHolder();
holder.mTextView_CourseName = (TextView) convertView.findViewById(R.id.mTextView_CourseName);
holder.mTextView_Update = (TextView) convertView.findViewById(R.id.mTextView_Update);
holder.mTextView_CourseName.setPadding(dScreenSizeWidth/50, dScreenSizeHeight/50, dScreenSizeWidth/50, dScreenSizeHeight/50);
holder.mTextView_Update.setPadding(0, dScreenSizeHeight/50, dScreenSizeWidth/50, dScreenSizeHeight/50);
/*holder.mTextView_Update.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
int getSection = (Integer) view.getTag(R.id.mTextView_CourseName);
int getPosition = (Integer) view.getTag(R.id.mTextView_Update);
Log.v("Item Clicked", getSection + " " + getPosition);
}
});*/
convertView.setTag(holder);
convertView.setTag(R.id.mTextView_CourseName, holder.mTextView_CourseName);
convertView.setTag(R.id.mTextView_Update, holder.mTextView_Update);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(holder.mTextView_CourseName != null)
{
holder.mTextView_CourseName.setText(aStatesDetails.get(section).getCoursesDetails().get(row).getCourseName());
holder.mTextView_CourseName.setTag(aStatesDetails.get(section).getCoursesDetails().get(row).getCourseId());
}
if(holder.mTextView_Update != null)
if(aStatesDetails.get(section).getCoursesDetails().get(row).getDownloadStatus())
{
holder.mTextView_CourseName.setTextColor(Color.parseColor("#FF0000"));
holder.mTextView_Update.setVisibility(View.VISIBLE);
holder.mTextView_Update.setText("Update");
holder.mTextView_Update.setTag(R.id.mTextView_CourseName, section);
holder.mTextView_Update.setTag(R.id.mTextView_Update, row);
}
else
{
holder.mTextView_CourseName.setTextColor(Color.parseColor("#000000"));
holder.mTextView_Update.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getSectionHeaderViewTypeCount() {
return 2;
}
#Override
public int getSectionHeaderItemViewType(int section) {
return section % 2;
}
#Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (TextView) getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
}
((TextView) convertView).setText(aStatesDetails.get(section).getStateName());
convertView.setBackgroundColor(getResources().getColor(R.color.holo_brown_light));
return convertView;
}
#Override
public void onRowItemClick(AdapterView<?> parent, View view, final int section, final int row, long id) {
super.onRowItemClick(parent, view, section, row, id);
boolean isDownloaded = aStatesDetails.get(section).getCoursesDetails().get(row).getDownloadStatus();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchGolfCourseActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Info");
// Setting Dialog Message
if(isDownloaded)
alertDialog.setMessage("This course has been already downloaded. Do you want to update this course?");
else
alertDialog.setMessage("Do you want to download this course?");
// Setting alert dialog icon
alertDialog.setIcon(R.drawable.tab_info_light);
// On pressing Yes button
alertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//StandardRouteFinderActivity.mapObject.redrawMap();
dialog.cancel();
String courseId = aStatesDetails.get(section).getCoursesDetails().get(row).getCourseId();
String courseName = aStatesDetails.get(section).getCoursesDetails().get(row).getCourseName();
new MyAsyncTaskDownloadCourse().execute(courseId, courseName);
}
});
// on pressing No button
alertDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
Toast.makeText(SearchGolfCourseActivity.this, "Section " + section + " Row " + row, Toast.LENGTH_SHORT).show();
}
});
list.setTextFilterEnabled(true);
mCourseListRelativeLayout.addView(list);
As it pointed in official documentation your adapter should implements Filterable interface, only than you can use list.setTextFilterEnabled(true). Does your SectionAdapter implement it? If not, try to do it.
I am using through edit text,hope this will help you !
inputSearch = (EditText)findViewById(R.id.inputSearch);
ListView lv = getListView();
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
youractivityname.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Use Edittext for Filter data from Listview.
here what i have done in my Adapter class and in my activity to filter data:
Adapter Class
public class FindAProfessorAdapter extends ArrayAdapter<Data> implements
Filterable {
int layoutResourceId;
LayoutInflater mInflater;
ArrayList<Data> list;
ArrayList<Data> secondlist;
Context context;
Data data;
public FindAProfessorAdapter(Context context, int layoutResourceId,
ArrayList<Data> data) {
super(context, layoutResourceId, data);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = new ArrayList<Data>();
secondlist = new ArrayList<Data>();
secondlist.addAll(data);
list.addAll(secondlist);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.list = data;
getFilter();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView
.findViewById(R.id.findproname);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(list.get(position).getName());
return convertView;
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
list = (ArrayList<Data>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0; i < list.size(); i++) {
add(list.get(i));
notifyDataSetChanged();
}
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Data> FilteredArrList = new ArrayList<Data>();
if (secondlist == null) {
secondlist = new ArrayList<Data>(list);
}
if (constraint != null && constraint.toString().length() != 0) {
constraint = constraint.toString().toLowerCase(
Locale.getDefault());
for (int i = 0; i < list.size(); i++) {
Data name = secondlist.get(i);
if (name.getName().toLowerCase(Locale.getDefault())
.contains(constraint)) {
FilteredArrList.add(name);
}
}
filterResults.count = FilteredArrList.size();
filterResults.values = FilteredArrList;
} else {
synchronized (this) {
filterResults.count = secondlist.size();
filterResults.values = secondlist;
}
}
return filterResults;
}
};
return filter;
}
class ViewHolder {
TextView textView;
}
}
In my Activity:
editText = (EditText) view.findViewById(R.id.findaprofessor);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
You need to use this as a example and edit your adapter according to this.