I have a gridView witch contains a group of images with two columns, what I want to do is to put a title for a group of images.
Now I can do that but the problem that title is that the title is only in one column and the second column is always empty, how can I put the title in the two columns?
Here is my adapter:
public class MultimediaPhotosAdapter extends BaseAdapter {
private Context mContext;
private List<ImagesDto> imagesDto = new ArrayList<ImagesDto>();
ImagesFlickrDto imagesFlickrDto;
final String formatImage = ImagesNameFormat.FORMAT_IMAGE_DEFAULT.getImagesNameFormat();
ImagesFormatsDto currentImageFormatToDisplay;
GridView gridViewImages;
public MultimediaPhotosAdapter(Context context, GridView gridViewImages) {
this.gridViewImages = gridViewImages;
this.mContext = context;
}
#Override
public int getCount() {
return imagesDto.size();
}
#Override
public Object getItem(int position) {
return imagesDto.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = null;
HolderElementGridView holderElementGridView;
ImagesDto currentImage = imagesDto.get(position);
if(convertView == null) {
holderElementGridView = new HolderElementGridView();
itemView = View.inflate(mContext,R.layout.item_multimedia_photo,null);
holderElementGridView.image = (NetworkImageView) itemView.findViewById(R.id.imageView);
holderElementGridView.title = (TextView) itemView.findViewById(R.id.title);
itemView.setTag(holderElementGridView);
}
else {
itemView = convertView;
holderElementGridView = (HolderElementGridView)itemView.getTag();
}
if(imagesDto.get(position).isFirstElementOfCategorie()){
holderElementGridView.image.setVisibility(View.GONE);
holderElementGridView.title.setVisibility(View.VISIBLE);
holderElementGridView.title.setText(currentImage.getTitle());
}
else if(imagesDto.get(position).getUrl()!=null){
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
holderElementGridView.image.setImageUrl(StringFormat.getUrlImage(mContext, currentImage.getUrl(), currentImageFormatToDisplay.getSuffix(), currentImageFormatToDisplay.getExtension()),VolleySingleton.getInstance(mContext).getImageLoader());
holderElementGridView.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayoutInterface screenInterface=(DrawerLayoutInterface)mContext;
if(screenInterface!=null)
screenInterface.showDiaporama(imagesFlickrDto,position);
}
});
}
// column near title is empty
else{
holderElementGridView.image.setVisibility(View.VISIBLE);
holderElementGridView.title.setVisibility(View.GONE);
}
return itemView;
}
public void update(ImagesFlickrDto imagesFlickrDto){
this.imagesFlickrDto = imagesFlickrDto;
this.imagesDto = imagesFlickrDto.getListImages();
AdministrerImagesSA administrerImages = new AdministrerImagesSAImpl();
currentImageFormatToDisplay = administrerImages.getFormatByProriete(imagesFlickrDto.getImagesFormatsDto(), formatImage);
}
class HolderElementGridView{
NetworkImageView image;
TextView title;
}
}
item_multimedia_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:geekui="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.netcosports.anderlecht.activity.utils.TypefaceTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/text_size_item_menu"
android:visibility="gone"
android:layout_marginLeft="5dp"
geekui:customTypeface="fonts/DIN-Regular.otf" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/ic_launcher" >
</com.android.volley.toolbox.NetworkImageView>
</LinearLayout>
</LinearLayout>
Related
I have a custom dialog that has a ViewPager inside of it, when the dialog shows the ViewPager is just blank, not progressing on swipe or when pressing the "Next" button I implemented. I tried slightly altering my code and it didn't work. I saw several posts like this, but none of their solutions worked. PS if some things don't make sense or have mismatched names then that's because I renamed/removed some of the files/variables to simplify.
SliderAdapter:
public class SliderAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private ArrayList<String> text;
public SliderAdapter(Context context, ArrayList<String> text) {
this.context = context;
this.text = text;
}
public String[] txtH = {
"test1",
"test2",
"test3"
};
#Override
public int getCount() {
return txtH.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == (ConstraintLayout) object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout_wr, container, false);
TextView txt1 = view.findViewById(R.id.txt11);
TextView txt2 = view.findViewById(R.id.txt22);
txt1.setText(txtH[position]);
txt2.setText(text.get(position));
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ConstraintLayout) object);
}
}
Dialog itself:
public class DialogWeeklyReport extends AppCompatDialogFragment {
...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
R.style.Dialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
preferences = getActivity().getSharedPreferences("label", 0);
Random random = new Random();
text.add("test1");
text.add("test2");
text.add("test3");
viewPager = view.findViewById(R.id.viewPager);
dotLayout = view.findViewById(R.id.dotLayout);
next = view.findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (next.getText().toString().equals("Proceed")) {
dismiss();
} else {
viewPager.setCurrentItem(currentPage + 1);
}
}
});
back = view.findViewById(R.id.back);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(currentPage--);
}
});
builder.setView(view)
.setCancelable(true);
addDotsIndicator(0);
viewPager.setOnPageChangeListener(viewListener);
adapter = new SliderAdapter(getActivity(), text);
return builder.create();
}
private void addDotsIndicator(int position) {
dots = new TextView[3];
dotLayout.removeAllViews();
for (int i = 0; i<dots.length; i++) {
dots[i] = new TextView(getActivity());
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(Color.parseColor("#404040"));
dotLayout.addView(dots[i]);
}
if(dots.length > 0) {
dots[position].setTextColor(Color.BLACK);
}
if (currentPage == 0) {
back.setEnabled(false);
next.setEnabled(true);
back.setText("");
next.setText("Next");
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
addDotsIndicator(position);
currentPage = position;
if (currentPage == 0) {
back.setEnabled(false);
next.setEnabled(true);
back.setText("");
next.setText("Next");
} else if (currentPage == 1) {
back.setEnabled(true);
next.setEnabled(true);
back.setText("Back");
next.setText("Next");
} else if (currentPage == 2) {
back.setEnabled(true);
next.setEnabled(false);
back.setText("Back");
next.setText("Proceed");
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
}
Dialog 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:background="#drawable/dialog_bg"
app:cardCornerRadius="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/dotLayout"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:layout_below="#+id/viewPager"
android:orientation="horizontal" />
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_below="#+id/viewPager"
android:background="#android:color/transparent"
android:elevation="0dp"
android:text="Back"
android:textColor="#android:color/black" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/viewPager"
android:layout_margin="5dp"
android:background="#android:color/transparent"
android:elevation="0dp"
android:text="Next"
android:textColor="#android:color/black" />
</RelativeLayout>
ViewPager's slide layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:id="#+id/txt11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textColor="#android:color/black"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.24000001" />
<TextView
android:id="#+id/txt22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/txt11"
app:layout_constraintStart_toStartOf="#+id/txt11"
app:layout_constraintTop_toBottomOf="#+id/txt11"
app:layout_constraintVertical_bias="0.26" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that you didn't set the ViewPager adapter
public class DialogWeeklyReport extends AppCompatDialogFragment {
...
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
...
viewPager = view.findViewById(R.id.viewPager);
...
adapter = new SliderAdapter(getActivity(), text);
viewPager.setAdapter(adapter); // <<<<<< change here
return builder.create();
}
...
Here is my test
I want Help to overlay a play icon on top of every grid item, if it contains video.Here is my Layout File of GridView. I was Trying to do, but i was not able to wire up all things.
Ps :- I am Newbie
<FrameLayout
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:padding="2dp"
tools:context="com.techx.storysaver.ImageFragment">
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="2dp"
android:listSelector="#drawable/griditem_selector"
android:numColumns="2"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
</FrameLayout>
Here is my ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures";
File f = new File(path);
File file[] = f.listFiles();
public ImageAdapter(Context c) {
context = c;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return file.length;
}
#Override
public Object getItem(int position) {
return file[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Arrays.sort(file, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
final String path = file[position].getAbsolutePath();
CheckableLayout l;
ImageView i;
if (convertView == null) {
i = new ImageView(context);
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
int widthSize = getScreenWidth();
widthSize = widthSize / 2;
widthSize = widthSize - 8;
int heightSize = getScreenHeight();
heightSize = heightSize / 3;
heightSize = heightSize - 10;
i.setLayoutParams(new ViewGroup.LayoutParams(widthSize, heightSize));
i.setPadding(8, 8, 8, 8);
l = new CheckableLayout(context);
l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));
l.addView(i);
} else {
l = (CheckableLayout) convertView;
i = (ImageView) l.getChildAt(0);
}
if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) {
Glide
.with(context)
.load(file[position])
.into(i);
}
if (path.endsWith(".mp4")) {
Glide
.with(context)
.load(file[position])
.into(i);
}
return l;
}
public class CheckableLayout extends FrameLayout implements Checkable {
private boolean mChecked;
public CheckableLayout(Context context) {
super(context);
}
#SuppressWarnings("deprecation")
public void setChecked(boolean checked) {
mChecked = checked;
setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.item_selector) : null);
}
public boolean isChecked() {
return mChecked;
}
public void toggle() {
setChecked(!mChecked);
}
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
}
1. Create an layout XML for your GridView Item.
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="false"
card_view:cardPreventCornerOverlap="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/sample_1" />
<ImageView
android:id="#+id/icon_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/icon_play"/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image"
android:padding="8dp"
android:background="#CCFFFFFF"
android:text="This is simple title"
android:textColor="#000000" />
</RelativeLayout>
</android.support.v7.widget.CardView>
2. Use grid_item.xml from your adapter getView() method for each item:
#Override
public View getView(int pos, View convertView, ViewGroup parent)
{
Arrays.sort(file, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
final String path = file[position].getAbsolutePath();
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
ImageView iconPlay= (ImageView) convertView.findViewById(R.id.icon_play);
TextView textTitle= (TextView) convertView.findViewById(R.id.title);
// Image
if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) {
Glide
.with(context)
.load(file[position])
.into(imageView);
// Hide play icon
iconPlay.setVisibility(View.GONE);
}
// Video
if (path.endsWith(".mp4")) {
Glide
.with(context)
.load(file[position])
.into(imageView);
// Show play icon
iconPlay.setVisibility(View.VISIBLE);
}
return convertView;
}
OUTPUT:
Hope this will help~
I have implemented CustomListView with RadioGroup. When I selected the radio button of first/beginning rows and scrolling to the end of list, last row elements are automatically selected. And when I'm scrolling back to beginning of List, Radiogroup state is not maintained. I have spent lot of time with this issue.
Kindly help me out where I'm doing a mistake.
MyAdapter.java
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected RadioGroup radioGroup;
protected RadioButton firstRadio;
protected RadioButton secondRadio;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group);
viewHolder.firstRadio = (RadioButton) convertView.findViewById(R.id.first_rad);
viewHolder.secondRadio = (RadioButton) convertView.findViewById(R.id.second_rad);
viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int selectedID) {
int getPosition = (Integer) radioGroup.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(selectedID); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.radio_group, viewHolder.radioGroup);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radioGroup.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
if(list.get(position).getSelected() == viewHolder.firstRadio.getId()){
viewHolder.firstRadio.setChecked(true);
}else if(list.get(position).getSelected() == viewHolder.secondRadio.getId()){
viewHolder.secondRadio.setChecked(true);
}
return convertView;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30sp" >
</TextView>
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label"
android:orientation="horizontal">
<RadioButton
android:id="#+id/first_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>
<RadioButton
android:id="#+id/second_rad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false" >
</RadioButton>
</RadioGroup>
Model.java
public class Model {
private String name;
private int selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
Change the conditions in if statement as below and it will work.
if(list.get(position).getSelected() == viewHolder.firstRadio.getId())
{
viewHolder.firstRadio.setChecked(true);
viewHolder.secondRadio.setChecked(false);
}
else if(list.get(position).getSelected() == viewHolder.secondRadio.getId())
{
viewHolder.secondRadio.setChecked(true);
viewHolder.firstRadio.setChecked(false);
}
else
{
viewHolder.secondRadio.setChecked(false);
viewHolder.firstRadio.setChecked(false);
}
I'm not sure how it is working now. I've spent lot of days in this issue. Overriding getViewTypeCount and getItemViewType resolved my issue.
#Override
public int getViewTypeCount() {
if (objects.size() > 0) {
return objects.size();
}
return 1;
}
#Override
public int getItemViewType(int position) {
return position;
}
I have a listView with multiple lines and I want to add one more line with a link, but i dont know how to do it.
example
this is my code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Custom ListView Example" />
<ListView
android:id="#+id/srListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
custom_row_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF00A7FF"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
SearchResults.java
public class SearchResults {
private String name = "";
private String cityState = "";
private String phone = "";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getCityState() {
return cityState;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
MyCustomBaseAdapter.java
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
holder.txtPhone.setText(searchArrayList.get(position).getPhone());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv.getItemAtPosition(position);
SearchResults fullObject = (SearchResults)o;
Toast.makeText(MainActivity.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setName("Hospital de Lisboa");
sr.setCityState("Avenida 123 nº15");
sr.setPhone("212321234");
results.add(sr);
sr = new SearchResults();
sr.setName("Hospital de Santarém");
sr.setCityState("Rua de Cima nº20");
sr.setPhone("234234234");
results.add(sr);
return results;
}
}
You can't just create a new textview and create new methods to your SearchResults then just sr.setNewLine()?
I have one app that creates a table row from an adapter.Here the adapter:
public class TableAdapter extends ArrayAdapter<Table> {
public TableAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TableAdapter(Context context, int resource, List<Table> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_table, parent, false);
holder.position = (TextView) convertView.findViewById(R.id.position);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
holder.image = (ImageView) convertView.findViewById(R.id.image);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.points = (TextView) convertView.findViewById(R.id.points);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position != 0) {
convertView.findViewById(R.id.header).setVisibility(View.GONE);
}
holder.position.setText(String.valueOf(getItem(position).getPosition()));
holder.image.setVisibility(View.GONE);
holder.progress.setVisibility(View.VISIBLE);
holder.name.setText(getItem(position).getName());
holder.points.setText(String.valueOf(getItem(position).getPoints()));
final ViewHolder tmp = holder;
Picasso.with(getContext()).load(getContext().getResources().getString(R.string.team_logo) + getItem(position).getId() + ".png").into(holder.image, new Callback() {
#Override
public void onSuccess() {
tmp.progress.setVisibility(View.GONE);
tmp.image.setVisibility(View.VISIBLE);
}
#Override
public void onError() {
}
});
return convertView;
}
class ViewHolder {
TextView position;
ProgressBar progress;
ImageView image;
TextView name;
TextView points;
}
}
and the layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/position"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/imgHolder"
android:layout_width="50dp"
android:layout_height="50dp" >
<ProgressBar
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/points"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
So far everything is working great as expected.
But I would like to add an header to the table.
Something like:
# | Name | Pts
I tried to add this in the layout, and check if the position != 0 and set the visibility to GONE, but the problem is when I scroll down and up it disappears.
My question is: How can I add and header to this table?
Or add this header fixed, and everything else scrollable?
In your adapter, add these two method overrides:
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return position == 0 ? 0 : 1;
}
This tells the adapter that you have two types of row layout, and which rows have each type.