On buttonclick the RecyclerView shows only the first item - java

I have 2 Activities, first to send data and the other pastes this data in a RecyclerView.
My problem is when I press on the button to take the data from the EditTexts and move them to the RecyclerView, it only updates the RecyclerView. I mean it shows only the first item which was put before, but when I try to add more data to the RecyclerView it overwrites them in the first item.
Here are my activities and layouts:
The Sender Activity
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (etTitle.length() != 0 || etDes.length() != 0){
String titled = etTitle.getText().toString();
String desed = etDes.getText().toString();
Bundle bund = new Bundle();
bund.putString("title", titled);
bund.putString("des", desed);
Intent inte = new Intent(getApplicationContext(), MainActivity.class);
inte.putExtras(bund);
startActivity(inte);
}else {
Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
}
}
});
The Receiver Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DataInput.class);
startActivity(intent);
}
});
recyclerView = findViewById(R.id.rv);
dAdapter = new DataAdapter(dataList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(dAdapter);
bundle = getIntent().getExtras();
if (bundle != null) {
sendData();
}
}
private void sendData() {
String addedTitle = bundle.getString("title");
String addedDes = bundle.getString("des");
Data data = new Data(addedTitle, addedDes);
dataList.add(data);
dAdapter.notifyDataSetChanged();
}
My XML Files:
activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/pen2"
tools:context=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
app:backgroundTint="#a40038"
app:srcCompat="#mipmap/add" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
list_item.xml:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#606c6c6c"
android:layout_margin="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_title"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#fff"
android:fontFamily="#font/mohave"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_des"
android:textSize="20sp"
android:textColor="#fff"
android:fontFamily="#font/mohave"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>

When you come back to your Sender activity and then again go to the receiver activity, you are sending data in a bundle (A fresh bundle). This bundle doesn't have any older data and when the receiver activity receives data from this bundle, it creates a new data (the previous data is being erased with a new data (sent from the sender) every time).
You need to store your previous data when you come back to your Sender activity.
One way to achieve this is to store the receiver's data locally in a database (A bad practice).
What you can do is in your Sender activity, use
startActivityForResult()
instead of
startActivity()
and override the onBackPressed() in your receiver activity.
and onBackPressed(), send the 'data' variable back to your sender activity and append that data with the new data while sending it again to the receiver activity.
Or every time you send your bundle, instead of sending a string, send an array of strings (Having previous data). (Simplest solution)
Please let me know if you need any help with the code.

Change the position of this code
bundle = getIntent().getExtras();
if (bundle != null) {
sendData();
}
to above , before initializing and setting the adapter.
And save your list , before leaving the activity to save the past values.
Hope this helps.

Related

display image in a splash screen

I am a novice android developer. I am trying to create a splash screen. I have a png image.
I have created an empty splash screen with a text box.I have 2 activities - MainActivity and SecondActivity.
code:
public class MainActivity extends AppCompatActivity {
private static int SPLASH_SCREEN_TIME_OUT=2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(MainActivity.this,
SecondActivity.class);
//Intent is used to switch from one activity to another.
startActivity(i);
//invoke the SecondActivity.
finish();
//the current activity will get finished.
}
}, SPLASH_SCREEN_TIME_OUT);
}
}
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="250dp"
android:layout_height="200dp"
app:srcCompat="#drawable/geeks"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.447" />
</androidx.constraintlayout.widget.ConstraintLayout>
But app:srcCompat="#drawable/geeks" is in red color. Means it is not resolved. Where shall I copy the png image for the splash screen? I have not copied the image anywhere in android studio. Without the image view, the splash screen runs fine showing hello world.
Few things to keep in mind.
The image size should be low (not more than 150-200kb)
Instead of adding text and image on the activity, design a splash screen using Illustrator or any other medium and save it in png format.
Store this in the res/drawable.
In the activity_main, just add line android:background ="#drawable/geeks".
In AndroidManifest.xml, add, android:theme="#style/Theme.<app name>.NoActionBar"
first you need to put your image in res/drawable folder:
and then access it by app:srcCompat or android:src attribute of ImageView inside your activity_main.xml:
app:srcCompat="#drawable/company_logo"
android:src="#drawable/company_logo"
Try writing android : src ="#drawable/geeks", instead.
put them in assets folder. It'll resolve your problem.

Android Finding Fragment TextView from Parent Class

I've seen similar questions to mine here, but none of the solutions have helped (I've tried many).
So I have a fragment, which I'm create an instance of both in my main activity and in another activity. The fragment has methods to change various TextViews, and everything works great when I call those methods from my main activity. However, when I try to do the same in my other activity, I keep getting a null pointer exception that the textView isn't found. Here is the relevant code:
activity where it doesn't work:
TextView p1n;
TextView p2n;
TextView p1s;
TextView p2s;
ScoreFragment SF;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
SF = new ScoreFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frag_frame_ttt, SF);
ft.commit();
Intent intent = getIntent();
Log.d(jl, intent.getStringExtra(P1_NAME_KEY));
if (intent != null) {
if (intent.getStringExtra(P1_NAME_KEY) != null && p1n != null) {
SF.setp1name(intent.getStringExtra(P1_NAME_KEY));
}
if (intent.getStringExtra(P2_NAME_KEY) != null && p2n != null) {
SF.setp2name(intent.getStringExtra(P2_NAME_KEY));
}
if (p1s != null) {
SF.setp1score(intent.getIntExtra(P1_SCORE_KEY, 0));
}
if (p2s != null) {
SF.setp2score(intent.getIntExtra(P2_SCORE_KEY, 0));
}
Log.d(jl, Integer.toString(intent.getIntExtra(P2_SCORE_KEY, 0)));
}
Since p1n, p2n, p1s, and p2s are always null, nothing happens.
Here is the frame layout i'm trying to put the fragment in,
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/frag_frame_ttt"
android:paddingBottom="20dp"
/>
and here is my relevant fragment code:
...
TextView p1name;
TextView p2name;
TextView p1score;
TextView p2score;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_score, container, false);
p1name = (TextView) v.findViewById(R.id.p1_name);
p2name = (TextView) v.findViewById(R.id.p2_name);
p1score = (TextView) v.findViewById(R.id.p1_score);
p2score = (TextView) v.findViewById(R.id.p2_score);
Log.d(jl, "onCreateView successful");
return v;
}
public void setp1name(String name) {
p1name.setText(name);
}
public void setp2name(String name) {
p2name.setText(name);
}
public void setp1score(int score) {
p1score.setText(Integer.toString(score));
}
public void setp2score(int score) {
p2score.setText(Integer.toString(score));
}
and the relevant fragment xml:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 1"
android:id="#+id/p1_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" : " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/p1_score"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 2"
android:id="#+id/p2_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" : " />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/p2_score"/>
</LinearLayout>
</LinearLayout>
So after doing some debugging I am quite certain that the issue has to do with the findViewById method in onCreateView() not finding anything, but I've tried moving where I findViewById (such as in each method where the text is being set), but it still hasn't worked.
Any help would be much appreciated.
Got it!
Aside from my stupid bug in my activity where I only set the text if the textview variables (the ones I never set) aren't null...
I had to move all of the code pertaining to my fragment (except for the initialize and the add / commit) from onCreate to onStart. Guess I was trying to preform actions on these textviews before the fragment actually inflated(? sorry I'm new to android, this may be incorrect).

Using imageButtons to switch activities. How can I switch image in second activity based on button pressed?

I am just starting to dabble with Android development. Right now, I have a program in Android Studio that has two columns of images about 6 images each. I want to make it so if I click that image, it will appear in the new activity. Right now, I am just loading the layout like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String name = intent.getStringExtra(Main.CHAMPION_NAME);
setContentView(R.layout.activity_champion_page);
}
With the layout in setContentView looking like this:
<LinearLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="atree496.leaguestats.ChampionPage">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/azir"
android:id="#+id/imageView" />
</LinearLayout>
As of now, it opens to the one image because that is how it is set up, but I want to be able to have the image be the one that I click. What changes do I need to do in order to achieve this? Again, I am new to this. Thank you for any help you can give.
You could add to the intent the int resource of your pressed image if you give it file name to Tag attribute in xml:
ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/azir"
android:tag="theImageFileName.png"
android:id="#+id/imageView" />
Then you could retreive it when pressed and add it to the Intent you are using:
#Override
public void onClick(View v) {
String fileName = (String) v.getTag();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("fileName", fileName);
startActivity(intent);
}
In second activity after you initialize the secondImageView:
String fileName = getIntent().getStringExtra("fileName");
int resId = getResources().getIdentifier(fileName,"drawable", getPackageName());
secondImage.setImageDrawable(getResources().getDrawable(resId));

Clickable text, new Intent, sqlite

I have a database and a method, which can get all my info from my database to a TextView. It shows only the Title of each row, there are more columns, which I want to show on new Intent when I click on a Title. My question is, how can I make each title clickable and if clicked it opens up a new intent with the relevant information(need to pass the id)? Many thanks. :)
My History Activity -
public class HistoryActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.history_layout);
final LinearLayout linearLayoutBackground = (LinearLayout) findViewById(R.id.MainLinearLayout);
TextView tv = (TextView) findViewById(R.id.tvHistory);
DataBaseHelper DataBaseHelper = new DataBaseHelper(this);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
int backgroundImages[] = {R.drawable.background1,
R.drawable.background2};
final Drawable backgroundImage = getResources().getDrawable(
backgroundImages[intValue]);
linearLayoutBackground.setBackgroundDrawable(backgroundImage);
DataBaseHelper.open();
String HistoryData = DataBaseHelper.getListData();
DataBaseHelper.closee();
tv.setText(HistoryData);
}
}
getListData method -
public String getListData() {
open();
Cursor c = myDataBase.query(TABLE_NAME1, columns, WHATTODONOW_COLUMN_ID, null, null, null, null);
String result = "";
int iTitle = c.getColumnIndex(WHATTODONOW_COLUMN_TITLE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iTitle) + "\n";
}
return result;
}
HistoryLayout -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp">>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="All the things you have already done!"
android:textSize="20dp"
android:textStyle="bold">
</TextView>
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:id="#+id/tvHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="get info from db"
android:paddingBottom="10dp"
android:textSize="25dp">
</TextView>
</ScrollView>
</LinearLayout>
There are two ways to go from my experience. First instead of using a ListView you look at ExpandableListView, which might accomplish what you want in one activity instead of having to start another intent.
Second, if you want to stick with the 2 intent design you have to pass some data from the current intent to the new one, you could do something like:
Intent i = new Intent(yourcurrentactivity.this, yournewactivity.class);
// following line is an example of passing data to the new intent.
i.putExtra(getString(R.string.str_selecteddialog), "1");
startActivity(i);
this.finish();
In the new intent that you have started in the onCreate you need to retrieve the data from the bundle:
Bundle extras = getIntent().getExtras();
if (extras != null) {
// Log.i(TAG, " 022:... we got some extras");
str_selecteddialog = getIntent().getStringExtra(getString(R.string.str_selecteddialog));
There are different types of put and get 'Extras' dependent on variable types. Now you have to decide: Do I get all the data from the cursor and pass it to this new intent or Do I just pass the row id column, typically '_id' and query it in this activity.
Hope this helps.

How to get instance of view's parent fragment:

I have a fragment with an XML layout file. in it I have an 2 clickable ImageViews.
for each ImageView I set an onClick method for example: android:onClick="commentFragmentRemoveOnClick".
In the FragmentActivity (The Activity no the Fragment) I defined it this way:
public void commentFragmentRemoveOnClick(View v)
{
}
No this Fragment is of type CommentFragment and it has a public void getFragmentTag()method
to get it's tag that I save in earlier time. I need to get an instance of the fragment in which the image was clicked to get it's tag.
I tried:
((CommentFragment)v).getParentFragment().getFragmentTag();
and:
((CommentFragment)v).getParent().getFragmentTag();
but eclipse gives me error on both of them, how is this done properly?
To make it more clear this is my CommentFragment:
public class CommentFragment extends Fragment {
private final static String TAG = CommentFragment.class.getSimpleName();
private String fragmentTag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.comment_fragment_layout,
container, false);
Bundle bundle = getArguments();
String text = bundle.getString("comment");
String fullUser = bundle.getString("user");
String user = fullUser.substring(0, fullUser.indexOf("#"));
String at = bundle.getString("at");
TextView tvCmment = (TextView) rootView.findViewById(R.id.tvComment);
TextView tvUser = (TextView) rootView.findViewById(R.id.tvUser);
TextView tvAt = (TextView) rootView.findViewById(R.id.tvDate);
tvCmment.setText(text);
tvUser.setText(user);
tvAt.setText(at);
return rootView;
}
public void setText(String item)
{
TextView view = (TextView) getView().findViewById(R.id.tvComment);
view.setText(item);
}
public void setFragmentTag(String tag)
{
this.fragmentTag = tag;
}
public String getFragmentTag()
{
return this.fragmentTag;
}
}
and the layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llCommentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/try2">
<TextView
android:id="#+id/tvUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvComment"
android:layout_alignParentTop="true"
android:background="#color/my_gray"
android:text="demo"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#color/my_even_darker_gray" />
<TextView
android:id="#+id/tvComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvDate"
android:padding="5dp"
android:text="This task is described in more details if you click on it."
android:textColor="#color/my_even_darker_gray" />
<TextView
android:id="#+id/tvAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingRight="5dp"
android:textColor="#color/my_even_darker_gray"
android:layout_toRightOf="#+id/tvUser"
android:background="#color/my_gray"
android:text="at" />
<TextView
android:id="#+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvAt"
android:layout_alignBottom="#+id/tvAt"
android:layout_toRightOf="#+id/tvAt"
android:background="#color/my_gray"
android:text="12/02"
android:textColor="#color/my_even_darker_gray" />
<ImageView
android:id="#+id/iEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvComment"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="#drawable/add_comment_button"
android:onClick="commentFragmentEditOnClick"
android:src="#drawable/add_comment_button" />
<ImageView
android:id="#+id/iRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/iEdit"
android:layout_toRightOf="#+id/iEdit"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="#drawable/add_comment_button"
android:onClick="commentFragmentRemoveOnClick"
android:src="#drawable/add_comment_button" />
</RelativeLayout>
I would love for a little assistance.
Thanks.
I have a general advice for you that would solve your problem and help you in the future -
don't use android:onClick in the xml file, use setOnClickListener in the code itself - need to avoid mixing your views with other parts of the app as much as possible.
Try to keep the Fragment independent of its activity.
If the image is part of the fragment, why does the listener is part of the FragmentActivity?
Use setOnClickListener in the fragment itself, and you might be able to use this Framgent in other pats of the app without being depended on the Activity.
It would also solve your problem of identifying the fragment in which the image was clicked.
v is not an instance of Fragment, that's why Eclipse does not like your code. If you want the instance of a fragment you have to use the FragmentManager and one of its findFragmentByXXX methods.
To get the instance of the fragment that the ImageView was clicked in I did the following:
in the Fragment I set two onClickListeners for both of the images like this:
iEdit = (ImageView)rootView.findViewById(R.id.iEdit);
iEdit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d(TAG, "pressed edit button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentEditOnClick(fragmentTag);
}
});
iRemove = (ImageView)rootView.findViewById(R.id.iRemove);
iRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d(TAG, "pressed remove button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentRemoveOnClick(fragmentTag);
}
});
and in the fragment activity I defined those two methods like this:
public void commentFragmentRemoveOnClick (String tag)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
}
for removing the fragment, and Now I'm working on editing the fragment.

Categories

Resources