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!
Manish nice profile pic :)
ReplyDeleteAnd blog too...
Thanks for post and for pic, like on my facebook page :)
Deletevery good and helpful code !! :)
ReplyDeleteThanks shalu!
Deletehi, this is not related to the post, but how do i set the listview height fixed for all the rows? some of my listrow can haveve 1 line some have 2-3 lines depending on the TEXT i get from feeds and depending on the orientation. i mean text can occupy 3 lines in portrait but 1 line in landscape. so how can i have each occupy the same height ?
ReplyDeleteHi, why not this post related? I am creating listview item like image and textview dynamically in java code then why not it's related to post? and one more thing you can create any number of size row dynamically.
DeleteAnd yes if it is not related to you, you can search on google for other post.
Thanks,
Great Help Thanks! May I also ask how can i display my text and image from json to this view? do you have sample of that? big help thanks
ReplyDeleteI am sorry Kevin i don't have demo for parsing JSON but it is very simple you can search it on google. and for display in that list you can put that instead of imageArray.
ReplyDeleteManish im new to androoid programming i want to learn how to send a httprequest to a server and then recieve a json response ans then i want to genereate a listview based on the json response
ReplyDeletethanks a lot dude...very simple tut..!
ReplyDeleteYour welcome!
Deletethis is not working if i have more then 3 or 4 view items(:
ReplyDeletemeans not working if convert view is not null..if convert view is being recycled then it gives wiered behaviour
ReplyDeleteRegarded Sir/Mam, Good Morning. I am trying to make an ExpandableListView. My requirement i already attached you. how can i insert value in that l,list. What I made for this, I already attached all files. complete source code was not accepted by google server so i am sending you individually. Please mam let me know how to display child's value in form of total. ... Suppose I have one parent named as Cloth, so structure should be like this
ReplyDeleteCloth (20)
|
|------Pant (15)
| |
| |----Denim (5)
| |----Shirt (10)
| |
|-------Jeans ( 5 )
I have to display total . How to do this..
Check this post and modify according to your need-
Deletehttp://www.androidhub4you.com/2014/07/android-multilevel-expandable-listview.html
Hi Manish!
ReplyDeleteI did the same thing, making new views...
But the problem is when listview is scrolled faster, it skips some of the dynamically created views :/
Amazing, thank you
ReplyDeleteCan you explain how to use for SimpleCursorAdapter
Database like this :
_id > company >apps
0 Google Gmail,youtube,map..
Amazing, thank you
ReplyDeleteCan you explain how to use for SimpleCursorAdapter
Database like this :
_id | company |apps
0 |Google |Gmail,youtube,map..