Android : Dynamically Adding view to LinearLayout in android

on Monday, September 29, 2014


Hi I'm trying to add views dynamically to a linear layout. I were fetch data from web service ,



it will return 6 category details with one image url and category name but the last detail were bounded view



.


My Activity class



public class CityNearBy_Main extends FragmentActivity implements AdapterCalback{
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
new ApiAsync(new ProgressDialog(this), this, this).execute();
Log.i("here", "onresume");
}

private AdapterLinearLayout cat_scroll;

private CategoryAdapter categoryAdapter;
private ArrayList<Category_model> category_models;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_near_by__main);

cat_scroll=(AdapterLinearLayout) findViewById(R.id.cat_scroll);
categoryAdapter=new CategoryAdapter(category_models, getApplication());
cat_scroll.setAdapter(categoryAdapter);
Log.i("here", "oncreate");
//cat_scroll.seta
}

public ArrayList<Category_model> getCategory_models() {
return category_models;
}

public void setCategory_models(ArrayList<Category_model> category_models) {
this.category_models = category_models;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void notify_data_set() {
// TODO Auto-generated method stub
Log.i("update", "notify");
categoryAdapter.upDateEntries(category_models);
}


}


My custom Linear Layout



public class AdapterLinearLayout extends LinearLayout{
private Adapter adapter;

private DataSetObserver dataSetObserver=new DataSetObserver() {

@Override
public void onChanged() {
// TODO Auto-generated method stub
super.onChanged();
reloadChildViews();
}

};
public AdapterLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs,defStyle);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
}
public AdapterLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.HORIZONTAL);
}
public void setAdapter(Adapter adapter) {
if (this.adapter == adapter) return;
this.adapter = adapter;
if (adapter != null) adapter.registerDataSetObserver(dataSetObserver);
reloadChildViews();
}

/* @Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
if (adapter != null) adapter.registerDataSetObserver(dataSetObserver);
}*/
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (adapter != null) adapter.unregisterDataSetObserver(dataSetObserver);
}

private void reloadChildViews() {
removeAllViews();
if (adapter == null) return;
int count = adapter.getCount();
for (int position = 0; position < count; ++position) {
View v = adapter.getView(position, null, this);
if (v != null) addView(v);
}

requestLayout();

}


}


My Adapter class



public class CategoryAdapter extends BaseAdapter implements OnClickListener{
private ArrayList<Category_model> category_model;
private Context context;
private int position;
private File cacheDir;
private DisplayImageOptions options ;
private ImageLoaderConfiguration config;
private ImageLoader imageLoader;




public CategoryAdapter(ArrayList<Category_model> category_model, Context context) {
super();
this.category_model = category_model;
this.context = context;


cacheDir = StorageUtils.getOwnCacheDirectory(context, "category");
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheInMemory()
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.displayer(new FadeInBitmapDisplayer(1500))
.showImageOnLoading(R.drawable.ic_launcher).build();
imageLoader=com.nostra13.universalimageloader.core.ImageLoader.getInstance();
config = new ImageLoaderConfiguration.Builder(context)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.build();
imageLoader.init(config);
imageLoader.clearMemoryCache();
imageLoader.clearDiskCache();
}

@Override
public int getCount() {
// TODO Auto-generated method stub
if(category_model!=null)
return category_model.size();

return 0;
}

@Override
public Category_model getItem(int position) {
// TODO Auto-generated method stub
if(category_model!=null)
return category_model.get(position);

return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

private class ViewHolder{
private TextView catname;
private ImageView cat_icon;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
this.position=position;
ViewHolder viewHolder;
if(category_model!=null && category_model.size()>0 ){
if(convertView==null){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

convertView=inflater.inflate(R.layout.inflate_cat , null);
viewHolder=new ViewHolder();
viewHolder.cat_icon=(ImageView) convertView.findViewById(R.id.cat_icon);
viewHolder.catname=(TextView)convertView.findViewById(R.id.cat_name);
convertView.setTag(viewHolder);
}
else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.catname.setText(category_model.get(position).getTitle());
int loader=R.drawable.ic_launcher;

imageLoader.displayImage(category_model.get(position).getIcon(), viewHolder.cat_icon, options);
viewHolder.cat_icon.setOnClickListener(this);
viewHolder.catname.setOnClickListener(this);
return convertView;
}


return null;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cat_icon:
case R.id.cat_name:
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
break;

default:
break;
}
}
public void upDateEntries(ArrayList<Category_model> category_model) {
this.category_model = category_model;
notifyDataSetChanged();
}


}


My Xml View



![<LinearLayout 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"
android:orientation="vertical"
tools:context="com.sujith.citynearby.CityNearBy_Main"
android:weightSum="1" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#e00032"
android:layout_weight=".13">


</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dp"

android:layout_weight=".62">


</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#ea0032"
android:layout_weight=".13">


</LinearLayout>][1]

<LinearLayout android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#e00032"
android:layout_weight=".12"
android:minHeight="45dp"
android:orientation="vertical">

<HorizontalScrollView

android:layout_width="fill_parent"
android:layout_height="fill_parent"

>

<!-- <LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"

android:minHeight="45dp"
android:id="@+id/cat_scroll">



</LinearLayout> -->
<com.sujith.custom_layout.AdapterLinearLayout
android:id="@+id/cat_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

</com.sujith.custom_layout.AdapterLinearLayout>
</HorizontalScrollView>



</LinearLayout>
</LinearLayout>

0 comments:

Post a Comment