I encounter an error where when I first load a listview with images from a URL the size is not displayed correctly. However this only happens the first time.
If I go back to the previous fragment and then again to the ListView, the image sizes are fixed ( although I can see that momentarily they had the wrong size before being fixed ).
I use Android Query to retrieve the images and this example for my listview :
http://javatechig.com/android/android-listview-tutorial
SearchResultAdapter.java
public class SearchResultAdapter extends BaseAdapter{
private Context context;
private ArrayList listData;
private LayoutInflater layoutInflater;
public SearchResultAdapter(Context context, ArrayList listData) {
this.context = context;
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.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 = layoutInflater.inflate(R.layout.searchresult_single_item, null);
holder = new ViewHolder();
holder.adImage = (ImageView) convertView.findViewById(R.id.adImage);
holder.adTitle = (TextView) convertView.findViewById(R.id.adTitle);
holder.adPrice = (TextView) convertView.findViewById(R.id.adPrice);
holder.adSize = (TextView) convertView.findViewById(R.id.adSize);
holder.adDetails = (TextView) convertView.findViewById(R.id.adDetails);
holder.adMessage = (ImageView) convertView.findViewById(R.id.adMessage);
holder.adFavorite = (ImageView) convertView.findViewById(R.id.adFavorite);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
AdItem adListItem = (AdItem) listData.get(position);
AQuery aq = new AQuery(convertView);
aq.id(holder.adImage).image(adListItem.getAdImageURL(), true, true, 50, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);
ImageLoader imgLoader = new ImageLoader(context);
imgLoader.DisplayImage(adListItem.getAdImageURL(), holder.adImage);
holder.adTitle.setText(adListItem.getAdTitle());
holder.adPrice.setText(adListItem.getAdPrice());
holder.adSize.setText(adListItem.getAdSize());
holder.adDetails.setText(adListItem.getAdDetails());
return convertView;
}
static class ViewHolder {
ImageView adImage;
TextView adTitle;
TextView adPrice;
TextView adSize;
TextView adDetails;
ImageView adMessage;
ImageView adFavorite;
}
}
SearchResultFragment.java
public class SearchResultFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_searchresult, container, false);
ArrayList listData = getListData();
final ListView searchResult = (ListView) view.findViewById(R.id.listViewSearchResult);
searchResult.setAdapter(new SearchResultAdapter(getActivity(), listData));
searchResult.invalidateViews();
return view;
}
private ArrayList getListData() {
ArrayList results = new ArrayList();
AdItem adData = new AdItem();
adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/36/3d3eb5b646560480_2014_09_07_19_26_33.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€300");
adData.setAdSize("37m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);
adData = new AdItem();
adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/46/a37ed74847504440_img_7409.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€450");
adData.setAdSize("50m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);
adData = new AdItem();
adData.setAdImageURL("https://img.wg-gesucht.de/media/up/2014/46/4399903447385080_img_3106.sized.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("€500");
adData.setAdSize("62m");
adData.setAdDetails("This is a wonderful apartment at a very reasonable price.");
results.add(adData);
return results;
}
}
This is my fragment layout for the row item :
<ImageView
android:id="#+id/adImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_marginRight="20dp"/>
<TextView
android:id="#+id/adTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/adImage" />
<TextView
android:id="#+id/adPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#id/adTitle"
android:layout_toRightOf="#id/adImage" />
<TextView
android:id="#+id/adSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#id/adTitle"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/adPrice" />
<TextView
android:id="#+id/adDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="#id/adImage"
android:layout_below="#id/adPrice"/>
<ImageView
android:id="#+id/adFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<ImageView
android:id="#+id/adMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/adFavorite"/>
First image is what it looks like the first time the listview is loaded and what it looks like after ( 2nd image is what it should look like ).
If it is not necessary to keep this min width/height, you can get 2nd image result changing ImageView attributes in your list row to:
<ImageView
android:id="#+id/adImage"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:scaleType="centerCrop"/>
Related
Hellow i've implemented ListView inside the CardView but now what problem i got is, the CardView is not Expanding on Adding list item. Can anybody help me,how to slove the issue? I have gone through different posts on stackoverflow but cannot find the solution :( My code is below
//this is my Listview adapter Class
public class EmploymentHistoryAdapter extends BaseAdapter {
public ArrayList<EmploymentHistory> mEmploymentHistories;
private Context context;
public EmploymentHistoryAdapter(ArrayList<EmploymentHistory> mEmploymentHistories, Context context) {
this.mEmploymentHistories = mEmploymentHistories;
this.context = context;
}
#Override
public int getCount() {
return mEmploymentHistories.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
EmploymentHistory empHistory = mEmploymentHistories.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_employee_history_row, parent, false);
} else {
v = convertView;
}
TextView hospital = (TextView) v.findViewById(R.id.hospital);
TextView department = (TextView) v.findViewById(R.id.department);
TextView startDate = (TextView) v.findViewById(R.id.startDate);
TextView endDate = (TextView) v.findViewById(R.id.endDate);
TextView hospitalName = (TextView) v.findViewById(R.id.hospitalName);
hospitalName.setText(empHistory.getHospital());
TextView departmentName = (TextView) v.findViewById(R.id.departmentName);
departmentName.setText(empHistory.getDepartment());
TextView startDate1 = (TextView) v.findViewById(R.id.startDate1);
startDate1.setText(empHistory.getStartDate());
TextView endDate1 = (TextView) v.findViewById(R.id.endDate1);
endDate1.setText(empHistory.getEndDate());
hospital.setVisibility(View.VISIBLE);
department.setVisibility(View.VISIBLE);
startDate.setVisibility(View.VISIBLE);
endDate.setVisibility(View.VISIBLE);
return v;
}
}
//this is where i populated the ListView
private void populateEmploymentHistoryList() {
listEmployeeHistory = (ListView) findViewById(R.id.listEmployeeHistory);
empHistory = dbHelper.getAllEmploymentHistory();
employmentHistoryAdapter = new EmploymentHistoryAdapter(empHistory,this);
listEmployeeHistory.setAdapter(employmentHistoryAdapter);
}
//this is my Layout file where i added the ListView and I have the custom design for list_row
<android.support.v7.widget.CardView
android:id="#+id/employmentHistoryCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/employmentHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#E0E0E0"
android:gravity="center"
android:text="Employment History"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textColor="#4A148C" />
<TextView
android:id="#+id/addEmployment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/employmentHistory"
android:layout_marginTop="15dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:drawableLeft="#drawable/ic_add"
android:drawablePadding="20dp"
android:drawableStart="#drawable/ic_add"
android:paddingLeft="30dp"
android:text="Add Employment"
android:textColor="#0e89d0" />
<ListView
android:id="#+id/listEmployeeHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/addEmployment">
</ListView>
</RelativeLayout>
</android.support.v7.widget.CardView>
You need to specify certain height for ListView in the xml code. WRAP_CONTENT will not expand your ListView when data is inflated into it. Specify some height like 200dp or something.
I have Listview that contains a Textview, I populate data from sqlite inside them , now I want to change the textview background color for each row based on the value in each one.
I couldn't do it , can anybody help me for how to get each textview from the listview ??
Edit :
Here is my code to populate the listview:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Group, SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.grop, R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
this is the XML file for the items :
<?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:orientation="horizontal"
android:background="#drawable/grp_shap" >
<ImageView android:id="#+id/Prio"
android:layout_width="32dip"
android:layout_height="32dip"
/>
<TextView
android:id="#+id/grop"
android:layout_width="64sp"
android:layout_height="40sp"
android:layout_alignBottom="#+id/Prio"
android:layout_alignParentLeft="true"
android:text="S"
android:textSize="12sp" />
<TextView
android:id="#+id/DT"
android:layout_width="wrap_content"
android:layout_height="14sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/title"
android:gravity="right"
android:text="Date Rec"
android:layout_marginRight="5dip"
android:textSize="12sp"
/>
<TextView
android:id="#+id/bdy"
android:layout_width="fill_parent"
android:layout_height="28sp"
android:layout_below="#+id/DT"
android:layout_toLeftOf="#+id/hi"
android:layout_toRightOf="#+id/Prio"
android:text="Body"
android:textSize="12sp" />
<TextView
android:id="#+id/sbj"
android:layout_width="fill_parent"
android:layout_height="12sp"
android:layout_alignLeft="#+id/bdy"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/bdy"
android:fontFamily="sans-serif"
android:text="Subject"
android:textSize="12sp" />
<ImageView
android:id="#+id/hi"
android:layout_width="20dip"
android:layout_height="26dip"
android:layout_alignLeft="#+id/DT"
android:layout_below="#+id/DT"
android:layout_marginLeft="15dp"
android:src="#drawable/highprio" />
</RelativeLayout>
and I have a listview on the activity_mail layout which populated whith this items.
I want to change the color for the textview with id grop based on the value inserted on it
You should use a custom adapter:
public class MyAdapter extends BaseAdapter {
private static class ViewHolder {
public final TextView mTv;
}
private final Context mContext;
private final List<String> mList; //you can change this to String array
public MyAdapter (Context context, List<String> list) {
this.mContext = context;
this.mList= list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
final String string = mList.get(position);
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_listview, null);
h = new ViewHolder();
h.mTv= (TextView) convertView .findViewById(R.id.mTvRes);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.mTv.setText....
h.mTv.setTextColor....
h.mTv.setBackground....
return convertView;
}
}
in your activity:
List<String> list = Arrays.asList(Fields);
MyAdapter adapter = new MyAdapter(this, list);
NotiLst.setAdapter(adapter);
I didn't checked it and you maybe need to do some changes but it should work.
If you have something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder.label.setBackground(getResources().getColor(color.white));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.label.setText(activitiesList.get(position).getName());
return convertView;
}
Im working on simple feedreader app. in my main activity i have listview that show all items . in each row i have imagebutton for save that item and when user click on that the image of imagebutton must be change(like fav icon on twitter) but it does not change ! After scrolling changes are observed !
this is my row layout .
<?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:descendantFocusability="blocksDescendants" >
<TextView
android:id="#+id/tvListViewItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:text="#string/listview_item_title"
android:textSize="25dp" />
<TextView
android:id="#+id/tvListViewItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvListViewItemTitle"
android:layout_below="#+id/tvListViewItemTitle"
android:layout_marginTop="20dp"
android:text="#string/listview_item_text"
android:textSize="18dp" />
<TextView
android:id="#+id/tvListViewItemTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvListViewItemText"
android:layout_below="#+id/tvListViewItemText"
android:layout_marginTop="22dp"
android:paddingTop="5dp"
android:text="#string/listview_item_time" />
<ImageButton
android:id="#+id/ibListViewItemSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/tvListViewItemTime"
android:layout_marginLeft="20dp"
android:paddingTop="11dp"
android:src="#drawable/icon_save_story_2x"
android:background="#null" />
and this is my custom adapter class.
public class FeedListAdapter extends ArrayAdapter<FeedItem> {
private final Activity context;
private final List<FeedItem> list;
public FeedListAdapter(Activity context, List<FeedItem> list) {
super(context, R.layout.feed_listview_item_type1, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
TextView tvTitle;
TextView tvText;
TextView tvTime;
ImageButton ibSave;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.feed_listview_item_type1, parent,false);
// configure view holder
ViewHolder holder = new ViewHolder();
holder.tvTitle = (TextView)rowView.findViewById(R.id.tvListViewItemTitle);
holder.tvText = (TextView)rowView.findViewById(R.id.tvListViewItemText);
holder.tvTime = (TextView)rowView.findViewById(R.id.tvListViewItemTime);
holder.ibSave = (ImageButton)rowView.findViewById(R.id.ibListViewItemSave);
rowView.setTag(holder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
final FeedItem feedItem = list.get(position);
holder.tvTitle.setText(feedItem.getTitle());
holder.tvText.setText(feedItem.getText());
holder.tvTime.setText(feedItem.getTime());
if(feedItem.isSave()) holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
else holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
final String msg = " "+(position+1);
holder.ibSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(feedItem.isSave()){
feedItem.setSave(false);
Toast.makeText(context,
msg+"deleted", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
}else{
feedItem.setSave(true);
Toast.makeText(context,
msg+"saved", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
}
}
});
return rowView;
}
}
Call notifyDataSetChanged in your onClick after the if else.
At present the FeedListAdapter does not realize that you have made some changes to the list
i have a problem in my app:
i use a listview with a personalized adapter,
in this adapter i want to change the color of the line depending on whether the message is read or not.
In the metod GETVIEW i control a variable, if it is equal to 0 i want to change the background color.
All works and the list is displayed as i want,
but when there are a lot of elements and the list is scrolled in any direction (from top to bottom and vice versa) the raws are displeyed with the same color even if by code is set another color.
Has anyone ever had the same problem?
You can advise me something about it?
There is the code of my Adapter:
public class LazyAdapterComunicazioni extends BaseAdapter {
private Activity activity;
private String[] id;
private String[] titolo;
private String[] data;
private String[] letto;
private static LayoutInflater inflater=null;
//public ImageLoader imageLoader;
public LazyAdapterComunicazioni(Activity a, String[] idCom, String[] titoloCom, String[] dataCom, String[]lettoCom) {
activity = a;
id = idCom;
titolo = titoloCom;
data = dataCom;
letto = lettoCom;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return id.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null)
{
vi = inflater.inflate(R.layout.comunicazionicslist, null);
}
ContactsViewHolder viewHolder = new ContactsViewHolder();
//Settimane
viewHolder.txtTitolo=(TextView)vi.findViewById(R.id.comCS_Titolo);
viewHolder.txtTitolo.setText(titolo[position].toString());
//Data
viewHolder.txtData=(TextView)vi.findViewById(R.id.comCS_Data);
viewHolder.txtData.setText(data[position].toString());
//ID
viewHolder.txtID=(TextView)vi.findViewById(R.id.comCS_ID);
viewHolder.txtID.setText(id[position].toString());
//Connessianne e Apretura del DB
String read = letto[position].toString();
if (read.equals("0")) //DA LEGGERE
{
//LAYOUT
viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA);
viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
viewHolder.txtTitolo.setTextColor(Color.WHITE);
}
return vi;
}
static class ContactsViewHolder {
TextView txtTitolo;
TextView txtData;
TextView txtID;
RelativeLayout rel;
}
}
and the xml of the single row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/comCS_RIGA"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="#+id/comCS_Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:textSize="12sp"
/>
<TextView
android:id="#+id/comCS_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/comCS_Data"
android:visibility="invisible"
/>
<TextView
android:id="#+id/comCS_Titolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/comCS_Data"
android:layout_marginTop="2dp"
android:layout_marginLeft="16dp"
android:text=""
android:textColor="#357cbc"
android:textSize="18sp"
/>
<ImageView
android:id="#+id/feedbackCars_Positivo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/frecciacom"
android:contentDescription="#string/title_Comunicazioni"
android:layout_alignParentRight="true"
android:layout_marginTop="2dp"
android:layout_marginRight="16dp"
/>
</RelativeLayout>
It's a common error.
Views are recycled so you have to set back default values in any cases.
if (read.equals("0")) //DA LEGGERE
{
//LAYOUT
viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA);
viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
viewHolder.txtTitolo.setTextColor(Color.WHITE);
}
else {
viewHolder.rel.setBackgroundResource("you_defaulf_bg_res");
}
First of all, you are not using the ViewHolder pattern correctly. The ViewHolder is designed to hold references to the Views of the list-item and minimize the findViewById(...) calls.
That purpose is not fulfilled by your code. The reason for the behaviour you described lies in the Views not being recycled.
Do it like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ContactsViewHolder viewHolder;
if(convertView == null) {
vi = inflater.inflate(R.layout.comunicazionicslist, null);
viewHolder = new ContactsViewHolder();
viewHolder.txtTitolo=(TextView)vi.findViewById(R.id.comCS_Titolo);
viewHolder.txtData=(TextView)vi.findViewById(R.id.comCS_Data);
viewHolder.txtID=(TextView)vi.findViewById(R.id.comCS_ID);
viewHolder.rel = (RelativeLayout)vi.findViewById(R.id.comCS_RIGA);
vi.setTag(viewHolder);
} else {
viewHolder = (ContactsViewHolder) vi.getTag();
}
//Settimane
viewHolder.txtTitolo.setText(titolo[position].toString());
//Data
viewHolder.txtData.setText(data[position].toString());
//ID
viewHolder.txtID.setText(id[position].toString());
//Connessianne e Apretura del DB
String read = letto[position].toString();
if (read.equals("0")) //DA LEGGERE {
viewHolder.rel.setBackgroundResource(R.drawable.sfondorigacomcs);
viewHolder.txtTitolo.setTextColor(Color.WHITE);
} else {
viewHolder.rel.setBackgroundResource(R.drawable.NORMAL_BACKGROUND);
}
return vi;
}
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.