Adding a Horizontal Scroll View to a ListView - android

I currently have an adapter that takes two images and a text view to add to a listView. I want to change this so that I can add a horizontalScrollView inside each list item that contains the images. Would there be any way to do this?
This my current working adapter as described above with two imageView's and a textView
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
private PopupWindow popUpWindow;
private LayoutInflater inflater;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object 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 holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.information= (Button) convertView.findViewById(R.id.button5);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//set text and url
final View finalConvertView = convertView;
holder.information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) inflater.inflate(R.layout.information_popup, null);
popUpWindow = new PopupWindow(container, 800,400,true);
popUpWindow.showAtLocation(finalConvertView.findViewById(R.id.orders), Gravity.CENTER, 0,0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popUpWindow.dismiss();
return true;
}
});
}
});
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(450,450).into(holder.imageView);
Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(450,450).into(holder.imageView2);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
ImageView imageView2;
Button information;
}
}
Getter and Setter Class
public class Bean {
String text;
String url;
String url2;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl2() {
return url2;
}
public void setUrl2(String url2) {
this.url2 = url2;
}
}
Activity Layout XML
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".OrdersActivity"
android:background="#ffffff">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/orderListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:dividerHeight="2dp"/>
</android.support.design.widget.CoordinatorLayout>
Content Layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".OrdersActivity"
android:id="#+id/orders">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView2"
android:src="#drawable/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/one"
android:id="#+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/imageView2"
android:layout_alignTop="#+id/imageView2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/informaci_n"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/entregado"
android:id="#+id/button3"
android:layout_below="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_entregado"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

I ended up figuring it out. In case anyone else wants to do this, this is the code I used. I just added a horizontalScrollView and I added a linearLayout with a horizontal direction inside the horizontalScrollView and then I added the images inside that linearLayout. This was my code.
MainActivity
public class MainActivity extends AppCompatActivity {
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String a1 = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Soccer_ball.svg/2000px-Soccer_ball.svg.png";
String a2 = "https://s-media-cache-ak0.pinimg.com/736x/91/27/24/912724cb45a6a2ba468024a5285b01e7.jpg";
String[] descriptionArray = {"Description 1", "Description 2"};
String[] photoArray = {a1,a2,a1,a2,a1,a2,a1};
String[] photoArray2 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray3 = {a1,a2,a1,a2,a1,a2,a1};
String [] photoArray4 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray5 = {a1,a2,a1,a2,a1,a2,a1};
String[] photoArray6 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray7 = {a1,a2,a1,a2,a1,a2,a1};
list = (ListView) findViewById(R.id.listView);
List<Bean> myList = new ArrayList<>();
for(int i = 0; i < descriptionArray.length; i++) {
Bean bean = new Bean();
bean.setText(descriptionArray[i]);
bean.setUrl(photoArray[i]);
bean.setUrl2(photoArray2[i]);
bean.setUrl3(photoArray3[i]);
bean.setUrl4(photoArray4[i]);
bean.setUrl5(photoArray5[i]);
bean.setUrl6(photoArray6[i]);
bean.setUrl7(photoArray7[i]);
myList.add(bean);
}
MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
list.setAdapter(adapter);
}
}
My Adapter
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object 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 holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_main, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView);
holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
holder.imageView3 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.imageView4 = (ImageView) convertView.findViewById(R.id.imageView4);
holder.imageView5 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.imageView6 = (ImageView) convertView.findViewById(R.id.imageView6);
holder.imageView7 = (ImageView) convertView.findViewById(R.id.imageView7);
holder.horizontalScrollView= (HorizontalScrollView) convertView.findViewById(R.id.horizontalScrollView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(200,200).into(holder.imageView);
Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(200,200).into(holder.imageView2);
Picasso.with(mContext).load(mList.get(position).getUrl3()).resize(200,200).into(holder.imageView3);
Picasso.with(mContext).load(mList.get(position).getUrl4()).resize(200,200).into(holder.imageView4);
Picasso.with(mContext).load(mList.get(position).getUrl5()).resize(200,200).into(holder.imageView5);
Picasso.with(mContext).load(mList.get(position).getUrl6()).resize(200,200).into(holder.imageView6);
Picasso.with(mContext).load(mList.get(position).getUrl7()).resize(200,200).into(holder.imageView7);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
ImageView imageView5;
ImageView imageView6;
ImageView imageView7;
HorizontalScrollView horizontalScrollView;
}
}
Bean Class(Getter and Setter Class)
public class Bean {
String text;
String url;
String url2;
String url3;
String url4;
String url5;
String url6;
String url7;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl2() {
return url2;
}
public void setUrl2(String url2) {
this.url2 = url2;
}
public String getUrl3() {
return url3;
}
public void setUrl3(String url3) {
this.url3 = url3;
}
public String getUrl4() {
return url4;
}
public void setUrl4(String url4) {
this.url4 = url4;
}
public String getUrl5() {
return url5;
}
public void setUrl5(String url5) {
this.url5 = url5;
}
public String getUrl6() {
return url6;
}
public void setUrl6(String url6) {
this.url6 = url6;
}
public String getUrl7() {
return url7;
}
public void setUrl7(String url7) {
this.url7 = url7;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
android:background="#ffffff">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/list_container" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/xml_full_img_linear_below_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
list_container.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="53dp"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:dividerHeight="2dp"/>
</RelativeLayout>

Not exactly what you asked, but i think this question can help you.
HorizontalScrollView inside ListView: minor vertical scroll stops horizontal scroll

Related

Expanding Listview using Recyclerview inside Scrollview

I tried below code to implement an expend and collapse content using recyclerview(listview) a link
final boolean isExpanded = position==mExpandedPosition;
holder.details.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
In my case when i click the particular position in recyclerview its expanding but it goes above the recyclerview.I cant see the full expanded content.i can see only partial content.In this case i need to scroll the recyclerview to view the full content.But i am searching for a solution to view the content without scroll the recyclerview. If i click another position in recyclerview that should be placed over an recyclerview.
Hear is My Code
public class CommonFragment extends Fragment {
#BindView(R.id.news_lists)
RecyclerView news_lists;
#BindView(R.id.nested_scroll)
NestedScrollView nested_scroll;
ArrayList<String> Names;
ArrayList<String> responseProducts = null;
NewsListAdaspters mAdapter;
Context mContext;
String position_name;
public CommonFragment() {
}
public static CommonFragment getInstance(String position, ArrayList<String> response) {
CommonFragment fragmentDummy = new CommonFragment();
Bundle args = new Bundle();
args.putStringArrayList("Types", response);
args.putString("position", position);
fragmentDummy.setArguments(args);
return fragmentDummy;
}
#Override
public void setArguments(Bundle args) {
super.setArguments(args);
this.responseProducts = args.getStringArrayList("Types");
this.position_name = args.getString("position");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
View view;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_common, container, false);
ButterKnife.bind(this, view);
} catch (InflateException ignored) {
}
mContext = getActivity();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
news_lists.setLayoutManager(mLayoutManager);
news_lists.setNestedScrollingEnabled(false);
Names = new ArrayList<>();
Names.clear();
for (int i = 0; i < 15; i++) {
Names.add("News Details " + i);
}
Log.e("position_name==>", "" + position_name);
getList();
return view;
}
private void getList() {
if (responseProducts.size() > 0) {
mAdapter = new NewsListAdaspters(mContext, responseProducts, position_name);
news_lists.setAdapter(mAdapter);
}
}
public class NewsListAdaspters extends RecyclerView.Adapter<NewsListAdaspters.MyViewHolder> {
private ArrayList<String> data;
private Context context;
private String name;
int mExpandedPosition = -1;
int previousExpandedPosition = -1;
NewsListAdaspters(Context context, ArrayList<String> maps, String selectedFragmentName) {
this.context = context;
this.data = maps;
this.name = selectedFragmentName;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.common_view, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, #SuppressLint("RecyclerView") final int position) {
holder.news_description.setText(data.get(position));
holder.news_title.setText(name);
final boolean isExpanded = position == mExpandedPosition;
Log.e("isExpanded=====>", "" + isExpanded);
holder.expend_layout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.itemView.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = position;
holder.news_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Activity_NewsFullDetails.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
holder.expand_click_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
holder.share_fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_whatsapp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
holder.share_twet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.news_title)
public TextView news_title;
#BindView(R.id.news_hour)
public TextView news_hour;
#BindView(R.id.news_description)
public TextView news_description;
#BindView(R.id.news_image)
public ImageView news_image;
#BindView(R.id.expand_click_layout)
RelativeLayout expand_click_layout;
#BindView(R.id.expend_layout)
public LinearLayout expend_layout;
#BindView(R.id.full_title)
public TextView full_title;
#BindView(R.id.txt_description)
public TextView txt_description;
#BindView(R.id.share_twet)
public ImageView share_twet;
#BindView(R.id.share_whatsapp)
public ImageView share_whatsapp;
#BindView(R.id.share_fb)
public ImageView share_fb;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:background="#03000000">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="#+id/news_lists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
ITEM XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:animateLayoutChanges="true"
android:background="#drawable/comment_background"
android:stateListAnimator="#animator/comment_selection"
android:elevation="3dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/news_image"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_margin="2dp"
android:background="#drawable/icon_card"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/expand_click_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/news_image"
android:layout_alignTop="#+id/news_image"
android:layout_toEndOf="#+id/news_image"
android:layout_toRightOf="#+id/news_image">
<TextView
android:id="#+id/news_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_layout"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:maxEms="3"
android:maxLines="4"
android:padding="4dp"
android:text=""
android:textColor="#color/black_color"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_margin="4dp">
<TextView
android:id="#+id/news_hour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/center_text"
android:layout_toStartOf="#+id/center_text"
android:maxLines="2"
android:text="Hours"
android:textColor="#color/black_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/center_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/center_text"
android:layout_toRightOf="#+id/center_text"
android:gravity="end"
android:text="News Title"
android:textColor="#color/black_color"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/expend_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/news_image"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/full_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Title Text"
android:textColor="#color/black_color"
android:textSize="20sp" />
<TextView
android:id="#+id/txt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/large_text1"
android:textColor="#color/black_color"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<ImageView
android:id="#+id/share_fb"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_facebook" />
<ImageView
android:id="#+id/share_whatsapp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_whatsapp" />
<ImageView
android:id="#+id/share_twet"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/share_tweet" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to use NestedScrollView instead of ScrollView and set below to your activity :-
recyclerView.setNestedScrollingEnabled(false);
For more information you can refer below stackoverflow links:-
How to use RecyclerView inside NestedScrollView?
Recyclerview inside ScrollView not scrolling smoothly

I am attaching a image to a recycler view which is being downloaded from server, but recycler view is not showing any image

The recycler view is showing other details in activity, except the image. I am loading an image through AsyncTask in getImage(). What am I doing wrong?
Here's my code:
MainActivity.java
int i;
Bitmap bitmap,imageOfPoday;
RecyclerView rvPoday;
ProductAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
pd = ProgressDialog.show(MainActivity.this, "Please Wait...", null, true, true);
callPoday(PODAY_URL);
}
public List<Product> getDataPoday() {
List<Product> data=new ArrayList<>();
for(i = 0; i < parsedPODAY_ID.size(); i++) {
Product current=new Product();
current.productName=parsedPODAY_NAME.get(i);
current.price=parsedPODAY_OFFPRICE.get(i);
current.pImage=getImage("http://www.tontosworld.com/img/productimages/" + parsedPODAY_P_IMG.get(i));
Toast.makeText(this,current.pImage+"",Toast.LENGTH_LONG).show();
data.add(current);
}
return data;
}
public Bitmap getImage(String s) {
LoadImageTask task=new LoadImageTask();
task.execute(s);
return imageOfPoday;
}
class LoadImageTask extends AsyncTask<String,String,Bitmap> {
#Override
protected Bitmap doInBackground(String... strings) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(strings[0]).getContent());
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap b) {
super.onPostExecute(b);
if(b == null) {
Toast.makeText(MainActivity.this,"Error in loading image",Toast.LENGTH_LONG).show();
}
else {
imageOfPoday=b;
Toast.makeText(MainActivity.this,"This is for image"+imageOfPoday,Toast.LENGTH_LONG).show();
}
}
}
ProductAdapter.java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
public LayoutInflater inflater;
List<Product> data= Collections.emptyList();
public ProductAdapter(Context context,List<Product> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.recycler_view_single,parent,false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Product current = data.get(position);
holder.name.setText(current.productName);
holder.price.setText(current.price);
holder.image.setImageBitmap(current.pImage);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name,price;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.rvItemName);
price = (TextView)itemView.findViewById(R.id.rvItemPrice);
image = (ImageView)itemView.findViewById(R.id.rvItemImage);
}
}
}
Product.java
public class Product {
String productName, price;
Bitmap pImage;
}
recycler_view_single.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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?android:selectableItemBackground">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/rvItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/rvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="#+id/rvItemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
activity_main.xml
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product of the day"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textColor="#000"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rvProductOfTheDay">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
android:name="com.tontosworld.tontosworld.NavigationDrawerFragment">
</fragment>
</android.support.v4.widget.DrawerLayout>
Thanks for your help.
You are missing the update notice for your adapter (which displays the data).
Everytime you update your List, you need to call
adapter.notifyDataSetChanged();
//if you only change one item, and use recyclerview, call:
adapter.notifyItemChanged();

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

I can't see ListFragment android?

I create a ListFragment for show Text & Image my code hase not any error but when i run it , it get me crash & when I see Log it don't get me any Message ?
My StartActivity.class :
public class StartActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new Setting_Fragment()).commit();
}
}
My activity_start.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:background="#drawable/back"
tools:context="in.project.StartActivity" >
<ImageView
android:id="#+id/imgHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#e42769"
android:src="#drawable/head" />
<LinearLayout
android:id="#+id/LFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="6" >
<ImageView
android:id="#+id/img_Setting_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting_note" />
<ImageView
android:id="#+id/img_Calendar"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_calendar" />
<ImageView
android:id="#+id/img_Mail"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_mail" />
<ImageView
android:id="#+id/img_Review_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_review_note" />
<ImageView
android:id="#+id/img_Help"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_help" />
<ImageView
android:id="#+id/img_Setting"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting" />
</LinearLayout>
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/LFooter"
android:layout_below="#+id/imgHeader" />
</RelativeLayout>
My SettingArrayAdapter.class :
public class SettingArrayAdapter extends ArrayAdapter<Instructure_setting>{
private Context context;
private List<Instructure_setting> objects;
public SettingArrayAdapter(Context context, int resource, List<Instructure_setting> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#SuppressLint({ "ViewHolder", "InflateParams" })
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Instructure_setting _Data = objects.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listview_setting_fragment, null);
ImageView image = (ImageView) view.findViewById(R.id.img_message);
image.setImageResource(_Data.getImageResource());
TextView tv = (TextView) view.findViewById(R.id.txt_message);
tv.setText(_Data.gettxtMessage());
return view;
}
}
My Setting_Fragment.class :
public class Setting_Fragment extends ListFragment{
List<Instructure_setting> DATA = new Setting_Fragment_Data().getMessages();
public Setting_Fragment()
{
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingArrayAdapter adapter = new SettingArrayAdapter(getActivity(),
R.layout.listview_setting_fragment,
DATA);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.setting_fragment, container, false);
return rootView;
}
}
My setting_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<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=".StartActivity" >
<ListView
android:id="#+id/lstview_Setting_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
My Setting_Fragment_Data.class :
public class Setting_Fragment_Data {
private List<Instructure_setting> _Messages = new ArrayList<Instructure_setting>();
public List<Instructure_setting> getMessages() {
return _Messages;
}
public Setting_Fragment_Data() {
_Messages.add(new Instructure_setting("AAA", R.drawable.period_color));
_Messages.add(new Instructure_setting("BBB", R.drawable.password_color));
_Messages.add(new Instructure_setting("CCC", R.drawable.cleare_data_color));
_Messages.add(new Instructure_setting("DDD", R.drawable.chanel_connect_color));
_Messages.add(new Instructure_setting("EEE", R.drawable.backup_color));
_Messages.add(new Instructure_setting("FFF", R.drawable.review_note_color));
_Messages.add(new Instructure_setting("GGG", R.drawable.notification_color));
_Messages.add(new Instructure_setting("HHH", R.drawable.sync_server_color));
}
}
My Instructure_setting.class :
public class Instructure_setting {
// constants for field references
public static final String TXT_Message = "TextMessage";
public static final String IMAGE_RESOURCE = "imageResource";
// private fields
private String txtMessage;
private int imageResource;
// getters and setters
public String gettxtMessage() {
return txtMessage;
}
public void settxtMessage(String txtMessage) {
this.txtMessage = txtMessage;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
// Used when creating the data object
public Instructure_setting(String msg, int imageResource) {
this.txtMessage = msg;
this.imageResource = imageResource;
}
// Create from a bundle
public Instructure_setting(Bundle b) {
if (b != null) {
this.txtMessage = b.getString(TXT_Message);
this.imageResource = b.getInt(IMAGE_RESOURCE);
}
}
// Package data for transfer between activities
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString(TXT_Message, this.txtMessage);
b.putInt(IMAGE_RESOURCE, this.imageResource);
return b;
}
// Output txtMessage data
#Override
public String toString() {
return txtMessage;
}
}
This is my listview_setting_fragment.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="wrap_content"
android:paddingBottom="10dp">
<ImageView
android:id="#+id/img_message"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:contentDescription="placeholder" />
<TextView
android:id="#+id/txt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/img_message"
android:text="placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
NOTICE : I am using from appcompat_v7 .
When you use ListFragment or ListActivity then ListView id in xml must be :
android:id="#id/android:list
Change your ListView id in xml :
<ListView
android:id="#id/android:list"
Instead of
<ListView
android:id="#+id/lstview_Setting_fragment"
Note : Try to set List Adapter in onCreateView() instead of onCreate() in Fragment.

ListView in nested fragment showing empty list item, (only textviews not images)

After googling alot i never found any helpful answer, so i'm posting my issue here. My problem is as follows :
I've a nested fragment (MainActivity calls SettingsFragment and that fragment calls another fragment named as ContactFragment) that uses a listview in it. I'm setting data in adapter but and call adapter.notifyDataSetChanged(); it adds an item with in ListView but show only images not TextViews. I debugged my code line by line each variable has its values but list items are not displaying it. Below is my Code.
ContactsFragment extends Fragment {
public static List<ContactItem> contactList;
private ContactsItemAdapter adapter;
private ListView lvContacts;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lvContacts = (ListView) getView().findViewById(R.id.lvContacts);
contactList = new ArrayList<ContactItem>();
contactList.add(new ContactItem(R.drawable.ic_action_overflow, "OverFlow", "Testing", SOS.CONTACT_TYPE_SMS));
adapter = new ContactsItemAdapter(getActivity(), R.layout.contact_item, contactList);
lvContacts.setAdapter(adapter);
}
Following is a Click Event for adding an item to the list
private void btnAddOnClick(int type){
contactList.add(new ContactItem(R.drawable.ic_action_call, "ABC", "123", "SMS"));
adapter.notifyDataSetChanged();
}
}
Following is fragment_contacts.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:gravity="center_horizontal"
android:orientation="vertical" >
<ListView
android:id="#+id/lvContacts"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ListView>
</LinearLayout>
By Calling btnAddOnClick adds item in listview but empty like this
here you can see it shows call icon but not showing text views values. Please tell me what i'm doing wrong.
Here is adapter
public class ContactsItemAdapter extends ArrayAdapter<ContactItem> {
private LayoutInflater mInflater;
public ContactsItemAdapter(Context context, int resource,
List<ContactItem> contactList) {
super(context, resource, contactList);
mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView tvName;
TextView tvValue;
ImageView ivIcon;
ContactItem rowData = ContactsFragment.contactList.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.contact_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
tvName = holder.getName();
tvName.setText(rowData.getName());
tvValue = holder.getValue();
tvValue.setText(rowData.getValue());
ivIcon = holder.getIcon();
ivIcon.setImageResource(rowData.getIcon());
return convertView;
}
public class ViewHolder {
private View mRow;
private TextView tvName = null;
private TextView tvValue = null;
private ImageView ivIcon = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView getName() {
if (null == tvName) {
tvName = (TextView) mRow.findViewById(R.id.tvName);
}
return tvName;
}
public TextView getValue() {
if (null == tvValue) {
tvValue = (TextView) mRow.findViewById(R.id.tvValue);
}
return tvValue;
}
public ImageView getIcon() {
if (null == ivIcon) {
ivIcon = (ImageView) mRow.findViewById(R.id.icon);
}
return ivIcon;
}
}
}
ContactItem Class
public class ContactItem {
private int icon;
private String name;
private String value;
private String contactType;
public ContactItem() {
icon = R.drawable.ic_action_call;
name = "";
value = "";
contactType = "";
}
public ContactItem(int icon, String nameString, String valueString,
String type) {
this.icon = icon;
this.name = nameString;
this.value = valueString;
this.contactType = type;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
contact_item.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="55dp"
android:background="#color/holo_gray_light"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
/>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
<ImageView
android:id="#+id/ivCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_action_cancel" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/divider"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:ellipsize="end"
android:text="Nauman Zubair"/>
<TextView
android:id="#+id/tvValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:paddingRight="40dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="+92 123 1234567"/>
</LinearLayout>
</RelativeLayout>
Your code should work, I think the problem is your contact_item.xml layout. Somehow your design is hiding the text. Use a simpler layout design to see if it shows the text, try getting rid of the LinearLayout:
<?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="55dp"
android:background="#color/holo_gray_light"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
<ImageView
android:id="#+id/ivCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_action_cancel" />
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:ellipsize="end"
android:text="Nauman Zubair"/>
<TextView
android:id="#+id/tvValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:paddingRight="40dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="+92 123 1234567"/>
</RelativeLayout>

Categories

Resources