Hello Friends!
Today I am going to post a logical code for add item in list-view dynamically. And increase size of row dynamically.
1)CustomListView.java
package com.manish.customlistview;
3)Item.java
4)main.xml
6)AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
Today I am going to post a logical code for add item in list-view dynamically. And increase size of row dynamically.
1)CustomListView.java
package com.manish.customlistview;
import java.util.ArrayList;
import android.os.Bundle;
import
android.widget.ListView;
import
android.app.Activity;
/**
*
* @author manish
*
*/
public class CustomListView extends Activity {
ArrayList<Item>
imageArry = new
ArrayList<Item>();
CustomImageAdapter
adapter;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// add image and
text in arraylist
String
Video[]={"xyz","abcd"};
String
Song[]={"xyz","abcd","pqerttt","yuioo"};
String
Movi[]={"xyz","abcd","qwer","yuioo","tyyuu","ggggggg","yuioo","tyyuu","ggggggg"};
imageArry.add(new Item(R.drawable.facebook, Song));
imageArry.add(new Item(R.drawable.outlook, Video));
imageArry.add(new Item(R.drawable.google, Movi));
// add data in
contact image adapter
adapter = new CustomImageAdapter(this, R.layout.list, imageArry);
ListView
dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
}
}
2)CustomImageAdapter.java
package
com.manish.customlistview;
import java.util.ArrayList;
import
android.app.Activity;
import
android.content.Context;
import
android.view.LayoutInflater;
import android.view.View;
import
android.view.ViewGroup;
import android.widget.ArrayAdapter;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.TextView;
/**
*
* @author manish
*
*/
public class CustomImageAdapter extends
ArrayAdapter<Item> {
Context
context;
int layoutResourceId;
LinearLayout
linearMain;
ArrayList<Item>
data = new
ArrayList<Item>();
public
CustomImageAdapter(Context context, int layoutResourceId,
ArrayList<Item>
data) {
super(context,
layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View
convertView, ViewGroup parent) {
View
row = convertView;
if (row == null) {
LayoutInflater
inflater = ((Activity) context).getLayoutInflater();
row
= inflater.inflate(layoutResourceId, parent, false);
linearMain = (LinearLayout)
row.findViewById(R.id.lineraMain);
Item
myImage = data.get(position);
for (int j = 0; j <
myImage.getName().length; j++) {
TextView
label = new TextView(context);
label.setText(myImage.name[j]);
linearMain.addView(label);
}
ImageView
image = new ImageView(context);
int outImage = myImage.image;
image.setImageResource(outImage);
linearMain.addView(image);
}
return row;
}
}
package
com.manish.customlistview;
/**
*
* @author manish
*
*/
public class Item {
int image;
String
name[];
public Item(int image, String[]
name) {
super();
this.image = image;
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String[] getName() {
return name;
}
public void setName(String[]
name) {
this.name = name;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="0.55" >
</ListView>
</LinearLayout>
5)list.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineraMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
6)AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.customlistview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.manish.customlistview.CustomListView"
android:label="@string/title_activity_custom_list_view"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
Thanks!