button in expandable list - android

I want to create a button in the child of expandable list.
However i get Null Pointer exception.
btnmore = (Button) findViewById(R.id.btn_more);
btnmore.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(getBaseContext(), ParticularNewsDetail.class); <-- got problem here
startActivity(i);
}
});
I want to click the button to go to next activity.
What to do?

instead of giving getBaseContext() try giving your yourclassname.this. this might work i guess..

Related

how to refresh activity from adapter when a record is deleted?

i am using android studio in listview i have added adapter. when user click delete button from listview item then i want to refresh activity.
adapter delete button definition
deletebtn =convertView.findViewById(R.id.delete_btn);
deletebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
databaseAccess.deleteFavorite(favoriteModel.getWid());
}
});
i have tried this code but not working
deletebtn =convertView.findViewById(R.id.delete_btn);
deletebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
databaseAccess.deleteFavorite(favoriteModel.getWid());
Intent intent1=new Intent(context,FavoriteNameslist.class);
context.startActivity(intent1);
((Activity)context).finish();
}
});
i am new to Android - please help
i want when user click on delete button then refresh activity
There is no need to call your Activity to referesh listview you can use
notifyDataSetChanged()
remove the item and set notifyDataSetChanged()
yourList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
If you are removing a single item from the view, you need to call
yourAdapter.notifyItemChanged(position) //Just pass the position to this. This also has a default animation. Try this in your adapter class instead of refreshing the layout.
Try below lines of code on Deleting Row to Update adapter
arrayList.remove(position);
mAdapter.notifyItemRemoved(position);

Using the same activity for different buttons but different action using xml resource

I am doing a project in Android Studio. I will have 2/3 activities at most. On main activity I will have some buttons. There will be a second activity. So for each button that second activity will pop up and show some more buttons or imageButton but they will be different. The buttons on the second activity will also be meant for different actions but all of it will be on a third activity. The third activity will have some images and a text view. So image view and text view will show different data from the "XML resource" for different buttons. Any ideas how I can do it?I am using Android Studio.
Can it be done using base Adapter?
Also when all the data are stored on sql database, what different thing do I need to do?
Thank You!
When you go from main activity to the second activity, you must be using intent.
You can add extra info in your intent to to modify accordingly using putExtra()
Then in your second activity, you can simply retrieve the extra info and make changes accordingly.
in main activity,
//your first button
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load first set of images");
startActivity(newintent);
}
});
//your second button
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load second set of images");
startActivity(newintent);
}
});
in second activity,
String extrainfo=getIntent().getStringExtra("details");
if(extrainfo.equals("load first set of images"))
{
//make your changes accordingly
}
else if(extrainfo.equals("load second set of images"))
{
//make your changes accordingly
}
else
{
//make according changes
}
This is how you can achieve it.
Hope this helps!

Saving image from viewpager into a custom gridview gallery

Im building an application in Android Studio. My MainActivity is fairly simple, existing out of;
Imageview(Logo)
Viewpager(Swipeable images (10x)
ImageButton(Dislike image)
ImageButton(Like image)
Button (My personal cookbook) This button sends the user to their personal cookbook where all the images are "Liked".
The thing that I'm trying to achieve is to be able to press the ImageButton(Like) and save the image that is being showed from the ViewPager in "My personal Cookbook".
The next thing that I also want to achieve is, that when the Dislike is pressed that the Imageviewer deletes the image and goes on to the next image in the PageViewer.
I've tried searching everywhere on how to get this going but I can't find it.
Therefor I hope that someone here will be able to help me with these 2 questions.
With kind regards,
A.
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(MainActivity.this, mealname, mealpicture);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
Button btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CookbookActivity.class));
}
});
// Check Authentication
mRef = new Firebase(com.example.sick.foodinspiration.Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
}
// Dont pay attention to this
//public void DislikeMethod(View view){
//Toast.makeText(MainActivity.this, "Dislike :(", Toast.LENGTH_SHORT).show();
}
// Dont pay attention to this
//public void LikeMethod (View view){
//Toast.makeText(MainActivity.this, "Like :)", Toast.LENGTH_SHORT).show();}
private void loadLoginView() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
Button likebtn = (Button) findViewById(R.id.likebtn);
Button dislikebtn = (Button) findViewById(R.id.dislikebtn);
likebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//LikeMethod();
}
});
dislikebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);//move to next image
//DisLikeMethod();
}
});
Use sqlite database for achieve this
First create a table called MyCookBook and create the columns such as ID(integer),ImagePath/Url(text),isLike(int 0,1)
Once user click the like button insert the image path and set isLike column as 1
If user click dislike button set isLike column as 0 vice versa( like on/off)
Select the MyCookBook table which are the rows isLike as 1 is your "My personal Cookbook"
I hope this can help you.

images from button press,images enlarged

new to android development and run into a hurdle,im making an app that will have a lot of pictures, what i want to achieve is after i have pressed a button in the main activity to take me to another activity, i would like a page full of buttons on this new activity that once pressed each button will show a image and have some text i would like to add, i would like to do this without having to create a new activity for each photo so i am hoping somebody could kindly help me with this! think of it like a sounbank app that plays a sound when the buttons from each row is pressed! in my case it will show an image for each button, could this be done with just the main activity and an extra activity or will i end up with an activity open in project explorer for each picture.
your help is greatly appreaciated thank you.
Ok lets assume you are using drawables from your resource folder.
In that way you deal with integer ids that represent the images.
These id can be transmitted per Intent to a second activity, that
retrieves the intent, parses the id and loads / shows the image.
In your Activity A, where you have all your buttons, I assume further that
you've added OnClicklisteners to the button like:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//something
}
});
In your click listener you replace //something with:
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, R.drawable.oneOfYourImages);
startActivity(i);
The first argument is the key to retrieve the id later and the second is the id.
The R.drawable.oneOfYourImages depends on which button got clicked and shoud
be different for every button.
You can do this for every button, or you create a method that returns an
OnClickListener with the id as parameter.
private View.OnClickListener showImageFor(final int resIdOfImage) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, resIdOfImage);
startActivity(i);
}
};
}
Now, in Your ActivityToShowImage, you override the onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
int imageId = 0;
Bundle extras = getIntent().getExtras();
if(extras != null){
imageId = extras.getInt(IMAGE_KEY_SOME_STRING, 0);
}
if(imageId == 0){
//some log message or exception and don't forget to finish in onResume()
}else{
loadImage(imageId);
}
}
And in your loadImage method you load the image via the id into an ImageView.
I hope that is the answer to your question and sorry for the late response.

Unable to show list-view when clicking back button in android

I am showing some data in list-view,actually i am getting data from the web service by consuming it.
If i click any item from that list-view it should go to the next page and will have to show some data on the next page (say next_activity.java),well its working fine.
But on the next_activity.java page is having one back button,if i click that button it should have to go to previous list-view activity okay.
Well the intent is working fine,
but
its not showing the list-view.why?
Here is the code i have used for the back button.
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i_listagain = new Intent(TabhostActivity.this,StationsListActivity.class);
startActivity(i_listagain);
}
see what you are doing .
1- you are creating new Intent for previous List-activity .there is no need of that.
2- instead of creating new Intent use.
finish()//this ll finish your current activity
3-This will finish your current activity and will go your last activity on stack i.e your List-activity
Just write this:
list-view activity
mlistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this,new_Activity.class);
startActivity(intent);
}
});
new_activity.java
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
this.finish(); // just write this
}

Categories

Resources