Skip Item Android Grid View in condition - android

I'm trying to build an android application,I have a 3 columns gridview that show a list of points,I want to jump to next line in a condition even when some of the columns are empty
Example I want to do something like this:
ARX ARH
BRH BRX BRY
CRH
DRH DRY

Try this One
your List item Row XML would be like this
Abc.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="wrap_content"
android:orientation="vertical"
android:background="#android:color/darker_gray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center"
android:text="Name1"
android:textSize="18sp"
android:background="#android:color/white"
android:layout_margin="5dp"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center"
android:text="Name2"
android:textSize="18sp"
android:layout_margin="5dp"
android:background="#android:color/white"/>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center"
android:text="Name3"
android:textSize="18sp"
android:layout_margin="5dp"
android:background="#android:color/white"/>
</LinearLayout>
</LinearLayout>
And CustomAdapter for List Would be like this
public class Custom_Adapter_Products extends BaseAdapter {
Context context;
String[] list = {"AAA","AAA","AAA","AAA","AAA","","AAA", "AAA","AAA"};
public Custom_Adapter_Products(Context context){
this.context = context;
}
#Override
public int getCount() {
return (list.length/3);
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
try{
int position = (pos*3);
ViewHolder holder;
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = vi.inflate(R.layout.abc, null);
holder = createViewHolder(convertView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if(!list[position].equals("")){
holder.textView1.setText(list[position]);
holder.textView1.setVisibility(View.VISIBLE);
}
else{
holder.textView1.setVisibility(View.GONE);
}
if(!list[position+1].equals("")){
holder.textView2.setText(list[position]);
holder.textView2.setVisibility(View.VISIBLE);
}
else{
holder.textView2.setVisibility(View.GONE);
}
if(!list[position+2].equals("")){
holder.textView3.setText(list[position]);
holder.textView3.setVisibility(View.VISIBLE);
}
else{
holder.textView3.setVisibility(View.GONE);
}
}catch (Exception e){
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder
{
public TextView textView1;
public TextView textView2;
public TextView textView3;
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
try{
holder.textView1 = (TextView) v.findViewById(R.id.textView1);
holder.textView2 = (TextView) v.findViewById(R.id.textView2);
holder.textView3 = (TextView) v.findViewById(R.id.textView3);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return holder;
}
}

Related

Showing or hiding an object in a listview

The image in the imageview disappears from the scrollview in the ListView. How can i solve it?
public class IzahAdapter extends BaseAdapter {
private ArrayList<IzahVeriModeli> list;
LayoutInflater layoutInflater;
Context context;
holder Holder;
int pos;
public IzahAdapter(Context context, ArrayList<IzahVeriModeli> list) {
this.context = context;
// Layout Inflater tanımlanıyor...
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder=null;
View satirView = null;
Button bIngg, bTrr;
ImageView resimm = null;
//
if (convertView == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listview_izahat, parent, false);
Holder = new holder();
Holder.resim = (ImageView) convertView.findViewById(resim);
Holder.bIng = (Button) convertView.findViewById(bIng);
Holder.bTr = (Button) convertView.findViewById(bTr);
Holder.bTr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pos = (Integer)v.getTag();
list.get(pos).setSelected(false);
System.out.println("pip2 = " + Integer.toString(pos));
notifyDataSetChanged();
}
});
Holder.bIng.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
list.get(pos).setSelected(true);
System.out.println("pip3 = " + Integer.toString(pos));
notifyDataSetChanged();
}
});
convertView.setTag(Holder);
}
else {
Holder = (holder) convertView.getTag();
}
Holder.bTr.setTag(position);
Holder.bIng.setTag(position);
Holder.resim.setTag(position);
System.out.println("pip1 = " + position);
if (list.get(position).isSelected()) {
Holder.bTr.setVisibility(View.VISIBLE);
Holder.bIng.setVisibility(View.GONE);
notifyDataSetChanged();
} else {
Holder.bTr.setVisibility(View.GONE);
Holder.bIng.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
if (this.list.get(position).getResim()!=null)
{
Holder.resim.setImageBitmap(this.list.get(position).getResim2());
}
else
{
Holder.resim.getLayoutParams().width=0;
}
return convertView;
}
}
The image in Imageview comes from the data base. ImageView is shrinking if it is null, if not it adds image, but when i scroll the listview, things get messed up.
I did a lot of hard work, but i did not get resolved it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="#drawable/satir_arkaplan"
android:orientation="horizontal"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/resim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:gravity="center_horizontal|center"
android:orientation="vertical"
android:padding="5dp">
<Button
android:id="#+id/bIng"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ing"
android:textSize="9dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/bTr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center"
android:background="#drawable/turk"
android:textSize="10dp" />
</LinearLayout>
Try something like that:
if (this.list.get(position).getResim()!=null) {
Holder.resim.setVisibility(View.Visible);
Holder.resim.setImageBitmap(this.list.get(position).getResim2());
}
else {
Holder.resim.setVisibility(View.Gone);
Holder.resim.setImageresource(0);
}

My list view is lagging can anyone help me to fix this?

i"m posting my adapter class and layout. can anyone suggest me how can i get rid from this lagging problem.
i tried view holder but it didn't work.
so any other solution possible then tell me fast.
my layout files are as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginBottom="5dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<View
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#drawable/circle2"/>
<com.hitesh.custom.Custom_font
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="13sp"
android:layout_gravity="center"
android:text=""/>
</FrameLayout>
<com.hitesh.custom.Custom_Roboto_Regular
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="25sp" />
</LinearLayout>
<com.hitesh.custom.Custom_Roboto_Light
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:lineSpacingExtra="1dp"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and adapter
public class Rules_d_adapter extends BaseAdapter{
private Context context;
List<String> question =new ArrayList<String>();
List<String> answer =new ArrayList<String>();
public Rules_d_adapter(Context con,List<String> Lines,List<String> Lines2)
{question=Lines;
answer=Lines2;
context = con;
}
#Override
public int getCount() {
return question.size();
}
#Override
public Object getItem(int i) {
return question.size();
}
#Override
public long getItemId(int i) {
return question.size();
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rules_d_adapter, viewGroup, false);
Custom_Roboto_Light tv2 =
(Custom_Roboto_Light)view.findViewById(R.id.tv2) ;
Custom_Roboto_Regular tv = (Custom_Roboto_Regular)
view.findViewById(R.id.tv1) ;
tv.setText(question.get(i));
tv2.setText(answer.get(i));
return view;
}
}
if you still happy with ListView then you can take idea from below
code
here i am using Cursor but you can use ArrayList in place of Cursor
public class Custom_Adapter_Products extends BaseAdapter {
Context context;
Cursor mCursor;
static int[] intArr;
DatabaseAdapter db;
public ImageLoader imageLoader;
public Custom_Adapter_Products(Context context, Cursor mCursor){
this.context = context;
this.mCursor = mCursor;
this.intArr = new int[this.mCursor.getCount()];
imageLoader=new ImageLoader(context);
}
#Override
public int getCount() {
return mCursor.getCount();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
try{
ViewHolder holder;
this.mCursor.moveToPosition(position);
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = vi.inflate(R.layout.lv_row_for_products, null);
holder = createViewHolder(convertView);
convertView.setTag(holder);
holder.btnMinus.setTag(holder);
holder.btnAdd.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
ViewHolder holder = (ViewHolder) v.getTag();
// Do whatever you want
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder
{
public TextView tvProductName;
public TextView tvPrice;
public TextView tvMRP;
public TextView tvMOQ;
public TextView tvPercent;
public TextView tvQuantity;
public ImageView imgProduct;
public ProgressBar pBar;
public Button btnMinus;
public Button btnAdd;
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
try{
holder.tvProductName = (TextView) v.findViewById(R.id.tvProductName);
holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
holder.tvMRP = (TextView) v.findViewById(R.id.tvMRP);
holder.tvMOQ = (TextView) v.findViewById(R.id.tvMOQ);
holder.tvQuantity = (TextView) v.findViewById(R.id.tvQuantity);
holder.imgProduct = (ImageView) v.findViewById(R.id.imgProduct);
holder.pBar = (ProgressBar) v.findViewById(R.id.img_pb);
holder.btnMinus = (Button) v.findViewById(R.id.btnMinus);
holder.btnAdd = (Button) v.findViewById(R.id.btnAdd);
holder.tvPercent = (TextView) v.findViewById(R.id.tvPercent);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return holder;
}
}

Scrolling GridView's items make GridView scrolls up down and will be gone

I request you just see this video you will know what is the issue
https://www.youtube.com/watch?v=gsZSMRPEZSI&feature=youtu.be
Gridview scrolls and Gone when I scroll it's item
This issue is not occurring when there is limited no. of items in GridView or when we scroll slowly
I have taken Custom GridView, in that I am loading images and text from server
below is the code
GridView's XML file
<?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="match_parent"
android:background="#color/background" >
<TextView
android:id="#+id/edtTxtProfileQuotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/blue_notification_shape"
android:drawableLeft="#drawable/blue_notification_icon"
android:drawablePadding="#dimen/small_margin"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/light_violet" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="#drawable/teambg" />
<GridView
android:id="#+id/grdGrupOfUnite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
android:layout_below="#id/edtTxtProfileQuotes"
android:numColumns="3" >
</GridView>
</RelativeLayout>
Adapter's xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" >
<com.example.achessapp.widget.CircularImageView
android:id="#+id/ivUniteGrpMemIcon"
android:layout_width="#dimen/icon_size"
android:layout_height="#dimen/icon_size"
android:layout_centerHorizontal="true"
android:src="#android:color/white" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ivUniteGrpMemIcon"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/extra_small_margin"
android:layout_marginBottom="#dimen/small_margin"
android:gravity="center_horizontal"
android:textColor="#android:color/black" />
</RelativeLayout>
in Activity File
adapter = new BondMemberAdapter(mActivity, list_members_bean);
mGridMembers.setAdapter(adapter);
In Adapter file
public class BondMemberAdapter extends BaseAdapter {
LayoutInflater mInflator;
Activity mActivity;
private ArrayList<MembersBean> list_members_bean;
Typeface tf = null;
public BondMemberAdapter(Activity activity, ArrayList<MembersBean> list) {
mActivity = activity;
mInflator = LayoutInflater.from(mActivity);
list_members_bean = list;
tf = Typeface.createFromAsset(
mActivity.getAssets(), Constants.FONT_TYPE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list_members_bean.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MembersBean bean = list_members_bean.get(position);
if (convertView == null) {
convertView = mInflator.inflate(R.layout.unite_grup_member_item,
null);
viewHolder = new ViewHolder();
viewHolder.ivUser = (CircularImageView) convertView
.findViewById(R.id.ivUniteGrpMemIcon);
viewHolder.tvName = (TextView) convertView
.findViewById(R.id.tvName);
viewHolder.tvName.setTypeface(tf);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String text = "";
text = bean.getScreen_name();
//viewHolder.ivUser.setImageResource(R.drawable.unite_item);
if (bean.getThumbnail_url() != null) {
if (bean.getThumbnail_url().equals("null")) {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
} else {
ImageLoader.getInstance().displayImage(bean.getThumbnail_url(),
viewHolder.ivUser);
}
}
else {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
}
viewHolder.tvName.setText(text);
return convertView;
}
static class ViewHolder {
CircularImageView ivUser;
TextView tvName;
int position;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_members_bean.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}

Text disappears in listview in android 2.3.6

I'm using an inflated ListView in my project. The layout file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background2" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:smoothScrollbar="true" >
</ListView>
</RelativeLayout>
And the inflated layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="70dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The above code works fine when I'm using API level 11 or above, but below API level 11, it behaves in an awkward fashion, like the textView1 becomes totally invisible while textView2 is still visible. Also, while scrolling, all the items are VISIBLE but when the scrolling is finished, it again turns invisible. I'm using Actionbarsherlock. And yeah, one last thing, I'm using SearchView in actionbar, the bottom part of which strangely appears like a blue highlighted line in API 8 while everything works perfect in higher android levels
EDIT
This is my code snippet of ListView adapter
private class ContactViewHolder {
CheckBox checkBox;
TextView textView;
TextView textView2;
ImageView iv;
}
private class ContactArrayAdapter extends ArrayAdapter<Contact> {
//private LayoutInflater inflater;
List<Contact> myList;
Context mContext;
//Cursor cursor;
public ContactArrayAdapter(Context context, List<Contact> planetList) {
super(context, R.layout.simplerow, R.id.textView1, planetList);
// Cache the LayoutInflate to avoid asking for a new one each time.
//inflater = LayoutInflater.from(context);
mContext = context;
myList = planetList;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Contact getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Contact to display
ContactViewHolder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new ContactViewHolder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.simplerow, null);
//Get xml components into our holder class
holder.textView = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Contact contact = (Contact) cb.getTag();
contact.setChecked(cb.isChecked());
}
});
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (ContactViewHolder) convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Contact contact = getItem(position);
//Fill our view components with data
holder.textView.setText(name1.get(position));
holder.textView2.setText(num1.get(position));
holder.checkBox.setChecked(contact.checked);
return convertView;
}
}
The Contact class is
private static class Contact {
private String name = "";
private boolean checked = false;
public Contact() {
}
public Contact(String name) {
this.name = name;
}
public Contact(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
Here is how to write a good adapater for your listview
public class MyAdapter extends ArrayAdapter<Person> {
Context context;
List<Person>myList;
public MyAdapter(Context context, int resource, List<Person> objects) {
super(context, resource, objects);
this.context = context;
this.myList = objects;
}
#Override
public int getCount() {
if(myList != null)
return myList.size();
return 0;
}
#Override
public Person getItem(int position) {
if(myList != null)
return myList.get(position);
return null;
}
#Override
public long getItemId(int position) {
if(myList != null)
return myList.get(position).hashCode();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
//If the listview does not have an xml layout ready set the layout
if (convertView == null){
//we need a new holder to hold the structure of the cell
holder = new Holder();
//get the XML inflation service
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate our xml cell to the convertView
convertView = inflater.inflate(R.layout.person_cell, null);
//Get xml components into our holder class
holder.txtName = (TextView)convertView.findViewById(R.id.person_cell_txtName);
holder.txtSurname = (TextView)convertView.findViewById(R.id.person_cell_txtSurname);
holder.imageView = (ImageView)convertView.findViewById(R.id.person_cell_imageview);
//Attach our holder class to this particular cell
convertView.setTag(holder);
}else{
//The listview cell is not empty and contains already components loaded, get the tagged holder
holder = (Holder)convertView.getTag();
}
//Fill our cell with data
//get our person object from the list we passed to the adapter
Person person = getItem(position);
//Fill our view components with data
holder.txtName.setText(person.getName());
holder.txtSurname.setText(person.getSurname());
Picasso.with(context).load(person.getImageUrl()).fit().into(holder.imageView);
return convertView;
}
/**
* This holder must replicate the components in the person_cell.xml
* We have a textview for the name and the surname and an imageview for the picture
*/
private class Holder{
TextView txtName;
TextView txtSurname;
ImageView imageView;
}
}
Notice that for Holder class you only set the properties reflecting the inflated xml. You dont need any constructor or getter/setters
In the get view you have to attach the holder class to the convertView tag property (convertView.setTag(holder))
retrieve the tagged holder if the convertView is not null
try to follow this pattern for the adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="fill_vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</LinearLayout>

ListView is showing gapes in list items Android

i am trying to implement listView through setListAdapter and Efficient Adapter. I want that when list is show then the background should not be repeat. My code is repeating the whole layout of list.xml due to which my list item are showing with so much gap.
Right now my list is working like that:
But i want this type of view:
Here is my editText.xml in which i type the word and a list View is opened.
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
this layout is for list.xml :
<RelativeLayout
android:id="#+id/RelativeLayout_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_cellbar" >
the list.xml file is repeating the layout in Effecient adapter:
here is my code:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
return convertView;
}
UPDATE
public class Start extends ListActivity implements OnTouchListener,
android.view.GestureDetector.OnGestureListener {
// ////////////////////////////////////////////////////
/************* INNER CLASS VIEWHOLDER ****************/
// ////////////////////////////////////////////////////
onCreate()
{
ListView list_to_use = getListView();
listAdapter = new EfficientAdapter2(this);
list_to_use.setAdapter(listAdapter);
list_to_use.setBackgroundColor(2);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
search = (EditText) findViewById(R.id.start_edit);
search.addTextChangedListener(myTextWatcher);
}
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
// ////////////////////////////////////////////////////
/*********** INNER CLASS EfficientAdapter ************/
// ////////////////////////////////////////////////////
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_search_item, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|left"
android:layout_marginTop="50dp" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_home"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
android:divider="#drawable/blue_dic"
android:dividerHeight="250sp" >
</ListView>
</FrameLayout>
</ViewFlipper>
rows for listView:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/blue_thumbnail" />
<TextView
android:id="#+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title_list"
android:layout_below="#+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />
I hope you have not provide android:dividerHeight attribute in your layout file under <ListView/>
If so please remove it.
this problem occurs when you have set the adapter class xml, parent root
match_content instead of wrap_content.
set the root content height wrap_content
As i can see in adapter layout there is no parent root like relative, linearlayout, Framelayout and ConstraintLayout etc.

Categories

Resources