Reputation: 47
I want to set 3 icons to right side in each row of list view .I am making shopping app in which I want to when in list view user select any product so he/she could see product in 3 way like if user will select icon 1 so user could see product in grid view and if user select icon 2 so user can see product in image switcher,on icon 3 user can see products in list view actually i wana user colud see products in 3 view.when click on icons so next activity open like if user select icon of grid view so next activity will open of grid view.this is my code
public class ListViewSupplementActivityActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.listlayout);
ListView view = (ListView) findViewById(R.id.list);
view.setAdapter(new CustomImageListAapter(this));
view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(position ==0)
{
Intent ii = new Intent(ListViewSupplementActivityActivity.this,SingleListItem.class);
ii.putExtra("operation", position);
startActivity(ii);
}
if(position ==1){
Intent in = new Intent(ListViewSupplementActivityActivity.this,Test.class);
in.putExtra("operation", position);
startActivity(in);
}
if(position == 5){
Intent in2= new Intent(ListViewSupplementActivityActivity.this,FullImageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("operation", position);
in2.putExtras(bundle);
startActivity(buse); }
}
});
}
}
This is custom adapter class:
public class CustomImageListAapter extends BaseAdapter {
private int[] images = {R.drawable.autumnwalk, R.drawable.bridge,R.drawable.butterfly,
R.drawable.cheetah, R.drawable.cloud, R.drawable.su, R.drawable.wi,
R.drawable.rocksculpture, R.drawable.skyatsunset, R.drawable.s,
R.drawable.smoke, R.drawable.tulips};
private String[] imageDesc = { "Autumn Walk", "Bridge",
"Butterfly", "Cheetah", "Cloud", "Highway", "Martini",
"Rock Sculpture", "Sky at Sunset", "Sliced Orange", "Smoke",
"Tulips"};
private Context ctx = null;
public CustomImageListAapter(Context context) {
this.ctx = context;
}
public int getCount() {
return images.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imgView = new ImageView(this.ctx);
imgView.setScaleType(ScaleType.FIT_CENTER);
imgView.setPadding(8, 8, 8, 8);
imgView.setImageResource(images[arg0]);
imgView.setAdjustViewBounds(Boolean.TRUE);
imgView.setContentDescription(imageDesc[arg0]);
imgView.setMaxHeight(200);
imgView.setMaxWidth(200);
TextView tv = new TextView(this.ctx);
tv.setText(imageDesc[arg0]);
tv.setMaxHeight(100);
tv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);
LinearLayout layoutView = new LinearLayout(this.ctx);
layoutView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 150);
layoutView.addView(imgView, params1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutView.addView(tv, params2);
return layoutView;}
}
Upvotes: 2
Views: 855
Reputation: 1248
To save time doing it programmatically, you can do as follow:
RelativeLayout
.(1) Create list_view_item.xml
for item layout:
The key work is set the first item to parent view's right: android:layout_alignParentRight="true"
Then the following items are aligned by previous item: android:layout_alignParentLeft="@+id/id_of_prev_item"
(EDITED for 3 images:)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="@+id/img1" />
<ImageView
android:id="@+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="@+id/img2" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="@+id/img3"
android:fadingEdge="vertical" />
</RelativeLayout>
(2) Inflate item layout in CustomImageListAapter
:
Suggestion: extend your CustomImageListAapter
class from ArrayAdapter<T>
to more specific use of data item and make your code more comprehensive.
public class CustomImageListAapter extend ArrayAdapter<YourDataItem>
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
int item_view_id = R.layout.list_view_item;
LinearLayout holderView = new LinearLayout(getContext());
String inflaterName = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(inflaterName );
inflater.inflate(item_view_id, holderView, true);
//YourDataItem: is your class represent each row on your database
YourDataItem item = getItem(position);
//TODO: do your stuff of works with your view item and data here
return holderView;
}
}
Upvotes: 0
Reputation: 1576
you have to create custm view for your list viewor you can simply create a xml file for that and later in Adapter class inflate it.
like here i have create a xml file with two TextViews.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.two_text, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(CountriesList.abbreviations[position]);
holder.text2.setText(CountriesList.countries[position]);
return convertView;
}
Upvotes: 0
Reputation: 1975
Define and use a xml layout file for your list item row. The row will contain 4 ImageViews on the right side along with any other Views. And use this as the layout for each row. And based on the position in your CustomAdapter
class you can get references to each of your rows.
Now, inflate this xml layout for list item row in getView()
of CustomAdapter.
Upvotes: 1