public class ListViewAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public ListViewAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
list = new View(context);
list = inflater.inflate(R.layout.listview_layout, null);
TextView name = (TextView) list.findViewById(R.id.location_name);
TextView desc = (TextView) list.findViewById(R.id.location_desc);
ImageView img = (ImageView) list.findViewById(R.id.location_image);
name.setText(locationDetails[i].getLocationName());
desc.setText(locationDetails[i].getLocationDesc());
img.setImageResource(locationDetails[i].getLocationIcon());
} else {
list = (View) view;
}
return list;
}
}
This code is the code before I use viewholder, it works fine. Then I tried to modify it with the use of viewholder, and compile it with gradle, all the image and textviews disappeared, I don't know what's wrong with the code below. Why can't I get any thing shown on the screen?
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}
public class LocationAdapter extends BaseAdapter {
private Context context;
private LocationDetails[] locationDetails;
Object model;
public LocationAdapter(Context c, LocationDetails[] locationDetails) {
context = c;
this.locationDetails = locationDetails;
}
#Override
public int getCount() {
return locationDetails.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View list = view;
ViewHolder vh;
if (list == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list = inflater.inflate(R.layout.listview_layout, null);
vh = new ViewHolder();
vh.name = (TextView) list.findViewById(R.id.location_name);
vh.desc = (TextView) list.findViewById(R.id.location_desc);
vh.img = (ImageView) list.findViewById(R.id.location_image);
list.setTag(vh);
} else {
vh = (ViewHolder) list.getTag();
}
model = getItem(i);
vh.name.setText(model.getLocationName());
vh.desc.setText(model.getLocationDesc());
// same as image...
return list;
}
static class ViewHolder {
TextView name;
TextView desc;
ImageView img;
}
}
Related
I would like to display video from database into a grid view i managed to do it with images but have no idea how to do it with a videoview , i would like to do the same but for videos
public class Photoadapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Photo> photoList;
#Override
public int getCount() {
return photoList.size();
}
#Override
public Object getItem(int position) {
return photoList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imageViewList);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Photo photo = photoList.get(position);
final byte[] photoImage = photo.getImage();
final Bitmap bitmap = BitmapFactory.decodeByteArray(photoImage, 0, photoImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
public Photoadapter(Context context, int layout, ArrayList<Photo> photoList) {
this.context = context;
this.layout = layout;
this.photoList = photoList;
}
}
UPDATE WITH VIEW HOLDER
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
static class ViewHolder {
TextView tvFirstName;
TextView tvSecondName;
View row;
}
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final DatabaseListItems databaseListItems = databaseList.get(position);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.custom_listview_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvFirstName = (TextView) rowView.findViewById(R.id.txtViewFirstName);
viewHolder.tvSecondName = (TextView) rowView.findViewById(R.id.txtViewSecondName);
viewHolder.row = rowView.findViewById(R.id.row);
rowView.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tvFirstName.setText(databaseListItems.getFirstName());
holder.tvSecondName.setText(databaseListItems.getSecondName());
if (databaseListItems.getFirstRow() == true) {
holder.tvSecondName.setAlpha(0.0f);
holder.tvFirstName.setTextColor(Color.parseColor("#161616"));
holder.row.setBackgroundColor(Color.parseColor("#f7f7f7"));
holder.row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
holder.tvSecondName.setAlpha(1.0f);
}
}
return rowView;
}
}
in my android app, i have got an custom table list, where the data will be dynamic filled.
only the first row should a an static row, which i set programmatically.
i have this function to fill my data into the list
private void showList() {
boolean FirstRow = true;
ArrayList<DatabaseListItems> databaseList = new ArrayList<>();
databaseList.clear();
String query = "SELECT * FROM " + DatabaseHelper.DATABASE_TABLE;
Cursor c1 = dbHandler.selectQuery(query);
if (c1.moveToFirst()) {
do {
DatabaseListItems databaseListItems = new DatabaseListItems();
if (c1.getPosition() == 0 && FirstRow == true) {
databaseListItems.setFirstRow(true);
FirstRow = false;
databaseListItems.setFirstName("FIRST ROW");
databaseListItems.setSecondName(null);
c1.moveToPosition(-1);
} else {
databaseListItems.setFirstRow(false);
FirstRow = false;
databaseListItems.setFirstName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_FIRSTNAME)));
databaseListItems.setSecondName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_SECONDNAME)));
}
databaseList.add(databaseListItems);
} while (c1.moveToNext());
}
c1.close();
DatabaseListAdapter databaseListAdapter = new DatabaseListAdapter(getActivity(), databaseList);
ListView.setAdapter(databaseListAdapter);
}
DatabaseListItems
public class DatabaseListItems {
String firstname;
String secondname;
Boolean FirstRow;
public String getFirstName() {
return firstname;
}
public void setFirstName(String first name) {
this.firstname= firstname;
}
public String getSecondName() {
return secondname;
}
public void setSecondName(String secondname) {
this.secondname = secondname;
}
public Boolean getFirstRow() {
return FirstRow;
}
public void setFirstRow(Boolean FirstRow) {
this.FirstRow = FirstRow;
}
}
DatabaseListAdapter
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
final DatabaseListItems databaseListItems = databaseList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_listview_item, null);
}
TextView tvFirstName = (TextView) convertView.findViewById(R.id.txtViewFirstName);
tvFirstName.setText(databaseListItems.getFirstName());
TextView tvSecondName = (TextView) convertView.findViewById(R.id.txtViewSecondName);
tvSecondName.setText(databaseListItems.getSecondName());
View row = convertView.findViewById(R.id.row);
ImageView Arrow = (ImageView) convertView.findViewById(R.id.Arrow);
// First Static Row
if (databaseListItems.getFirstRow() == true) {
tvSecondName.setAlpha(0.0f);
Arrow.setAlpha(1.0f);
tvFirstName.setTextColor(Color.parseColor("#161616"));
row.setBackgroundColor(Color.parseColor("#f7f7f7"));
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
tvSecondName.setAlpha(1.0f);
Arrow.setAlpha(0.0f);
}
return convertView;
}
}
On the first view, all looks fine.
but if i scroll down and up, random rows get the background color and the onClick function , which only the first row should get.
anyone an idea what i do wrong?
I am working on an app with multiple TextViews in a ListView, but I seem to have encountered a problem. The list does not appear to get modified.
I'm attachig my code now
public class MainActivity extends ActionBarActivity {
private List<CustomItem> myItem = new ArrayList<>();
ListView listView;
EditText edit;
Button insert;
String getText;
StringTokenizer st;
private String text1,text2;
List<CustomItem> customItem = new ArrayList<CustomItem>();
ArrayAdapter<CustomItem> adapter_List;
CompleteAdapter completeAdapter;
CustomItem obj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
insert = (Button)findViewById(R.id.addBtn);
populate();
listView = (ListView) findViewById(R.id.listView);
completeAdapter = new CompleteAdapter(customItem, this);
listView.setAdapter(completeAdapter);
}
private void populate() {
customItem.add(new CustomItem("Testing","Application"));
}
public void addToList(View view) {
getText = edit.getText().toString();
st = new StringTokenizer(getText," ");
text1 = st.nextToken().toString();
text2 = st.nextToken().toString();
MainActivity.this.customItem.add(new CustomItem(text1,text2));
MainActivity.this.completeAdapter.notifyDataSetChanged();
}
/*
private class MyListAdapter extends ArrayAdapter<CustomItem> {
private MyListAdapter() {
super(MainActivity.this, R.layout.item_row,myItem);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView== null){
itemView = getLayoutInflater().inflate(R.layout.item_row,parent,false);
}
CustomItem customItem = myItem.get(position);
TextView t1 = (TextView) itemView.findViewById(R.id.text1);
TextView t2 = (TextView) itemView.findViewById(R.id.text2);
t1.setText(customItem.getName());
t2.setText(customItem.getAddress());
return itemView;
}
}*/
}
this is my adapter class
class CompleteAdapter extends ArrayAdapter<CustomItem>{
private List<CustomItem> items;
private Context context;
CompleteAdapter(List<CustomItem> items, Context context) {
super (context,R.layout.item_row,items);
this.items = items;
this.context = context;
}
#Override
public int getCount() {
return 0;
}
#Override
public CustomItem getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= convertView;
Holder holder = new Holder();
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return v;
}
private class Holder {
public TextView nameView;
public TextView addView;
}
}
This is my CustomIte.java
class CustomItem{
private String name;
private String address;
CustomItem(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}}
As you can see I've tried with 2 adapters with the same result. There is data contained in the Strings being passed, so I think the problem is with the adapter. (The ids of all the components are perfect) What changes should I make?
Should change in your Adapter
#Override
public int getCount() {
return 0;
}
#Override
public CustomItem getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
to
#Override
public int getCount() {
return items.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public CustomItem getItem(int position) {
return items.get(position);
}
The problem is in your adapter which return a null view (v)
#Override
public int getCount() {
return items.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public CustomItem getItem(int position) {
return items.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
convertView =LayoutInflater.from(context).inflate(R.layout.item_row, null);
TextView name_text = (TextView)v.findViewById(R.id.name);
TextView address_text = (TextView)v.findViewById(R.id.address);
holder = new Holder();
holder.nameView = name_text;
holder.addView = address_text;
v.setTag(holder);
}
else
holder = (Holder)v.getTag();
CustomItem custom = items.get(position);
holder.nameView.setText(custom.getName());
holder.addView.setText(custom.getAddress());
return convertView ;
}
Hop it helps ;)
I have a Navigation Drawer, With a ListView and with a header. When I scorll the drawer up down the items are mixed, the type of the items. Why?
My DrawerAdapter.java:
public class DrawerAdapter extends BaseAdapter {
private Context context;
private List<DrawerItem> navDrawerItems;
DrawerItemClickListener drawerItemClickListener;
public DrawerAdapter(Context context, ArrayList<DrawerItem> navDrawerItems,DrawerItemClickListener drawerItemClickListener) {
this.context = context;
this.navDrawerItems = navDrawerItems;
this.drawerItemClickListener = drawerItemClickListener;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean isEnabled(int position) {
return false;
}
static class ViewHolder {
ImageView imvIcon;
TextView textView;
LinearLayout layout;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final DrawerItem mItem = navDrawerItems.get(position);
if(mItem.getType().equals(DrawerItem.TYPE_CLICKABLE))
{
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_list_item, parent, false);
holder.imvIcon = (ImageView) convertView.findViewById(R.id.item_icon);
holder.textView = (TextView) convertView.findViewById(R.id.item_text);
holder.layout = (LinearLayout) convertView.findViewById(R.id.drawer_one_layout);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
if(holder.imvIcon != null)
Picasso.with(parent.getContext()).load(mItem.getIcon()).into(holder.imvIcon);
if(holder.textView != null)
holder.textView.setText(mItem.getTitle());
if(holder.layout != null)
holder.layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerItemClickListener.DrawerItemClicked(mItem);
}
});
}
else
{
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_list_item_two, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.item_text);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mItem.getTitle());
}
return convertView;
}}
And the Drawer Item :
public class DrawerItem {
public final static String TYPE_CLICKABLE = "clikc";
public final static String TYPE_HEADER = "header";
private String title, type;
private int icon;
public DrawerItem(String title, int icon, String type) {
this.title = title;
this.icon = icon;
this.type = type;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
public String getType() {return this.type;}}
Please help me. Thanks.
You are not clearly stating which item type your current view is, so the adapter doesn't know the viewtype and doesn't display correctly the items (e.g. it mixes them).
In your getItemViewType() you should be clear about the viewtype of the current item, and do something like this:
#Override
public int getItemViewType(int position) {
if(navDrawerItems.get(position).getType().equals(DrawerItem.TYPE_CLICKABLE))
return 0;
return 1;
}
This is my BaseAdapater class in which I am using ViewHolder.
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
public int[] mThumbIds = {R.drawable.shoes,R.drawable.dress,R.drawable.purse,
R.drawable.shoes,R.drawable.dress,R.drawable.purse ,
R.drawable.shoes,R.drawable.dress,R.drawable.purse };
public WishbookCategoryAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.wishbook_gridview_items, null);
holder.Category = (TextView)convertView.findViewById(R.id.wishbook_grid_category);
holder.items = (TextView)convertView.findViewById(R.id.wishbook_grid_category_number);
holder.iv = (ImageView)convertView.findViewById(R.id.wishbook_category_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.iv.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
holder.iv.setScaleType(ScaleType.FIT_XY);
holder.Category.setText("Category");
holder.items.setText("items");
return convertView;
}
private static class ViewHolder {
protected ImageView iv;
protected TextView Category;
protected TextView items;
}
}