android - No Scrolling on Custom Spinner Items - android

I am creating a custom spinner in my application. The items are displaying perfectly, but the issue is, the list items are not getting scrolled.
Please refer the image below. In the image below, I am using plenty of countries but not being able to scroll for other country options.
This is my complete spinner code below:
main_activity.xml
<android.support.percent.PercentRelativeLayout
android:id="#+id/layoutSpinner"
android:layout_centerHorizontal="true"
android:background="#drawable/bg_spinner_countrycode"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
app:layout_widthPercent="25%">
<Spinner
android:id="#+id/spCountryCode"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.percent.PercentRelativeLayout>
MainActivity.java:
Spinner spCountryCode = (Spinner) findViewById(R.id.spCountryCode);
CountryCodeAdapter countryCodeAdapter = new CountryCodeAdapter(LoginScreenActivity.this, modelList);
spCountryCode.setAdapter(countryCodeAdapter);
CountryCodeAdapter.java:
public class CountryCodeAdapter extends ArrayAdapter<CountryCodeModel> {
Activity activity;
List<CountryCodeModel> itemList;
LayoutInflater inflater;
public CountryCodeAdapter(Activity activity, List<CountryCodeModel> itemList) {
super(activity, R.layout.country_code_spinner_layout, itemList);
this.activity = activity;
this.itemList = itemList;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);
ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);
Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imgFlag.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
imgFlag.setLayoutParams(lp);
//lblCountryName.setText(itemList.get(position).getStrCountryName());
lblCountryName.setVisibility(View.GONE);
//lblCountryCode.setText("(" + itemList.get(position).getStrCountryCode() + ")");
lblCountryCode.setVisibility(View.GONE);
return itemView;
}
#Override
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View itemView = inflater.inflate(R.layout.country_code_spinner_layout, parent, false);
ImageView imgFlag = (ImageView) itemView.findViewById(R.id.imgFlag);
TextView lblCountryName = (TextView) itemView.findViewById(R.id.lblCountryName);
TextView lblCountryCode = (TextView) itemView.findViewById(R.id.lblCountryCode);
Picasso.with(activity).load(itemList.get(position).getStrCountryFlagUrl()).into(imgFlag);
lblCountryName.setText(itemList.get(position).getStrCountryName());
lblCountryCode.setText(itemList.get(position).getStrCountryCode());
return itemView;
}
}
Also, I have also tried, countryCodeAdapter.setDropDownViewResource(R.layout.country_code_spinner_layout);, just before spCountryCode.setAdapter(countryCodeAdapter);, but still no success.
Also, if you want to see my custom spinner layout, here is it.
country_code_spinner_layout.xml:
<?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="#dimen/countryCodeSpinnerDropDownHeight">
<ImageView
android:id="#+id/imgFlag"
android:layout_width="#dimen/countryCodeSpinnerImageSize"
android:layout_height="#dimen/countryCodeSpinnerImageSize"
android:layout_marginLeft="#dimen/countryCodeSpinnerMarginLeft"
android:layout_centerVertical="true"/>
<com.base.silpre.Font_TextViewOpenSans_SemiBold
android:id="#+id/lblCountryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgFlag"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/countryCodeSpinnerMarginLeft"
android:textSize="#dimen/countryCodeSpinnerTextsSize"
android:textColor="#android:color/black"/>
<com.base.silpre.Font_TextViewOpenSans_Italic
android:id="#+id/lblCountryCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/lblCountryName"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/countryCodeSpinnerMarginLeft"
android:textSize="#dimen/countryCodeSpinnerTextsSize"
android:textColor="#android:color/black"/>
</RelativeLayout>
Please revert back if anyone has a solution for scrolling the custom spinner.

Try wrap_content for height
`< Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>`

Related

Why ListView onClick does not work?

Trying to implement a onlick functionality for the android listview but something seems to not working. I tried to find some solutions over the internet and I found that there were recommendation to change the focusable property of imageview and textview elements. Tried it and it did not work. Appreciate if you guys can recommend any other alternatives.
Layout item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/rootItemId"
android:clickable="true"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable='false'
android:background="#drawable/frame">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="#string/hello_world"
android:padding="5dp"
android:focusable='false'
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:focusable='false'
android:layout_toRightOf="#id/ivIcon">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable='false'
android:textAppearance="#android:style/TextAppearance.Medium"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
list fragment
public class EventsListFragment extends ListFragment{
ArrayList<Event> eventsChildren;
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.e("RedditListingsClick",position + " " + id);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eventsChildren = new ArrayList<Event>();
EventsChildAdapter adapter = new EventsChildAdapter(inflater.getContext(), eventsChildren);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
list adapter
public class EventsChildAdapter extends ArrayAdapter<Event> {
Context context;
RelativeLayout root;
private static class ViewHolder {
TextView title;
ImageView thumbnail;
}
public EventsChildAdapter(Context context, ArrayList<Event> eventsChildren) {
super(context, R.layout.item_listing, eventsChildren);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Event child = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_listing, null);
//root=(RelativeLayout)convertView.findViewById(R.id.rootItemId);
viewHolder.title = (TextView) convertView.findViewById(R.id.tvTitle);
//viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivIcon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
System.out.println(child.getTitle());
viewHolder.title.setText(child.getTitle());
Picasso.with(this.context).load(child.getThumbnailImageURL()).resize(200, 100).into(viewHolder.thumbnail);
// Return the completed view to render on screen
return convertView;
}
}
In addition to the above, I have already tried adding android:descendantFocusability="blocksDescendants" to relativelayout but still did not work. Finally I tried to make the root relativelayout clickable and removed focus from the other elements and this did not work either.
You can try this:
public class EventsListFragment extends ListFragment implement OnItemClickListener {
...
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// the child callback here.
}
}
and this in onActivityCreated
setListAdapter(Your adapter);
getListView().setOnItemClickListener(this);

ListView Inside CardView not Showing its full length

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.

How to Hide or Kill The White Space Below The Spinner Drop-Down List

I have successfully implemented Spinners in my project using the Spinner class which exists in this link:
How to make an Android Spinner with initial text "Select One"
Also, I customized the layout of each item and named it as spinner_entries_style.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="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
Also, These are the Adapter classes I used in my code..
class LoanTypeAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanTypeAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
// ------------------------------------------
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
And this..
class LoanProgramAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanProgramAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
But, there is something strange I encountered after building the spinners. When running the app in Android versions (4.2 or less), there is a white space below the dropdown list.
Here are the screenshots of what happens..
http://www.mediafire.com/view/cqj51t8e3aju18m/01.png
http://www.mediafire.com/view/ure788yetrt00v3/02.png
That is not just on the emulator, but also in the real devices having Android 4.2 or less. Also, this white space seems a bit bigger in some devices.
Is there any idea to hide or kill these white area? Any solution for this problem?
I found the solution of this problem after reading this post.
How to wrap lengthy text in a spinner
In my layout called spinner_entries_style.xml, I should specify the height of text view like that:
android:layout_height="?android:attr/listPreferredItemHeight"
And it will be like that:
<?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="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
That solved my problem successfully..
Remove this line from spinner_entries_style.xml.
android:layout_marginBottom="2dp"

Create CustomAdapter for listView with different row layouts

I want to add different rows to a ListView. In first row I want to show a textView. In second Two imageViews and third row Single textView. Then a list of text with icon. I know this is possible using custom CustomAdapter. But not understanding how to proceed.
My CustomAdapter :
public class CustomList extends ArrayAdapter<String>
{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.drawer_list_item, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.drawer_list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.drawer_imgIcom);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
In your drawer_list_item xml file create all the elements you need, text, images etc, positioned as desired. Then in the getView() after the inflate, simple hide and show these elements as desired based on the position.
Remember to hide and show, because Android will re-use the views.
Also you should consider using a viewHolder as this will improve performance.
try this code in getview(),
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.drawer_list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.drawer_imgIcom);
ImageView imageView1 = (ImageView) rowView.findViewById(R.id.drawer_imgIcom1);
if(position == 0)
{
txtTitle.setText(web[position]);
imageView.setvisibility(View.GONE);
imageView1..setvisibility(View.GONE);
}
else if(Position == 1){
txtTitle.setText(web[position]);
imageView.setvisibility(View.Visible);
imageView1..setvisibility(View.Visible);
imageView.setImageResource(imageId[position]);
imageView1.setImageResource(imageId1[position]);
}else
{
txtTitle.setText(web[position]);
imageView.setvisibility(View.GONE);
imageView1..setvisibility(View.GONE);
}
return rowView;}
and then corresponding views use in your xml layouts.
First you need your custom view (this contains the Views that you need for your purpose)
example:
<?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">
<ImageView
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#android:color/holo_blue_light"
android:contentDescription="#string/cd_pr_photo"
android:scaleType="centerCrop" />
<!--Invisible divider so we can put our views just in the
right place :) -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/photo" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/divider"
android:layout_margin="10dp"
android:text="#string/sample_title"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold|italic" />
<ImageButton
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/divider"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:background="#android:drawable/ic_menu_share"
android:contentDescription="#string/cd_the_sharebutton" />
</RelativeLayout>
Once you have decided your layout you extend ArrayAdapter in your CustomAdapterClass.
Example:
#Override
public View getView(int position, View v, ViewGroup parent) {
//In case we don't have an inflated view, inflate.
if (v == null) {
v = View.inflate(getContext(), R.layout.custom_puertorico_layout, null);
}
//The domain object with data.
PuertoRico p = getItem(position);
//Photo of Puerto Rico
final ImageView photo = (ImageView) v.findViewById(R.id.photo);
photo.setImageDrawable(p.getPicture());
//Given Title
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(p.getTitle());
//Extra stuff...
ImageButton share = (ImageButton) v.findViewById(R.id.share);
return v;
}
This easy example and the full code can be found at https://github.com/JRSosa/PRPics
Hope it Helps.

findViewByID returning null on BaseAdapter

I started using fragments to develop a new app, one of the fragments is a ListFragment,
when I use the adapter I made to show the list's rows I get some null from some findViewById that I use on the getView() method.
The structure is:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</RelativeLayout>
frag_detail.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_gravity="center"/>
</LinearLayout>
detail_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/detailItemLayout" >
<ImageButton
android:layout_width="64dp"
android:layout_height="64dp"
android:id="#+id/imgIcon"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="txtTitle"
android:id="#+id/txtTitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="label1"
android:id="#+id/label1"
android:layout_below="#+id/imgIcon"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
DetailFragment class
public class DetailFragment extends ListFragment {
private Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_detail,container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MonsterDataSource ds = new MonsterDataSource(context);
List<Monster> lsMonster = ds.getAllMonsters();
//ListView lsView = (ListView)this.getActivity().findViewById(R.id.lsDetail);
MonsterListAdapter adapter = new MonsterListAdapter(context,lsMonster);
//lsView.setAdapter(adapter);
setListAdapter(adapter);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = activity.getApplicationContext();
}
}
And the MonsterListAdapter class
public class MonsterListAdapter extends BaseAdapter {
static class ViewHolder {
private TextView txtTitle;
private ImageView imgIcon;
private TextView txtHealth;
}
private Context context;
private LayoutInflater inflater;
private List<Monster> data;
private ViewHolder holder;
public MonsterListAdapter(Context c, List<Monster> data){
context = c;
this.data = data;
inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/*Basic BaseAdapter Methods*/
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
holder = new ViewHolder();
/*Those Guys get null*/
RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.detailItemLayout);
holder.txtTitle = (TextView) view.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView) view.findViewById(R.id.imgIcon);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Monster monster = data.get(i);
holder.txtTitle.setText(monster.name);
holder.imgIcon.setImageResource(monster.imgResID);
holder.txtHealth.setText(monster.health);
return view;
}
}
In case anyone asking, I'm trying to retrieve the relative layout to add some controls dynamically after.
Thanks
You posted the XML for detail_list_item.xml but in your code you inflate menu_list_item.xml.
detail_list_item.xml contains all of the IDs you are trying to find so it appears you have accidentally inflated the wrong View in getView
Additionally, you should remove your ViewHolder as a member variable and place it as a local variable in the getView method. You are reusing that variable for multiple views at the same time which means you have the potential for modifying the wrong views at different positions (data going to the wrong positions, etc).
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
...
...//all of your other code here
}
Try without affecting "view"
RelativeLayout layout = (RelativeLayout)findViewById(R.id.detailItemLayout);
holder.txtTitle = (TextView) findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView) findViewById(R.id.imgIcon);
Edit This works for me... My texviews nbCommentaires and Comment give NullPointerException if i inflate View to them... I thought you get the same issue...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.comment_row, null);
}
v.setTag(position);
Commentaires o = Global_reco.CommRecos.get(position);
if (o != null) {
TextView nom = (TextView) v.findViewById(R.id.nom_com);
TextView com = (TextView) v.findViewById(R.id.com);
ImageView photo = (ImageView) v.findViewById(R.id.profile_com);
TextView nbCommentaires = (TextView) findViewById(R.id.nb_commentaires);
TextView Comment = (TextView)findViewById(R.id.nbre_comment);
photo.setTag(position);

Categories

Resources