Reputation: 53
package com.example.layout.layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.w3c.dom.Text;
/**
* A placeholder fragment containing a simple view.
*/
public class MovieInfoFile extends Fragment {
public static final String ARG_OPTION="argument_option";
HashMap[]items=new HashMap[30];
int i;
public ImageView image;
TextView name;
TextView des;
TextView stars;
TextView year;
TextView rating;
public static MovieInfoFile newInstance(int option)
{
MovieInfoFile fragment=new MovieInfoFile();
Bundle args=new Bundle();
args.putInt(ARG_OPTION,option);
fragment.setArguments(args);
return fragment;
}
public MovieInfoFile() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.movieinfolayout, container, false);
Button loadnext=(Button) view.findViewById(R.id.next);
image=(ImageView) view.findViewById((R.id.image1));
name=(TextView) view.findViewById(R.id.nameans);
des=(TextView) view.findViewById(R.id.desans);
stars=(TextView) view.findViewById(R.id.starans);
year=(TextView) view.findViewById(R.id.yearans);
rating=(TextView) view.findViewById(R.id.ratingans);
enter code hereView.OnClickListener aButtonChangeImageListener=new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId()==R.id.next)
{
int id=(Integer)items[1].get("image");
image.setImageResource(id);
}
}
};
return view;
}
}
Here is the layout (XML file)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/baground"
android:weightSum="1">
<ImageView
android:layout_width="315dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/avatar"
android:id="@+id/image1"
android:layout_weight="0.53" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:id="@+id/table1">
<TableRow>
<TextView
android:text="Movie Name: "
android:textStyle="bold"
android:id="@+id/name"
/>
<TextView
android:text="Movie Name: "
android:textStyle="bold"
android:id="@+id/nameans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Description: "
android:textStyle="bold"
android:id="@+id/des"
/>
<TextView
android:text="Description: "
android:textStyle="bold"
android:id="@+id/desans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Stars "
android:textStyle="bold"
android:id="@+id/star"
/>
<TextView
android:text="Stars "
android:textStyle="bold"
android:id="@+id/starans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Year"
android:textStyle="bold"
android:id="@+id/year"
/>
<TextView
android:text="Year"
android:textStyle="bold"
android:id="@+id/yearans"
/>
</TableRow>
<TableRow>
<TextView
android:text="Rating"
android:textStyle="bold"
android:id="@+id/rating"
/>
<TextView
android:text="Rating"
android:textStyle="bold"
android:id="@+id/ratingans"
/>
</TableRow>
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LoadPrev"
android:id="@+id/prev"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loadnext"
android:layout_gravity="center_horizontal"
android:id="@+id/next"/>
</LinearLayout>
I am trying to change the image view dynamically on a button click inside a fragment. But when i do this, it is giving me error, "Unfortunately your app has stopped".Can any one help me in this and tell me the right way of modifying views dynamically on a button click with in a fragment.
Upvotes: 0
Views: 490
Reputation: 1302
for the button load next(i guess after clicking this image should change), So pass aButtonChangeImageListener to the Button loadNext like this:
loadNext.setOnClickListener(aButtonChangeImageListener);
before return view line
Upvotes: 1