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.
Related
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 to pass my value from one activity to other one.
I download data from database with Json and it works propelly.
My First Activity:
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view_test, null);
viewItem.txtTitle = (TextView)convertView.findViewById(R.id.nome_prodotto);
viewItem.txtDescription = (TextView)convertView.findViewById(R.id.descrizione_prodotto);
viewItem.txtPrice = (TextView)convertView.findViewById(R.id.prezzo);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtPrice.setText(valueList.get(position).prezzo_prodotto);
viewItem.txtTitle.setText(valueList.get(position).nome_prodotto);
viewItem.txtDescription.setText(valueList.get(position).descrizione_prodotto);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(context, Dettaglio_Prodotto.class);
// Pass all data country
intent.putExtra("nome_prodotto", "test");
// Start SingleItemView Class
context.startActivity(intent);
}
});
return convertView;
}
}
class ViewItem
{
TextView txtTitle;
TextView txtDescription;
TextView txtPrice;
}
I have to pass the value of:
viewItem.txtPrice.setText(valueList.get(position).prezzo_prodotto);
My Second Activity i have the code:
Intent i = getIntent();
nome_prodotto = i.getStringExtra("nome_prodotto");
How i can do it?
if i put:
intent.putExtra("nome_prodotto",viewItem.txtTitle.setText(valueList.get(position).nome_prodotto));
i have the error:
Variable is accessed within inner class. Needs to be declared final
Suggestion: Use an ArrayAdapter rather than BaseAdapter if you are using List data anyway.
As the IDE indicates, make a final variable.
...
else
{
viewItem = (ViewItem) convertView.getTag();
}
// This is the item in the current position
final cources item = (cources) getItem(position);
Then, you can use that instead of repeatedly calling valueList.get(position)
viewItem.txtPrice.setText(item.prezzo_prodotto);
viewItem.txtTitle.setText(item.nome_prodotto);
viewItem.txtDescription.setText(item.descrizione_prodotto);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
Intent intent = new Intent(context, Dettaglio_Prodotto.class);
// Pass all data country
intent.putExtra("nome_prodotto", item.nome_prodotto);
// Start SingleItemView Class
context.startActivity(intent);
}
});
When you use valueList in a method that creates a new onTouchListener for an instance, that is an inner class.
Any variables accessed by that class has to be final or declared inside the class. Meanign if you create a new int inside the inner class, that doesn
t have to be final. But if the int is declared outside, it has to be final.
In your case, the variable position has to be marked as final
I guess you're trying to do something like this
intent.putExtra("nome_prodotto", valueList.get(position).prezzo_prodotto);
The method putExtra expects a key (String) and a value (in your case you're passing a string value).
Been researching around this for a while and can't seem to find any solutions.
I have a 5 images that is in a PagerLayout swipe format.
What I want to do is upon clicking an image, I want to go to another XML file/class.
Code: (I've commented out methods I have tried.)
public class CustomSwipeAdapter extends PagerAdapter {
Context context;
private int[] image_resources = {R.drawable.chestpress,R.drawable.deadlift,R.drawable.squat,R.drawable.pullups,R.drawable.dips};
private Context ctx; //Gets the context of a current state or application. Tells program what is going on somewhere else.
private LayoutInflater layoutInflater; //USed to instantiate layout XML files to View Objct.
private LayoutInflater inflater;
private String[] names = {"Dumbell Press","Deadlift","Squats","Pullups","Tricep Dips"};
public ImageView imageView;
Activity activity;
View views = null;
public CustomSwipeAdapter(Context ctx){
this.ctx = ctx;
}
#Override
public int getCount() {
return image_resources.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view==(LinearLayout)o;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
imageView = (ImageView)item_view.findViewById(R.id.image_view);
TextView textView = (TextView)item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(position == 0) {
Log.d("Exercise","Dumbell Press");
//Intent intent = new Intent(ctx,MyActivity.class);
//Intent intent = new Intent(CustomSwipeAdapter.this,MyActivity.class);
//Intent intent = new Intent(context,MyActivity.class);
//views = inflater.inflate(R.layout.main,null);
//context.startActivity(intent);
//MyActivity koo = new MyActivity(); (Tried just instantiating another class (Im so lost)
}
if(position == 1){
Log.d("Exercise","Deadlift!");
}
if(position == 2){
Log.d("Exercise","Squats");
}
}
});
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
Very new android/java programmer. Thanks for reading.
Try this:
Intent intent = new Intent(CustomSwipeAdapter.this.ctx, MyActivity.class);
CustomSwipeAdapter.this.ctx.startActivity(intent);
Also make sure to pass the correct context to your Adapter constructor. The context you will pass can be the activity itself.
So, on your Activity, when you need to create a new adapter, do it like:
CustomSwipeAdapter myAdapter = new CustomSwipeAdapter(this);
Where this is the Activity on which you are creating the adapter.
If you need to create the adapter inside a fragment, just get the activity with:
MyFragment.getActivity();
Lastly, if you are wondering: "why does that work? It expects a Context and I'm passing an activity!".
Here's a short answer:
Activity inherits context. Thus, if you are in an activity, you only
need to pass itself to use the context. It also contains a pointer to
getBaseContext(). You might occasionally need to reference that, if
you need the entire application context, but most likely you won't for
a while.
Hope this helps.
Let me know if you get any more errors (don't forget to post your logcat so we can help you better).
What you should ideally be doing is this,
1: Create an interface call it swipeAdapterCallback or something, declare a method for handling the onClick
2: Make sure you get the swipeAdapterCallback instance in the constructor.
3: Then all you have to do is invoke the onClick method in the callback instance.
I've posted the adapter code here:
public class CustomSwipeAdapter extends PagerAdapter {
Context context;
private int[] image_resources = {R.drawable.chestpress, R.drawable.deadlift, R.drawable.squat, R.drawable.pullups, R.drawable.dips};
private Context ctx; //Gets the context of a current state or application. Tells program what is going on somewhere else.
private LayoutInflater layoutInflater; //USed to instantiate layout XML files to View Objct.
private LayoutInflater inflater;
private String[] names = {"Dumbell Press", "Deadlift", "Squats", "Pullups", "Tricep Dips"};
public ImageView imageView;
Activity activity;
View views = null;
private swipeAdapterCallback mCallback;
public CustomSwipeAdapter(Context ctx,swipeAdapterCallback callback) {
this.ctx = ctx;
this.mCallback = callback;
}
#Override
public int getCount() {
return image_resources.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == (LinearLayout) o;
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
imageView = (ImageView) item_view.findViewById(R.id.image_view);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCallback.onImageClick(position);
}
});
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
public interface swipeAdapterCallback {
void onImageClick(int position);
}
}
Then in your activity what you do is implement this interface and handle the onClick in the onImageClick function like so
#Override
public void onImageClick(int position) {
//handle intents and launching activities here.
switch(position){
case 0:
Log.d("Exercise","Dumbell Press");
Intent intent = new Intent(this,exampleAcitivity.class);
startActivity(intent);
break;
case 1:
Log.d("Exercise","Deadlift!");
break;
case 3:
Log.d("Exercise","Squats");
break;
}
}
Since this block of code will be inside your Activity class creating intents and starting new activities handling result from these actitivities become very simple
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 }
}
I'm getting an error in eclipse stating: The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, TextView)
I believe I'll need to change my textView to a boolean however when I attempt to do so - it causes other errors to occur. What is the best way of avoiding this error in a scenario such as this:
public class CustomListViewAdapter extends ArrayAdapter<Cmd> {
Activity context;
List<Cmd> videos;
public CustomListViewAdapter(Activity context, List<Cmd> videos) {
super(context, R.layout.list_item2, videos);
this.context = context;
this.videos = videos;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
public void run() {
Intent intent = new Intent(context, ViewVideo.class);
ViewHolder holder;
intent.putExtra("videofilename", holder.txtCmd);
context.startActivity(intent);
}
public Cmd getItem(int position) {
return videos.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtSuccess.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.txtCmd.setText(cmd.getCmd());
holder.txtCmd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.txtPrice.setText(cmd.getVideoURL() + "");
holder.txtPrice.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
run();
}
});
return convertView;
}
}
public void run(String txt) {
Intent intent = new Intent(context, ViewVideo.class);
intent.putExtra("videofilename",txt);
context.startActivity(intent);
}
and in get view:
final Cmd cmd = (Cmd) getItem(position);
and in onclick:
run(cmd.getCmd());
The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, TextView)
public Intent putExtra (String name, boolean value) name is the key and value is a boolean. So it expects a boolean value.
I don't think you want to boolean either. I guess you want to pass the text in textview to another activity
Look #
http://developer.android.com/reference/android/content/Intent.html
You can use the View object v
holder.txtCmd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv =(TextView)v; // cast to textview
Intent intent = new Intent(context, ViewVideo.class);
intent.putExtra("videofilename", tv.getText().toString());
// use getText to get the text from textview
context.startActivity(intent);
}
});
Or you can use setTag and getTag on the view
The part:
ViewHolder holder;
intent.putExtra("videofilename", holder.txtCmd);
won't get you anything, holder.txtCmd.getText is not going to return the filename, instead you can pass the file name with the function run(String filename) as a parameter. Then you will be able to pass it directly as text.