I have many ListActivity classes in my app that act pretty much the same : list with a property (TextView) on the left and a value (Button) on the right. When I press the button I want to do something like create a dialog. Because of the repetability, I decided to create a general custom ArrayAdapter like this :
public class GeneralTvBtnAdapter extends ArrayAdapter<SettingsProperty> {
private Context mContext;
private ArrayList<SettingsProperty> mProps;
private int mLayout;
private ButtonListener mListener;
public GeneralTvBtnAdapter(Context context, int tv_btn_layout, ArrayList<SettingsProperty> objects, ButtonListener listener) {
super(context, tv_btn_layout, objects);
mContext = context;
mProps = objects;
mLayout = tv_btn_layout;
mListener = listener;
}
public void updateValue (int position, String newValue) {
mProps.get(position).setValue(newValue);
notifyDataSetChanged();
}
private class ViewHolder {
TextView mName;
Button mValue;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(mContext);
convertView = mInflater.inflate(mLayout, null);
holder.mName = (TextView) convertView.findViewById(R.id.prop);
holder.mValue = (Button) convertView.findViewById(R.id.value);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mName.setText(mProps.get(position).getName());
holder.mValue.setText(mProps.get(position).getValue());
holder.mValue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onButtonClicked(position);
}
});
return convertView;
}
public interface ButtonListener {
void onButtonClicked (int position);
}
}
I want to know how can I remove the ButtonListeners listeners after the calling activity is destroyed.
P.S :SettingsProperty is a POJO with String name and String value and the tv_btn_layout is a layout with a TextView and a Button as described above.
if your intent is to remove your custom listener, you have to reset it to null. You can create a setter
public void setButtonListener (final ButtonListener listener) {
mListener = listener;
}
and call if from the outside with null. Just to be sure, before accessing mListener, check for null values
If your intention is not to call listener method if activity is destroyed then override your onfinish() of Activity and initialize the boolean flag.
private boolean IsActivityAlive;
#Override
protected void onDestroy() {
IsActivityAlive=true;
super.onDestroy();
}
And inside your interface method just check for boolean flag if it is false do the action required else ignore
void onButtonClicked (int position){
if(!IsActivityAlive){
//Do remaining task }
}
Related
In my project, I have a PagerAdapter to inflate CardViews that hold text objects created by the user.
On each CardView I have edit and delete buttons to access methods to do with the ArrayList holding the text objects.
I have coded an interface in my adapter to handle clicks made within the text object creation activity. However, my instatiateItem method in my adapter is now showing on the #Override annotation "Method does not override method from its superclass". When I remove the #Override annotation I get an error in my Logcat:
java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
How do I implement a click listener for buttons on a CardView in a PagerAdapter?
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
#Override
public int getCount() {
return pagesList.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position, final onItemClickListener listener) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
listener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
listener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
Activity code:
public class CreateActivity extends AppCompatActivity {
ViewPager viewPager;
CreateAdapter createAdapter;
List<PdfPage> pdfPageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
pdfPageList = new ArrayList<>();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
}
#Override
protected void onStart() {
super.onStart();
createAdapter = new CreateAdapter(pdfPageList, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(createAdapter);
viewPager.setPadding(130, 0, 130, 0);
createAdapter.setOnItemClickListener(new CreateAdapter.onItemClickListener() {
#Override
public void onEditClick(int position) {
}
#Override
public void onDeleteClick(int position) {
pdfPageList.remove(position);
}
});
}
Your instantiateItem method does not override from it's superclass because you added an extra argument final onItemClickListener listener.
As you are using the same clickListener for all entries and passing the item position as an argument, I suggest that you pass the onItemClickListener in the adapter constructor, or just set it via
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
Adapter code:
public class CreateAdapter extends PagerAdapter {
private List<PdfPage> pagesList;
private LayoutInflater layoutInflater;
private Context context;
private onItemClickListener mListener;
public interface onItemClickListener{
//click method for edit button
void onEditClick(int position);
//click method for delete button
void onDeleteClick(int position);
}
public void setOnItemClickListener(onItemClickListener listener){
mListener = listener;
}
public CreateAdapter(List<PdfPage> pagesList, Context context) {
this.pagesList = pagesList;
this.context = context;
}
#Override
public int getCount() {
return pagesList.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view.equals(object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
View view = null;
if(pagesList.get(position).getPageType() == "Text") {
view = layoutInflater.inflate(R.layout.create_text_cardview, container, false);
TextView inputText = view.findViewById(R.id.inputTextView);
ImageButton editButton = view.findViewById(R.id.imageButtonEdit);
ImageButton deleteButton = view.findViewById(R.id.imageButtonDelete);
//if this works, see 12:30 of video for image getter
inputText.setText(pagesList.get(position).getPageText());
//edit click listener
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mlistener != null){
int position2 = pagesList.indexOf(pagesList.get(position));
mlistener.onEditClick(position2);
}
}
});
//delete click listener
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mlistener != null){
int position3 = pagesList.indexOf(pagesList.get(position));
mlistener.onDeleteClick(position3);
}
}
});
container.addView(view, 0);
}
//put else statement here for image cardView
assert view != null;
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
And your Activity code could stay as it is.
I want to make a dialog that pops up if you click on a Name TextView in my ListView.
So I've been following this youtube tutorial: https://www.youtube.com/watch?v=ARezg1D9Zd0&t=511s and he makes a dialog that pops up from his MainActivity. But I want to pop it up from my ListView, so from my Adapter.
Because of that I can't call getSupportFragmentManager(). I think it's because my Adapter does extend ArrayAdapter<> instead of AppCompatActivity. Can anyone help me?
It goes wrong when I say: changeNameDialog.show(getSupportFragmetnManager(), "Change Name Dialog");
public class PlayersAdapter extends ArrayAdapter<Player> {
Tournament tournament = new Tournament();
private LayoutInflater Inflater;
public PlayersAdapter(#NonNull Context context, #NonNull List<Player> objects) {
super(context, R.layout.list_players,objects);
Inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = Inflater.inflate(R.layout.list_players, parent, false);
}
ImageView profilePictureButtonImageView = convertView.findViewById(R.id.profilePictureButtonImageView);
TextView namePlayerTextView = convertView.findViewById(R.id.namePlayerTextView);
TextView pointsTextView = convertView.findViewById(R.id.pointsTextView);
ImageView deleteButtonImageView = convertView.findViewById(R.id.deleteButtonImageView);
Player player = getItem(position);
profilePictureButtonImageView.setImageResource(player.getProfilePicture());
namePlayerTextView.setText(player.getName());
pointsTextView.setText(tournament.getPlayers().get(position).getTotalPoints() + "");
// Change name
namePlayerTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChangeNameDialog changeNameDialog = new ChangeNameDialog();
changeNameDialog.show(getSupportFragmetnManager(), "Change Name Dialog");
}
});
return convertView;
}
}
Create an interface with a method in your Adapter class as follows:
public interface OnAdapterInteractionListener {
void showDialog();
}
Change the constructor of your Adapter class and receive your Activity / Fragment as OnAdapterInteractionListener in it. Save it as a global variable in your Adapter class. While calling this constructor from your Activity or Fragment, pass on this for OnAdapterInteractionListener.
private LayoutInflater Inflater;
private OnAdapterInteractionListener mListener;
public PlayersAdapter(#NonNull Context context, #NonNull List<Player> objects, OnAdapterInteractionListener listener) {
super(context, R.layout.list_players,objects);
Inflater = LayoutInflater.from(context);
mListener = listener;
}
Make your Activity / Fragment implement the adapter interface OnAdapterInteractionListener and implement its method as follows:
#Override
public void showDialog() {
ChangeNameDialog changeNameDialog = new ChangeNameDialog();
changeNameDialog.show(getSupportFragmentManager(), "Change Name Dialog");
}
Finally, in your Adapter, amend your onClick() method as follows:
namePlayerTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mListener != null) mListener.showDialog();
}
});
Hope it serves your purpose!
I have a list adapter using a viewholder pattern, my problem is I need to update a TextView from the viewholder in onpostexecute() of an AsyncTask contained within the same class, but this always returns a nullpointer on the TextView, how can give my textviews from the viewholder enough scope that I can change them in teh AsyncTask? I will include a code example below, thanks in adcvance
public class ListAdapter extends BaseAdapter {
TextView tvA1, tvA2;
Integer submitId;
String submitQuestion;
public ListAdapter() {
....
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
friendsViewHolder holder = null;
if (convertView == null) {
holder = new friendsViewHolder();
convertView = inflater.inflate(R.layout.list_item_status, null);
holder.tvA1 = (TextView) convertView
.findViewById(R.id.tvA1);
holder.tvA2 = (TextView) convertView
.findViewById(R.id.tvA2);
holder.btQ1 = (LinearLayout) convertView
.findViewById(R.id.btConfirm);
holder.btQ2 = (LinearLayout) convertView
.findViewById(R.id.btDeny);
convertView.setTag(holder);
} else {
holder = (friendsViewHolder) convertView.getTag();
}
tvA1 = holder.tvA1;
tvA2 = holder.tvA2;
holder.btQ1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitId = id;
submitQuestion = q1;
new UpdateAnswer().execute();
}
});
holder.btQ2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitId = id;
submitQuestion = q2;
new UpdateAnswer().execute();
}
});
return convertView;
}
static class friendsViewHolder {
TextView tvA1, tvA2;
LinearLayout btQ1, btQ2;
}
private class UpdateAnswer extends AsyncTask<Void, Void, ArrayList<Object>> {
#Override
protected ArrayList<Object> doInBackground(Void... params) {
return apiHelper.submitAnswer(submitId, submitQuestion);
}
#Override
protected void onPostExecute(ArrayList<Object> answers) {
super.onPostExecute(answers);
if (answers != null) {
Integer a1 = (Integer) answers.get(1);
Integer a2 = (Integer) answers.get(2);
//Error here
tvA1.setText(a1);
tvA1.setVisibility(View.VISIBLE);
//error here
tvA2.setText(a2);
tvA2.setVisibility(View.VISIBLE);
}
}
}
}
Lets change few things here and there :)
Lets make friendsViewHolder public, also i need to add that in java it is somehow custom to name classes with names starting with cammel case simply put FirstLetterOfEeachWordIsBig :)
Lets add constructor to Update task :)
private class UpdateAnswer extends AsyncTask<Void, Void, ArrayList<Object>> {
int submitId;
String submitQuestion;
friendsViewHolder holder;
public UpdateAnswer (int submitId, String submitQuestion, friendsViewHolder holder) {
this.submitId = submitId;
this.submitQuestion = submitQuestion;
this.holder = holder;
}
On click lets change to
final frendsViewHolder finalHolder = holder;
holder.btQ1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new UpdateAnswer(id, q1, finalHolder).execute();
}
});
We are no longer in need of global TvA1, tvA2, submitId, submitQuestion;
And lastly we need to edit onPostExecute, hovewer i think that want be so hard since you have holder value forwarded :)
holder.tvA1.setText(a1);
holder.tvA1.setVisibility(View.VISIBLE);
holder.tvA2.setText(a2);
holder.tvA2.setVisibility(View.VISIBLE);
In java you can create constructor to allow passing additional data to object, we use this to set id, question and holder for our task, and for passing that information to other object we should'nt be using global values (they can be changed by every process), think about situation when your call "apiHelper.submitAnswer(submitId, submitQuestion)" would take long to proceed (half a second for example), then user click within half a second on answer in first and answer c on 3 question, by passing values globally you cant determine wich answer to accept :)
Cheers :)
Java is pass by value, but you can send reference of an Object in the value. I have used the above, and sent reference of holder to AsyncTask.
public class ListAdapter extends BaseAdapter {
TextView tvA1, tvA2;
Integer submitId;
String submitQuestion;
public ListAdapter() {
....
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
friendsViewHolder holder = null;
if (convertView == null) {
holder = new friendsViewHolder();
convertView = inflater.inflate(R.layout.list_item_status, null);
holder.tvA1 = (TextView) convertView
.findViewById(R.id.tvA1);
holder.tvA2 = (TextView) convertView
.findViewById(R.id.tvA2);
holder.btQ1 = (LinearLayout) convertView
.findViewById(R.id.btConfirm);
holder.btQ2 = (LinearLayout) convertView
.findViewById(R.id.btDeny);
convertView.setTag(holder);
} else {
holder = (friendsViewHolder) convertView.getTag();
}
holder.btQ1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitId = id;
submitQuestion = q1;
new UpdateAnswer(holder).execute();
}
});
holder.btQ2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submitId = id;
submitQuestion = q2;
new UpdateAnswer().execute();
}
});
return convertView;
}
static class friendsViewHolder {
TextView tvA1, tvA2;
LinearLayout btQ1, btQ2;
}
private class UpdateAnswer extends AsyncTask<Void, Void, ArrayList<Object>> {
friendsViewHolder holder;
UpdateAnswer(friendsViewHolder holder)
{
this.holder = holder;
}
#Override
protected ArrayList<Object> doInBackground(Void... params) {
return apiHelper.submitAnswer(submitId, submitQuestion);
}
#Override
protected void onPostExecute(ArrayList<Object> answers) {
super.onPostExecute(answers);
if (answers != null) {
Integer a1 = (Integer) answers.get(1);
Integer a2 = (Integer) answers.get(2);
//Error here
holder.tva1.setText(a1);
holder.tva1.setVisibility(View.VISIBLE);
//error here
holder.tva2.setText(a2);
holder.tva2.setVisibility(View.VISIBLE);
}
}
}
}
I need to update progress bar after i press the button inside my custom adapter.
Only when i declared view value as final i can do that. But it doesn't suit me.
What are the right solutions to do this ?
My custom adapter source:
public class DownloadListAdapter extends BaseAdapter implements DownloadManagerListener{
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
View view;
DownloadListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setProgress(p.size);
Button btSet = (Button) view.findViewById(R.id.btSet);
btSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this does not work
progressBar.setProgress(100);
//this doesn't work too, it will work only when view will declared as final
ProgressBar pb = (ProgressBar) view.findViewById(R.id.progressBar);
pb.setProgress(100);
}
});
return view;
}
#Override
public void OnDownloadStarted(long taskId) {
//This method should also have access to view
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
}
As I understood when I read your code, you want to start download indicator (in this case, indicator is your progress bar). So you can try this way:
1. I assume that this is your DownloadManagerListener or you can change yours to this:
public interface DownloadManagerListener {
void OnDownloadStarted(long taskId, View view);
}
2. Change your button listener like this:
btSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProgressBar progress = ((LinearLayout) v.getParent())
.findViewById(R.id.progressBar);
OnDownloadStarted(0, progress);
}
});
In this I assumed that your layout (R.layout.item) like that:
<LinelearLayout>
...
<ProgressBar android:id="#+id/progressBar"/>
...
<Button android:id="#+id/btSet"/>
</LinearLayout>
3. Finally your method OnDownloadStarted like this:
public void OnDownloadStarted(long taskId, View view) {
ProgressBar progressBar = (ProgressBar) view;
progressBar.setProgress(0);
}
UPDATE 1:
Base on author comment. I suggest following point:
1. Remove View view; from member off class and in getview method. Because when list view is populated, view reference to last item that has been call getView. So view member of adapter class is meaningless.
2. Change class which like this:
public class DownloadListAdapter extends BaseAdapter implements DownloadManagerListener{
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
HashMap<Integer, ProgressBar> mProductDownloaderMap = new HashMap<Integer, ProgressBar>();
3. Chang getView() method like this:
if (convertView == null) {
convertView = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
Button btSet = (Button) view.findViewById(R.id.btSet);
mProductDownloaderMap.put(position, progressBar);
btSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
OnDownloadStarted(position);// position is indicated taskID
}
});
4. On your dowload method:
public void OnDownloadStarted(long taskId) {
ProgressBar progressBar = mProductDownloaderMap.get(taskId);
progressBar.setProgress(0);
//Your download code here
}
Please let me know if I missunderstand your code/your purpose!
Also dont forget to mark this is anwsers if my answers is right for your question
Hope that help!
The view variable is global in the class so no need to make it final.
You should make the progressBar as final and change its progress inside button onClick method
I am trying to pass the position of checkbox present in my listview to another activity with the help of an intent. But it shows me null at another activity. Or can someone tell me how can i pass the selected checkbox data to another activity using the code below?
I want to store value in an array from the listview of the respective check box. and then transfer to another activity
public class MyAdapter extends ArrayAdapter<Model> {
int getPosition;
Intent in;
private final List<Model> list;
public final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getPosition = (Integer) buttonView.getTag(); // setTag.
}
});
convertView.setTag(viewHolder);
convertView.setOnClickListener(new MyCustomItemClickListener(getPosition));
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
class MyCustomItemClickListener implements OnClickListener {
MyCustomItemClickListener(int getPositionx) {
/*
* Whatever initialization you need can be done here. You can pass
* values in constructor when calling it from getview and use with
* intent extras
*/
in = new Intent(context, YourActivity.class);
in.putExtra("aa", getPositionx);
}
#Override
public void onClick(View arg0) {
// Launch Activity
context.startActivity(in);
}
}
}
try it out as Below way. No Need to Pass getPositionx inside Constructor as you have already define it globally in your class.
class MyCustomItemClickListener implements OnClickListener {
MyCustomItemClickListener() {
/*
* Whatever initialization you need can be done here. You can pass
* values in constructor when calling it from getview and use with
* intent extras
*/
}
#Override
public void onClick(View arg0) {
// Launch Activity
in = new Intent(context, YourActivity.class);
in.putExtra("aa", getPositionx);
context.startActivity(in);
}
}
and Change your Initialization to
convertView.setOnClickListener(new MyCustomItemClickListener());
Declear getPosition as a static variable and you can access
the value MyAdapter.getPosition to the another activity
public static getPosition;
and the activity where you want to access this variable
MyAdapter.getPosition use this.
but the way you are passing the value that is also wright but before passing
it to the intent check weather your value is not null,in this case
getPosition = (Integer) buttonView.getTag();
is not able to get the values.