Android Gridview getview is repeat - android

How to fix with Gridview getview position Too little cause repetitive problems
it is my code
class MyAdapter extends BaseAdapter {
private ArrayList<UserInforData> myData;
public MyAdapter(ArrayList<UserInforData> data){
myData = data;
}
public void updateData(ArrayList<UserInforData> data){
myData = data;
mAdapter.notifyDataSetChanged();
}
#Override
public int getCount() {
return myData.size();
}
#Override
public Object getItem(int position) {
return myData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
UserInforData d= myData.get(position);
Log.d(TAG, "getView: "+d.getUserPic());
Log.d(TAG, "getView: "+myData.toString());
ViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.from(mActivity).inflate(R.layout.islivelayout,null);
holder = new ViewHolder();
holder.liveImg =(ImageView) convertView.findViewById(R.id.liveimg);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
// if(d.isLive==1){
Log.d(TAG, "isLive: "+d.getUserPic());
String reSizeUrl = JinShanImageScaleUtil.replaceJinShanUrl(d.getUserPic(),30,30);
GlideUtil.loadUrl(mActivity, d.getUserPic(), R.drawable.lobby_defult_profile, holder.liveImg, false, false);
// }
return convertView;
}
}
i try to getPosition is at 5 or 6
but i sliding will repeat the same picture
please tell how to fix

it seems there's no problem with your code, check the data , by the way, you have not used reSizeUrl

Related

Android getView() in BaseAdapter not called (but getCount() is called)

There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. And the ListView is visibility。
I had call setListAdapter on the ListView,and the xml is match_parent.
This is my Fragment.
mNewsList = infoBean.getNews();
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
mListView.setAdapter(mNewsAdapter);
mNewsAdapter.notifyDataSetChanged();
}
} else {//load more
List<HomeBean.NewsBean> news = infoBean.getNews();
if (mNewsList != null) {
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();
}
This is my Adapter:
private class NewsAdapter extends BaseAdapter {
#Override
public int getCount() {
return mNewsList.size();
}
#Override
public HomeBean.NewsBean getItem(int position) {
return mNewsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.iv_item);
holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
holder.content = convertView.findViewById(R.id.tv_item_list_content);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.author = convertView.findViewById(R.id.tv_author);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(getItem(position).getTitle());
holder.content.setText(getItem(position).getContent());
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
holder.tvTime.setText(itemTime);
holder.author.setText(getItem(position).getSection_name());
if (getItem(position).getCover_pic().startsWith("http:")) {
picUrl = getItem(position).getCover_pic();
} else {
picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
}
HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);
return convertView;
}
}
This is my holder.
class ViewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public TextView tvTime;
public TextView content;
public TextView author;
}
this is because mListView.setAdapter(mNewsAdapter); is never executed and of cource there is no need to call mNewsAdapter.notifyDataSetChanged(); immediatelly after calling mListView.setAdapter(mNewsAdapter);
ListView is old, please switch to RecyclerView.

How to get all column from Realm's DB table?

I know that question is stupid, but I need to create Spinner using Realm and get one column for this.
All I want is get one all columt to String array to use ArrayAdapter. How can I get this column? Or maybe the better way is extend ArrayAdapter where I will get all rows from that column using a loop? Tell me, please, the better solution.
There are special Adapters for Realm:
Documentation: https://realm.io/docs/java/latest/#adapters
Project-Page: https://github.com/realm/realm-android-adapters
Example-Code: https://github.com/realm/realm-android-adapters/blob/master/example/src/main/java/io/realm/examples/adapters/ui/listview/MyListAdapter.java
Example code:
public class MyListAdapter extends RealmBaseAdapter<TimeStamp> implements ListAdapter {
private static class ViewHolder {
TextView timestamp;
}
public MyListAdapter(OrderedRealmCollection<TimeStamp> realmResults) {
super(realmResults);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.timestamp = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
TimeStamp item = adapterData.get(position);
viewHolder.timestamp.setText(item.getTimeStamp());
return convertView;
}
}
If you truly want to reinvent the wheel, then you should avoid ArrayAdapter in the first place. After all, it handles the actual binding between your elements and your views, which means you learn less!
So if you want to learn, you should create a BaseAdapter. With this, we'll recreate the RealmBaseAdapter.
Okay, so how it works is that you can extend BaseAdapter which expects the following methods:
public class MyAdapter extends BaseAdapter {
#Override
public int getCount() {
// return count;
}
#Override
public Object getItem(int position) {
// return item at position;
}
#Override
public long getItemId(int position) {
// return unique identifier at position index
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// implement view holder pattern
// bind view holder with data at position
// return bound view
}
}
And a RealmBaseAdapter also gives you a RealmChangeListener that is appended to a RealmResults that you provide on creation.
So your case would look like this
public class YourAdapter extends BaseAdapter {
RealmResults<YourObject> results;
final RealmChangeListener realmChangeListener = new RealmChangeListener() {
#Override
public void onChange(Object element) {
notifyDataSetChanged();
}
};
public YourAdapter(RealmResults<YourObject> results) {
this.results = results;
results.addChangeListener(realmChangeListener);
}
public void updateData(RealmResults<YourObject> results) {
if(this.results.isValid()) {
this.results.removeChangeListener(realmChangeListener);
}
this.results = results;
results.addChangeListener(realmChangeListener);
}
#Override
public int getCount() {
// return count;
if(results == null || !results.isValid()) {
return 0;
}
return results.size();
}
#Override
public YourObject getItem(int position) {
// return item at position;
return results.get(i);
}
#Override
public long getItemId(int position) {
// return unique identifier at position index
return position; // this is sufficient
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// implement view holder pattern
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// bind view holder with data at position
YourObject item = results.get(position);
viewHolder.textView.setText(item.getNeededColumn());
// return bound view
return convertView;
}
private static class ViewHolder {
TextView textView;
public ViewHolder(View view) {
textView = (TextView)view.findViewById(android.R.id.text1);
}
}
}
And then you can do this:
YourAdapter yourAdapter = new YourAdapter(realm.where(YourObject.class).findAll());
listView.setAdapter(yourAdapter);
Although I kinda prefer RecyclerViews lately, but that's ok

Optimize listview performance Android

I have a listView (vertical) and every list item has a horizontal list view (horizontal).
But the problem is when i scroll the horizontal scrollview in the row the vertical list is also calling getView()...
So, there is a huge performance hit..
So , can any one tell me a better solution to it ..
public class GridViewAdapter extends BaseAdapter {
List<List<Hotel>> gridDatasource;
Context mContext;
public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}
public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}
#Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}
#Override
public Object getItem(int position) {
return gridDatasource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);
holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}
}
static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}
public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;
public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}
#Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}
#Override
public Object getItem(int position) {
return mHotels.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}
Dear i suggest to try My this Code
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}
Use The Holder Class

Android - ListView with different heights in each row / getView()

I want to build a dynamic ListView (like a chat list with different layouts/bubbles).
My problem is, that each row has an individual height. My code below works,
but every time I scroll down or receive a new message,
the row with a different height gets heigher.
private class dialogAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public dialogAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public boolean hasStableIds() {
return true;
}
public int getCount() {
return dialog.size();
}
public int getViewTypeCount() {
return 999999;
}
public Object getItem(int position) {
return dialog.get(position);
}
public int getItemViewType(int position) {
return position;
}
public String getType(int position) {
return dialogType.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
Log.w("DRAGII", "POS: "+position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.bubble, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.parser = new URLImageParser(holder.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position <= dialogCache.size())
dialogCache.add(position, Html.fromHtml((String)getItem(position),
holder.parser, null));
holder.text.setText(dialogCache.get(position));
holder.type = getType(position);
int bubble = R.drawable.bubble;
if (holder.type.equals("R")) bubble = R.drawable.bubble_right;
else if (holder.type.equals("L")) bubble = R.drawable.bubble_left;
holder.text.setBackgroundResource(bubble);
return convertView;
}
class ViewHolder {
TextView text;
String type = "B";
URLImageParser parser;
}
}
What should I do?
Solved this problem by using TableLayout instead of ListView.
if u are adding tableRow programmatically to tableLayout, you will have performance issues. Think it again and find a way by using listView

Listview items background

How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code
public class Places extends Activity {
private ListView listView;
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
} else {
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
}
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Try this,it should work logically.(I didn't try it,btw! :P)
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
...
private class EfficientAdapter extends BaseAdapter {
public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
for(int i=0;i<5;i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Every time you click on listview item whole listview is getting refreshed.
So if you want to show previously selected items also then need to keep record of all the item selected. and when listview refreshed you need to check that this positions are previously selected or not according to that set your color.
Hope this help you
try this
android:background="#drawable/img_list_background_repeater"
if(clickWord.size()!=0)
{
for(int i = 0;i<clickWord.size();i++){
if(clickWord.get(i).equalsIgnoreCase(adListText[position])&&clickIndex.get(i)==position){
wordChk.setBackgroundResource(R.drawable.star_1);
}
}
}
Here clickWord is an arraylist of items selected. so when items get selected it will be added in this arraylist and when arraylist is refreshed i check all this using this loop.

Categories

Resources