Reputation: 3415
I want to fill a ViewFlipper with layouts like this, programmatically:
flippercontainer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="0.3"
android:padding="10dp"
android:src="@drawable/breakfast" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Name of product"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="Cafeteria" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Delivery" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtCaf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="$" />
<TextView
android:id="@+id/txtDel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="$" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But, I want to change the ImageView's background, and the text of the TextViews that have an id. I get NullPointerException if I try that like this:
LayoutInflater inflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.flippercontainer, null );
name=(TextView)view.findViewById(R.id.txtName);
name.setText("View number "+ i);
In the last line. How can I populate a ViewFlipper programmatically, and change the values I want to? thanks in advance.
Upvotes: 0
Views: 6047
Reputation: 3415
Each layout of the flipperview will be the same as this- flippercontainter.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:padding="10dp" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Name of product"
android:textColor="#31B6E7"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtCaf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="40dp"
android:text="$" />
</LinearLayout>
On the activity with the viewflipper:
private void setFlipperContent() {
List<Stuff> aux=getStuffList();
end = aux.size();
for (int i = 0; i < end; i++) {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.flippercontainer, null);
flipper.addView(view);
image = (ImageView) view.findViewById(R.id.imgView);
image.setBackgroundResource(aux.get(i).getImg());
name = (TextView) view.findViewById(R.id.txtName);
name.setText(aux.get(i).getName());
cafPrice = (TextView) view.findViewById(R.id.txtCaf);
cafPrice.setText("$" + aux.get(i).getCafPrice());
}
setFlipperAnimation();
}
Upvotes: 3