How do I render a custom object to Android in linearLayout? - android

I am new to Android development. I have created a custom object in java. How do I render it to screen? I look around and is really confused about the whole Android rendering process.
My object is not a list. It is a tree. I also read about Adapter, Layout and View, but don't understand it. Here is my code:
public class GeoArea {
public String id;
public String name;
public String searchLocation;
public static String searchTerm;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
searchLocation = "";
subGeoAreas = new LinkedList<GeoArea>();
}
#Override
public String toString() {
return name;
}
public int size(){
int result = 0;
for(GeoArea curGeoArea:subGeoAreas){
result += curGeoArea.size();
}
return result + 1;
}
}
And here is the Layout I have:
<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=".Activity_GeoAreas" >
<TextView
android:id="#+id/textMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Choose Suburb"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id=""#+id/geoAreasLayout""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textMain"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
So the idea is to render the main GeoArea into geoAreasLayout, and since it is a tree, each subGeoArea will get rendered in turn.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_areas);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
torontoArea.name = "City of Toronto";
torontoArea.searchLocation = "Toronto, ON";
GeoArea testGeoArea = null;
testGeoArea = new GeoArea("DownTownCore");
testGeoArea.name = "Down Town Core";
testGeoArea.searchLocation = "Downtown Core, Toronto, ON";
testGeoArea.parentGeoArea = torontoArea;
torontoArea.subGeoAreas.add(testGeoArea);
testGeoArea = new GeoArea("AlexandraPark");
testGeoArea.name = "Alexandra Park";
testGeoArea.searchLocation = "Alexandra Park, Toronto, ON";
testGeoArea.parentGeoArea = torontoArea;
torontoArea.subGeoAreas.add(testGeoArea);
// what's next?
}
}
I try to write an Adapter, but I am not even sure what it's for or how to use it:
public class GeoAreaAdapter extends BaseAdapter implements Filterable{
private Context _context;
private int resource;
private GeoArea _myGeoArea;
public GeoAreaAdapter(Context context, int resource, GeoArea myGeoArea) {
_context = context;
_myGeoArea = myGeoArea;
}
public int getCount() {
return _myGeoArea.size();
}
public Object getItem(int position) {
return _myGeoArea;
}
public long getItemId(int position) {
return _myGeoArea.id.hashCode();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout GeoAreaView;
if (convertView == null) {
GeoAreaView = new LinearLayout(_context);
LayoutInflater li = LayoutInflater.from(_context);
convertView = li.inflate(R.layout.layout_geo_area, null);
}
else {
GeoAreaView = (LinearLayout) convertView;
}
TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
name.setText(_myGeoArea.name);
return convertView;
}
static class ViewHolder {
TextView name;
RelativeLayout childContainer;
}
#Override
public Filter getFilter() {
return null;
}
}

Firstly your class must be extends from one of Views or ViewGroups classes to render and use it in LinearLayout.like this:
public class GeoArea extends View{
According to this issue after that you can use it in xml Layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.YourClassName
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mapview"
></com.example.YourClassName>
</LinearLayout>
as you see you must use package name to render your new object in xml layout file.

Related

How to pass adapter image from model category?

How to transfer image from category model to adapter **public String category_image;**Only these should be taken to the adapter. I have tried many times and I am getting many errors.I have given the image below.The error in them should be corrected i am new android devloper
category model this
import java.io.Serializable;
public class Category implements Serializable {
public int cid = -1;
public String category_name;
public String category_image;
public String recipes_count;
public Category(String name, String profession, int photo) {
}
public String getItemName() {
return this.category_name;
}
public String category_image() {
return this.category_image;
}
}
public class GalleryAdapter extends BaseAdapter {
private Context context;
private List<Category> mensWears;
public GalleryAdapter(Context context, List<Category> mensWears) {
this.context = context;
this.mensWears = mensWears;
}
#Override
public int getCount() {
return mensWears.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i,View view,ViewGroup viewGroup) {
final Category mensWear = mensWears.get(i);
if (view == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.custom_gallery_layout, null);
}
//For text
TextView prdId = view.findViewById(R.id.name);
// prdId.setText(prdId.toString());
Picasso.get()
.load(getApiUrl + "/upload/category/" + mensWears.category_image)
.placeholder(R.drawable.ic_thumbnail)
.into(i.category_image);
prdId.setText(mensWears.get(i).getItemName());
// //For images
// final ImageView imageView = view.findViewById(R.id.name);
// if(!TextUtils.isEmpty(mensWear.getItemName())){
//
//// Picasso.with(context).load(imageUrlFromServer+mensWear.category_image())
//// .into(imageView);
return view;
}
this adapter layout
<?xml version="1.0" encoding="utf-8"?>
<com.kannada.newspaper.india.utils.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ll_main"
android:padding="#dimen/nav_header_vertical_spacing"
android:orientation="vertical">
<ImageView
android:background="#drawable/bg_google"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:alpha="1"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:id="#+id/photo"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorWhite"
android:textSize="#dimen/primeryText"
android:fontFamily="#font/sfprodisplayregular"
android:layout_marginTop="#dimen/margin10"
android:id="#+id/name"
android:text="Facebook"/>
</LinearLayout>
</com.kannada.newspaper.india.utils.SquareFrameLayout>
First, you need to get the view from the XML
ImageView imageView = view.findViewById(R.id.name);
Then you need to set the data and imageview in Picasso like
Picasso.get()
.load(getApiUrl + "/upload/category/" + mensWears.get(i).category_image())
.placeholder(R.drawable.ic_thumbnail)
.into(imageView);

i have array of objects. i want to display its elements in a listview

i have array of objects carts containing itemname and price. Array is coming correctly from sqlite database. i want to display it in a listview. But in a listview it is only showing the package name not itemname and price. I think problem is in this line "ListAdapter buckysAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, carts); "of Main2Activity.java
Below is my code please guide me.It is very important for me
Main2Activity.java
public class Main2Activity extends Activity {
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
final int position = intent.getIntExtra("history",1);
Log.i("positin","position = "+String.valueOf(position));
dbHandler = new MyDBHandler(this,null,null,6);
Cart[] carts = dbHandler.databaseToArray(position);
for (Cart c : carts)
Log.i("jhnbvb1","item1 = "+c.getItemname()+" price1 = "+c.getPrice());
ListAdapter buckysAdapter = new ArrayAdapter<Cart>(this, android.R.layout.simple_list_item_1, carts);
ListView buckysListView = (ListView) findViewById(R.id.buckysListView2);
buckysListView.setAdapter(buckysAdapter);
}}
CustomAdapter2.java
public class CustomAdapter2 extends ArrayAdapter<Cart> {
public CustomAdapter2(Context context, Cart[] foods) {
super(context, R.layout.custom_row2 ,foods);
}
#Override
public Cart getItem(int position) {
return super.getItem(position);
}
#Override
public int getPosition(Cart item) {
return super.getPosition(item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater buckysInflater = LayoutInflater.from(getContext());
View customView = buckysInflater.inflate(R.layout.custom_row2, parent, false);
Cart singleFoodItem = getItem(position);
String name = singleFoodItem.getItemname();
String price = singleFoodItem.getPrice();
Log.i("rtyiykj","item2 = "+name+" price2 = "+price);
TextView buckysText = (TextView) customView.findViewById(R.id.buckysText2);
TextView buckysText1 = (TextView) customView.findViewById(R.id.buckysText3);
buckysText.setText(name);
buckysText1.setText(price);
return customView;
}}
custom_row2.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="match_parent">
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/buckysText2"
android:layout_margin="5dp" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/buckysText3"
android:layout_margin="5dp" />
activity_main2.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Main2Activity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buckysListView2"></ListView>
cart.java
public class Cart {
private String itemname,price;
public Cart(String itemname,String price) {
this.setItemname(itemname);
this.setPrice(price);
}
public Cart() {}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public void setPrice(String price) {
this.price = price;
}
public String getItemname() {
return itemname;
}
public String getPrice() {
return price;
}}
You use standard ArrayAdapter instead of that one created by you. It should be
CustomAdapter2<Cart> buckysAdapter = new CustomAdapter2<Cart>(this, carts);
And array adapter itself should be modified:
public class CustomAdapter2 extends ArrayAdapter<Cart> {
private final List<Object> foods;
public CustomAdapter2(Context context, List<Cart> foods) {
super(context, 0);
this.foods = foods;
}
#Override
public int getCount() {
return foods.size();
}
#Override
public Cart getItem(int position) {
return foods.get(position);
}
//your getView
}

Custom List View in Navigation Drawer

I am trying to populate Navigation Drawer with some custom layout. XML file for that layout is below:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:orientation="vertical"
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="40px"
android:layout_marginTop="40px">
<ImageView
android:id="#+id/imgUser"
android:src="#drawable/empty_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20px"
android:layout_marginTop="20px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Waqas Ahmed Ansari"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="63km"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Joined: Feb '16"/>
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/lstDrawerItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:layout_marginTop="20sp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Then I made a CustomAdapter class for ListView lstDrawerItems
Here it is.
public class CustomAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
ImageView imgView;
public CustomAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
super();
this.activity=activity;
this.list=list;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView = inflater.inflate(R.layout.custom_drawer_list, null);
txtFirst = (TextView) convertView.findViewById(R.id.drawer_itemName);
imgView = (ImageView) convertView.findViewById((R.id.drawer_icon));
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get("name"));
imgView.setImageResource(Integer.parseInt(map.get("imgDrawerIcon")));
return convertView;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
When I set list adapter, I am not able to see anything.
I set custom adapter here.
CustomAdapter adapter = new CustomAdapter(HomeNoVehicle.this, list);
ListView drawerList = (ListView) findViewById(R.id.lstDrawerItems);
drawerList.setAdapter(adapter);
But when I set the entries attribute if list, it works well.
Can't figure out what the problem is.
In order to make a custom list, the way that I like to start is design the cell, or cells you wish to display in the list. Something maybe like this:
Then I like to make a Class to go with that cell:
public class ProfileCell extends RelativeLayout {
ImageView imageView;
TextView name;
TextView dateJoined;
TextView distance;
String nameText;
String dateJoinedText;
String distanceText;
public ProfileCell(Context context) {
super(context);
inflate(context, R.layout.nav_list_row, this);
name = (TextView)findViewById(R.id.name);
dateJoined = (TextView)findViewById(R.id.date_joined);
distance = (TextView)findViewById(R.id.distance);
imageView = (ImageView)findViewById(R.id.imgUser);
//set Image to a default value
imageView.setImageResource(R.mipmap.ic_launcher);
}
public String getNameText() {
return nameText;
}
public void setNameText(String nameText) {
this.nameText = nameText;
name.setText(nameText);
}
public ImageView imageView() {
return imageView;
}
public String getDateJoinedText() {
return dateJoinedText;
}
public void setDateJoinedText(String dateJoinedText) {
this.dateJoinedText = dateJoinedText;
dateJoined.setText(dateJoinedText);
}
public String getDistanceText() {
return distanceText;
}
public void setDistanceText(String distanceText) {
this.distanceText = distanceText;
distance.setText(distanceText);
}
}
Then I also find it convenient to make a class that can hold the information that I want to put in that cell:
public class Profile {//Information about Users Profile
public String name;
public String dateJoined;
public String distance;
/////////possibly have image url here ....
public Profile(String name, String dateJoined, String distance)
{
this.name = name;
this.dateJoined = dateJoined;
this.distance = distance;
}
}
Then I make my custom adapter class:
public class CustomAdapter extends BaseAdapter {
ArrayList<Profile> profiles;
Context context;
public CustomAdapter(Context context, ArrayList<Profile> profiles)
{
this.context = context;
this.profiles = profiles;
}
#Override
public int getCount() {
return profiles.size();
}
#Override
public Profile getItem(int position) {
return profiles.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
//Custom Cell is inflated inside of the Profile view class
convertView = new ProfileCell(context);
}
ProfileCell cell = (ProfileCell) convertView;
Profile profile = getItem(position);
cell.setNameText(profile.name);
cell.setDateJoinedText(profile.dateJoined);
cell.setDistanceText(profile.distance);
//Then you would set the image view to something...
// cell.imageView().setImageResource(R.mipmap.ic_launcher);
//something like that maybe
return cell;
}
}
Then if I have set up my list view where I want it, in your case inside a navigation drawer then inside of your navigation activity you can do this:
//A list of data to display in list
ArrayList<Profile> profiles = new ArrayList<>();
for (int i = 0; i < 15; i++)
{
profiles.add(profile);//Adding same profile over an over
}
CustomAdapter adapter = new CustomAdapter(this,profiles);
listView.setAdapter(adapter);
And then this would display whatever you put in those cells.
I put the full code for this up here:
https://github.com/amffz9/NavigationProject

Android flipview in a fragment not working

Am trying to make a ui with flipview as in this tutorial. IThe tutorial deals with activities, but i want it in fragments case. That ie, the flip effect as well as its parent is a fragment. When i use activity it works, but on using fragment,only the empty textview shows up. Can anyone help me with this?
This is some part of the code
page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3333" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/banner"
android:id="#+id/head"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:id="#+id/header"
android:gravity="center"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/des"
android:textSize="30dp"
android:gravity="center"/>
</FrameLayout>
fragment_news_feed :
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</merge>
NewsFeedFragment.java
public class NewsFeedFragment extends Fragment implements NewsFlipAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener{
private FlipView mFlipView;
private NewsFlipAdapter mAdapter;
int i=0,no_of_items=7;
public NewsFeedFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout wrapper = new LinearLayout(getActivity());
View view = inflater.inflate(R.layout.fragment_news_feed, wrapper, true);
setUpView(view);
return wrapper;
}
private void setUpView(View view) {
mFlipView = (FlipView) view.findViewById(R.id.flip_view);
mAdapter = new NewsFlipAdapter(getActivity());
mAdapter.setCallback(this);
mFlipView.setAdapter(mAdapter);
mFlipView.setOnFlipListener(this);
mFlipView.peakNext(false);
mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
mFlipView.setOnOverFlipListener(this);
}
#Override
public void onPageRequested(int page) {
mFlipView.smoothFlipTo(page);
}
#Override
public void onFlippedToPage(FlipView v, int position, long id) {
Log.i("pageflip", "Page: " + position);
if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount()<30){
mAdapter.addItems(5);
}
}
#Override
public void onOverFlip(FlipView v, OverFlipMode mode,
boolean overFlippingPrevious, float overFlipDistance,
float flipDistancePerPage) {
Log.i("overflip", "overFlipDistance = "+overFlipDistance);
}
NewsFlipAdapter.java
public class NewsFlipAdapter extends BaseAdapter {
public interface Callback {
public void onPageRequested(int page);
}
static class Item {
static long id = 0;
long mId;
public Item() {
mId = id++;
}
long getId() {
return mId;
}
}
private LayoutInflater inflater;
private Callback callback;
private List<Item> items = new ArrayList<Item>();
public NewsFlipAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return items.get(position).getId();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.page, parent, false);
holder.heading = (TextView) convertView.findViewById(R.id.header);
holder.description = (TextView) convertView.findViewById(R.id.des);
holder.header = (ImageView) convertView.findViewById(R.id.head);
// holder.firstPage = (Button) convertView.findViewById(R.id.first_page);
// holder.lastPage = (Button) convertView.findViewById(R.id.last_page);
// holder.firstPage.setOnClickListener(this);
// holder.lastPage.setOnClickListener(this);
// convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView heading;
ImageView header;
TextView description;
}
/* #Override
public void onClick(View v) {
switch(v.getId()){
case R.id.first_page:
if(callback != null){
callback.onPageRequested(0);
}
break;
case R.id.last_page:
if(callback != null){
callback.onPageRequested(getCount()-1);
}
break;
}
}*/
public void addItems(int amount) {
TextView text;
for (int i = 0; i < amount; i++) {
items.add(new Item());
}
notifyDataSetChanged();
}
public void addItemsBefore(int amount) {
for (int i = 0; i < amount; i++) {
items.add(0, new Item());
}
notifyDataSetChanged();
}
I am not a 100 % sure about the following explanation, so please correct me if I am wrong.
The <merge> tag merges the inflated layout with it's parent view.
It generally doesn't seem to sit well with fragments as explained in more detail here.
So try to replace merge with LinearLayout in your fragment_newsfeed_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</LinearLayout>

Custom ListView error when inserting many Views

Actually I have created a custom ListView. I want to add items to this custom ListView dynamically. But in my case, the first item is inserted without any problem but when I try to insert the second item it gives an error. If anyone knows how to solve this, please suggest it to me.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/historyFrmLstView2"
android:layout_alignParentLeft="true"
android:layout_below="#+id/historyFrmLstView1"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip">
</ListView>
</RelativeLayout>
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip"
android:background="#00000000">
<TextView
android:textSize="18dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#drawable/history_scroll_title_label_bg"
android:layout_width="wrap_content"
android:id="#+id/txtHistoryDisplay"
android:layout_below="#+id/txtViewHeading"
android:layout_marginTop="0dip"
android:layout_marginLeft="2dip">
</TextView>
</RelativeLayout>
main.java
public void displayHistory()
{
String strDisplay[] = {" "};
historyFrmLstView2 = (ListView) findViewById(R.id.historyFrmLstView2);
int iHistCount = CycleManager.getSingletonObject().getHistoryCount();
int i;
SimpleDateFormat sdFormatter = new SimpleDateFormat("dd-MMMM-yyyy");
for (i=0; i<iHistCount; i++)
{
strDisplay[i] = "";
Date dtHist = CycleManager.getSingletonObject().getHistoryDate(iHistCount);
strDisplay[i]=sdFormatter.format(dtHist.getTime());
aHistFrmAdpter2 = new HistoryFrmCustomAdapter2(this,strDisplay);
historyFrmLstView2.setAdapter(aHistFrmAdpter2);
}
}
custom Adapter.java
public class HistoryFrmCustomAdapter2 extends BaseAdapter
{
public String heading1[];
public Activity context;
public LayoutInflater inflater;
public HistoryFrmCustomAdapter2(Activity context,String[] heading1)
{
super();
this.context = context;
this.heading1=heading1;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return heading1.length;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
public static class ViewHolder
{
TextView txtHistoryDisplay;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.histryfrm_listview2, null);
holder.txtHistoryDisplay =(TextView) convertView.findViewById(R.id.txtHistoryDisplay);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.txtHistoryDisplay.setText(heading1[position]);
return convertView;
}
}
Hi i think that may be used below code..
public void displayHistory()
{
String strDisplay[] = {" "};
historyFrmLstView2 = (ListView) findViewById(R.id.historyFrmLstView2);
int iHistCount = CycleManager.getSingletonObject().getHistoryCount();
int i;
SimpleDateFormat sdFormatter = new SimpleDateFormat("dd-MMMM-yyyy");
for (i=0; i<iHistCount; i++)
{
strDisplay[i] = "";
Date dtHist = CycleManager.getSingletonObject().getHistoryDate(iHistCount);
strDisplay[i]=sdFormatter.format(dtHist.getTime());
}
aHistFrmAdpter2 = new HistoryFrmCustomAdapter2(this,strDisplay);
historyFrmLstView2.setAdapter(aHistFrmAdpter2);
}
there will be only one element in your string array strDisplay[iHistCount]. since you r putting value at the last position each time. replace iHistCount with i.

Categories

Resources