textview of listview in android - android

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;
}

Related

ListView shows only One Row

I have simple list which suppose to display all the rows from DB table. However, it shows only one row in the list view.
Though, it receives all the rows from the DB (verified the size of configArrayList), but displays only the first row and discards others.
Here is the Activity and Custom Adapter.
Main Activity :
ArrayList<HashMap<String, String>> configArrayList = dbTools.getAllConfig(); // Returns 4 rows
ArrayList<ConfigEntity> configMenu = new ArrayList<>();
if (!(configArrayList.isEmpty()))
{
Iterator<HashMap<String, String>> iterator = configArrayList.iterator();
while (iterator.hasNext())
{
HashMap<String, String> configMap = iterator.next();
// ConfigEntity contains two members Name, ID and their setter, getter
ConfigEntity entity = new ConfigEntity();
entity.setID(configMap.get("ID"));
entity.setName(String.valueOf(configArrayList.size()));
// Set Name as Size to verify the list size is grater than 1. (Size is 4)
configMenu.add(entity);
}
}
if (!(configMenu.isEmpty()))
{
ListAdapter adapter = new ConfigListAdapter(this, configMenu);
ListView theListView = (ListView) findViewById(R.id.config_list_listView);
theListView.setAdapter(adapter);
}
Here is the Custom Adapter :
public class ConfigListAdapter extends BaseAdapter {
private ArrayList<ConfigEntity> entityList;
private LayoutInflater layoutInflater;
public ConfigListAdapter(Context context, ArrayList<ConfigEntity> entityList) {
this.entityList = entityList;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return entityList.size();
}
#Override
public Object getItem(int position) {
return entityList.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.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) convertView.findViewById(R.id.config_list_row_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView idTextView;
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:id="#+id/config_list_addConfig_button"
android:layout_gravity="right"
android:onClick="addNewConfig" />
<ListView
android:id="#+id/config_list_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp">
</ListView>
</LinearLayout>
</ScrollView>
config_list_listView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin_20dp">
android:layout_marginRight="#dimen/margin_20dp">
<TextView
android:id="#+id/config_list_row_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="#dimen/margin_10dp"
android:layout_marginLeft="#dimen/margin_20dp"
android:longClickable="true" />
<TextView
android:id="#+id/config_list_row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Please try this.
Change getView Method as follows
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=convertView;
ViewHolder holder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) view.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) view.findViewById(R.id.config_list_row_id);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return view;
}
and Declare View view; as class level variable;
Hope it helps Thanks. Refer here :-https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/

ImageView size issue within ListView

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"/>

Can't find TextView in recursive custom Listview adapter

I created this class called GeoArea, which is suppose to store "Geographical Area" that have children Geographical Areas, this is fairly strait foward:
public class GeoArea {
public String id;
public String name;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
subGeoAreas = new LinkedList<GeoArea>();
}
#Override
public String toString() {
return name;
}
}
I have created the following Layout to render it on Android, the idea here is to for each GeoArea to recursively render it self and then it's children GeoArea in a listView:
//layout_geo_area.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtGeoAreaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Geo Area Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtGeoAreaName"
android:gravity="left" >
</ListView>
</RelativeLayout>
This is my adapter I created for GeoArea to be displayed in a listView:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private ArrayList<GeoArea> _myGeoArea;
private Context _myContext;
LayoutInflater _inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_myGeoArea = myGeoArea;
_inflater = LayoutInflater.from(context);
_myContext = context;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
GeoAreaLayoutHolder holder;
if (convertView == null) {
convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);
holder = new GeoAreaLayoutHolder();
holder.txtGeoAreaName = (TextView)convertView.findViewById(R.id.txtGeoAreaName);
holder.txtGeoAreaName.setTag(convertView);
holder.listViewChildGeoAreas = (ListView)convertView.findViewById(R.id.listViewChildGeoAreas);
holder.listViewChildGeoAreas.setTag(convertView);
} else {
holder = (GeoAreaLayoutHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.txtGeoAreaName.setText(curGeoArea.name);
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
}
return convertView;
}
static class GeoAreaLayoutHolder {
public TextView txtGeoAreaName;
public ListView listViewChildGeoAreas;
}
}
And here is my Activity that I am using to set it all up:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_area);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
}
}
When I try to run it, I get the error nullPointerException on the line:
holder.txtGeoAreaName.setText(curGeoArea.name);
What am I doing wrong?
You may want to check ExpandableListView may suit your needs better
http://developer.android.com/reference/android/widget/ExpandableListView.html
An example # http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Continuing from my previous answer to your question ( i though that solved your problem)
To display just name in your listview
list_row.xml // this is the layout with textview to be inflated in getView.
Each row will have textview
<?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" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
layout_geo_area.xml // with only listview no textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left" >
</ListView>
</RelativeLayout>
Now your adapter class
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
// inflate list_row.xml with textview
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
You have a custom adapter.
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
Now you set the adapter to listview like below
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
So why do you need the below. remove these
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);

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.

ActionBarSherlock list navigation with icon and title

I am developing an application where navigation is done by using the spinner on the action bar. Spinner items needed to include icons as well so I created custom layouts for that.The problem is that spinner is too wide with respect to its items. I am using ActionBarSherlock. The codes I wrote so far:
navigation_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:padding="4dip"/>
<TextView
android:id="#+id/title"
style="?attr/spinnerItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:layout_gravity="center_vertical"/>
</LinearLayout>
navigation_list_dropdown_item.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:layout_gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:adjustViewBounds="true"
android:padding="4dip"/>
<TextView
android:id="#+id/title"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true" />
</LinearLayout>
NavigationListAdapter.java:
public class NavigationListAdapter extends BaseAdapter{
private Drawable[] mIcons;
private String[] mTitles;
private Context mContext;
private LayoutInflater mInflator;
public NavigationListAdapter(Context context, Drawable[] icons, String[] titles){
mContext = context;
mInflator = (LayoutInflater)mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mIcons = icons;
mTitles = titles;
}
#Override
public int getCount() {
return mTitles.length;
}
#Override
public Object getItem(int position) {
return mTitles[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 = mInflator.inflate(R.layout.navigation_list_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflator.inflate(R.layout.navigation_list_dropdown_item, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(mTitles[position]);
holder.icon.setImageDrawable(mIcons[position]);
return convertView;
}
private class ViewHolder{
public ImageView icon;
public TextView title;
}
}
onCreate method of the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getSupportActionBar().getThemedContext();
TypedArray ta = getResources().obtainTypedArray(R.array.leagues);
Drawable[] logos = new Drawable[ta.length()];
for(int i = 0; i < logos.length; i++){
logos[i] = ta.getDrawable(i);
}
NavigationListAdapter adapter = new NavigationListAdapter(context, logos,
getResources().getStringArray(R.array.league_names));
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, null);
}
An illustration of the problem:
Maybe changing the width of your textview in navigation_list_dropdown_item.xml to wrap_content take effect (if changing width of LinearLayout does not work). You can see this to: http://dandar3.blogspot.com.es/2013/03/actionbarsherlock-custom-list-navigation.html .

Categories

Resources