I am attempting to display a listview in a fragment. However, nothing is displaying and the logging seems correct. The screen is completely blank. I thought the cause was an error in inflating the view, but I just have no clue. I have researched SO and other websites with no luck.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v("FeaturesFragment", "FeaturesFragment");
//View v = inflater.inflate(R.layout.carfeatures_list, container, false);
//View v = inflater.inflate(R.layout.features_list_item, container, false);
View v = inflater.inflate(R.layout.features_list_item, null);
//View v = inflater.inflate(R.layout.carfeatures_list,null);
ListView lv = (ListView) v.findViewById(android.R.id.list);
Log.v("ListView lv1-features", lv.toString());
adapter = new CustomListViewAdapter(carFeaturesList, R.layout.carfeatures_list, super.getActivity());
//adapter = new CustomListViewAdapter(carFeaturesList, R.layout.features_list_item, super.getActivity());
lv.setAdapter(adapter);
Log.v("ListView lv2-features", lv.toString());
adapter.notifyDataSetChanged();
//Listview on item click listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Gets values from selected ListItem
String car_features = ((TextView) view.findViewById(R.id.car_features)).getText().toString();
//Log.v("onCreate-car_features", car_features.toString());
String carfeatures_id = ((TextView) view.findViewById(R.id.carfeatures_id)).getText().toString();
//Log.v("onCreate-carfeatures_id", carfeatures_id.toString());
String model_id = ((TextView) view.findViewById(R.id.model_id)).getText().toString();
//Log.v("onCreate-model_id", model_id.toString());
String carfeatures_desc = ((TextView) view.findViewById(R.id.carfeatures_desc)).getText().toString();
//Log.v("onCreate-carfeats_desc", carfeatures_desc.toString());
Boolean b = Boolean.valueOf(TAG_CARID);
if (b == true) {
view.setBackgroundColor(Color.GREEN);
}
//Colors the selected listview item
if (previouslySelectedItem != null) {
previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT);
//Log.v("previouslySelectedItem", "!=null");
//Log.v("position", Integer.toString(position));
}
view.setBackgroundColor(Color.GREEN);
//Log.v("position", Integer.toString(position));
//Log.v("outofif-view", view.toString());
previouslySelectedItem = view;
}
});
//Calls async task to get json
new GetCarFeatures().execute();
Log.v("return called-Features", "return called");
Log.v("v", v.toString());
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
CarFeatures item = getItem(position);
//Log.v("CarFeaturesitem", Integer.toString(position));
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(getContext());
convertView = mInflater.inflate(R.layout.carfeatures_list, null);
//convertView = mInflater.inflate(R.layout.features_list_item, null);
Log.v("getView", "getView");
Log.v("View convertView", convertView.toString());
Log.v("ViewGroup parent", parent.toString());
holder.carfeatures_id = (TextView) convertView.findViewById(R.id.carfeatures_id);
//Log.v("if-holder.carfeat_id", holder.carfeatures_id.toString());
holder.model_id = (TextView) convertView.findViewById(R.id.model_id);
//Log.v("if-holder.model_id", holder.model_id.toString());
holder.carfeatures_desc = (TextView) convertView.findViewById(R.id.carfeatures_desc);
//Log.v("if-holder.carfeat_id", holder.carfeatures_desc.toString());
convertView.setTag(holder);
//Log.v("if-convertView", convertView.toString());
//Log.v("if-holder", holder.toString());
} else {
holder = (ViewHolder) convertView.getTag();
//Log.v("else-convertView", convertView.toString());
//Log.v("else-holder", holder.toString());
}
holder.carfeatures_id.setText(item.carfeatures);
//Log.v("holder.carfeatures_id", holder.carfeatures_id.toString());
holder.model_id.setText(item.modelid);
//Log.v("holder.model_id", holder.model_id.toString());
holder.carfeatures_desc.setText(item.carfeaturesdesc);
//Log.v("holder.carfeatures_desc", holder.carfeatures_desc.toString());
adapter.getItemId(position);
//Log.v("getitemid1", Long.toString(adapter.getItemId(position)));
Log.v("convertView", convertView.toString());
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/car_features"
android:layout_below="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_id"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/car_features" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_desc"
android:layout_above="#+id/car_features"
android:layout_alignParentStart="true" />
</RelativeLayout>
I might be wrong, but it seems that in your
public View getView(int position, View convertView, ViewGroup parent)
you are trying to inflate this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/car_features"
android:layout_below="#+id/model_id"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_id"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/car_features" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/model_id"
android:layout_below="#+id/header"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/carfeatures_desc"
android:layout_above="#+id/car_features"
android:layout_alignParentStart="true" />
<ListView
android:id="#android:id/list"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentStart="true" />
</RelativeLayout>
which will not work, because you need to inflate an item of the list (not the layout containig the list), an item, which you will fill with your data from array. I would suggest you to create a layout for that item, name it list_item.xml and inflate it.
Oh, and in case you have this item, please show it to us
Related
I don't understand what's going wrong, when clicking an item on the list nothing appends.
my environment is minsdk:21, maxsdk:27, java 8 (openjdk), android-studio 3.1.4, terminal android 6.0
note : using FragmentManager or SupportFragmentManager provides the same result.
the fragment list layout
<?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:orientation="vertical"
android:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list"
android:dividerHeight="1sp"
android:divider="#color/blue_serenity_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
android:background="#color/blue_serenity_transparent"
android:columnCount="3"
android:rowCount="2"
>
<ImageView
android:id="#+id/notif_type"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_column="1"
android:layout_row="0"
android:textSize="18dp"
android:textColor="#color/black"
/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="1"
/>
</GridLayout>
</LinearLayout>
list setup
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<NotificationBean> data = new NotificationBean().list(getActivity().getBaseContext());
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
list = (ListView) v.findViewById(R.id.list);
listAdapter = new Adapter(getActivity().getBaseContext(), data);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "row clicked");
}
});
return v;
}
List adapter
class NotificationsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater = null;
private List<NotificationBean> notifications;
public NotificationsAdapter(Context context, List<NotificationBean> notifications){
this.context = context;
this.notifications = notifications;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return notifications.size();
}
#Override
public Object getItem(int position) {
return notifications.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.fragment_notifications_row, null);
NotificationBean n = notifications.get(position);
GridLayout layout = (GridLayout) v.findViewById(R.id.row_layout);
if (dark) layout.setBackgroundColor(getResources().getColor(R.color.blue_serenity_transparent));
else layout.setBackgroundColor(getResources().getColor(R.color.white));
dark = !dark;
ImageView notifType = (ImageView) v.findViewById(R.id.notif_type);
notifType.setImageResource(n.getIcon());
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(n.getTitle());
TextView date = (TextView) v.findViewById(R.id.date);
date.setText(Constants.DTF.format(n.getDate()));
return v;
}
}
I have a List View which has layout:
list.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.phongponix.trackingbodybuilding.MainActivity$PlaceholderFragment">
<ListView android:id="#+id/lvExerciseList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="50dp"
></ListView>
<View
android:layout_width="match_parent"
android:layout_height="100dp"></View>
</RelativeLayout>
list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgExcercisePhoto"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView android:text="aaa"
android:id="#+id/tvExerciseName"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvExcerciseRecords"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</android.support.v7.widget.RecyclerView>
</TableRow>
</TableLayout>
</LinearLayout>
I setup CustomAdapter for ListView and it works fine.
Now i want setup Adapter for RecyclerView which will be Horizontal orientation. Something like this in my list Adapter
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.tracking_plan_list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
The problem is it will cause this error:
java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager#48264ed is already attached to a RecyclerView: android.support.v7.widget
If i commented the line recyclerView.setLayoutManager(linearLayoutManager); My recycler adapter will never execute the onCreateViewHolder or onBindViewHolder. I checked the data for this recycler already and it's not an empty array.
What should i do to have both List View with Vertical Scroll and Recycler View with Horizontal scroll in side a list item??
Change the linearLayoutManager from global to local, like this :
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.tracking_plan_list_item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvExerciseName);
view.setTag(viewHolder);
recyclerView = (RecyclerView)view.findViewById(R.id.rvExcerciseRecords);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(trackingPlanHorizontalAdapter);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (data.size() > 0) {
ExerciseModel exercise = (ExerciseModel) data.get(i);
viewHolder.title.setText(exercise.getTitle());
} else {
viewHolder.title.setText("No data");
}
return view;
}
I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.
I have an listView with TextView and another horizontal listView corresponding to that listView. now i need to write onclick event for list item in the horizontal listview.i can't get the parent list item position(vertical listview position).please help me. Thanks in advance.
// First Listview for Vertical
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
</RelativeLayout>
//Adapter Getview for first list
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(mArrayList.get(position).getSite());
typeId = mArrayList.get(position).getTypeId();
hori_view = (TwoWayView) convertView.findViewById(R.id.hor_list);
mChildList = mArrayList.get(position).getChildArrList();
HorizontalAdapter horListAdapter = new HorizontalAdapter(mContext, R.layout.hori_list,mChildList);
hori_view.setAdapter(horListAdapter);
horListAdapter.notifyDataSetChanged();
hori_view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mChildList = mArrayList.get(position).getChildArrList(); // Here i need parent list position
String text = mChildList.get(position).getChannel().toString();
int cId = mChildList.get(position).getChannelId();
int pen = mChildList.get(position).getPending();
int typeId = mChildList.get(position).getSiteid();
Intent intent = new Intent(mContext,ChannelDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putString("channel", text);
bundle.putInt("typeId", typeId);
intent.putExtras(bundle);
mContext.startActivity(intent);
}
});
return convertView;
}
// Second list for horizontal view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Title"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#000000"
android:textStyle="bold"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="10dp">
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/hor_list"
style="#style/TwoWayView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tools:context=".MainActivity" />
</LinearLayout>
</LinearLayout>
// second adapter for second list(Horizontal list)
public View getView(int position, View convertView, ViewGroup parent)
{
View retval = null;
if(retval == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
retval = mInflater.inflate(this.resource, null);
holder = new Holder();
}
holder.image = (ImageView) retval.findViewById(R.id.image);
holder.pending = (TextView) retval.findViewById(R.id.textOne);
holder.channel = (TextView) retval.findViewById(R.id.text1);
holder.chan_id = (TextView) retval.findViewById(R.id.text2);
holder.text3 = (TextView) retval.findViewById(R.id.text3);
mChannel = mChildList.get(position).getChannel();
holder.channel.setText(mChannel);
holder.pending.setText(String.valueOf(mChildList.get(position).getPending()));
holder.chan_id.setText(String.valueOf(mChildList.get(position).getChannelId()));
if(mChildList.get(position).getSiteid() == 100)
{
holder.image.setBackgroundResource(R.drawable.fb_icon1);
holder.text3.setText("facebook");
}
else if(mChildList.get(position).getSiteid() == 200)
{
holder.image.setBackgroundResource(R.drawable.tr_icon1);
holder.text3.setText("twitter");
}
else if(mChildList.get(position).getSiteid() == 300)
{
holder.image.setBackgroundResource(R.drawable.in_icon1);
holder.text3.setText("linkedin");
}
retval.setTag(holder);
return retval;
}
}
When I click the button in the ListView, I see the toast once. Then further clicks don't show anything when I click away from the button and in an empty space in the ListView, all the button click events that were not responding earlier appear at once (many toasts pop up one after another). Any idea how to fix this?
class Adapter extends ArrayAdapter
{
HashMap<Integer, View> rowViews = new HashMap<Integer, View>();
public Adapter(Context context) {
super(context, R.layout.manage_member_row, members);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View rowView;
if(rowViews.containsKey(position)) {
rowView = rowViews.get(position);
} else {
rowView = inflater.inflate(R.layout.manage_member_row, parent, false);
((TextView) rowView.findViewById(R.id.manage_member_row_name)).setText(members.get(position).name);
rowViews.put(position, rowView);
}
((Button) rowView.findViewById(R.id.manage_member_row_delete)).setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "btnclick", Toast.LENGTH_SHORT).show();
}
});
Log.i("CRC", "called");
return rowView;
}
//manage_member_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:id="#+id/manage_member_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="18sp"
android:textColor="#53585E"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp" />
<Button
android:id="#+id/manage_member_row_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:paddingLeft="8dip"
android:clickable="true"
android:focusable="true"
android:textSize="18sp"
android:textColor="#4E4E4E"
android:background="#EAEAEA"
android:paddingRight="8dip"
android:textStyle="bold"
android:text="REMOVE"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip" />
</RelativeLayout>
//fragment_manage_members.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="activities.ManageMembersActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:descendantFocusability="blocksDescendants"
android:layout_marginTop="0dip"
android:dividerHeight="2.0dip"
android:divider="#EEEDF3"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</RelativeLayout>
The problem is that sometimes position from getView are not synchronized have that problem before..
Also note that you use a HashMap<Integer, View> just to check if the view is already there that cost a lot of memory and will cause OutOfMemoryException..
so instead of putting it in a hashMap just create a ViewHolder for all your views. and add the object of the ViewHolder to the tag of the View..
example:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if(view == null)
{
viewHolder = new ViewHolder();
view = mInflater.inflate(R.layout.chat_online_user_list_view_item, null);
viewHolder.userName = (TextView) view.findViewById(R.id.chat_online_user_name);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) view.getTag();
//DO THE ON CLICK LISTENER HERE
return view;
}
private class ViewHolder
{
private TextView userName;
}