download: http://www.mediafire.com/?us2oa4vcvav726a
main.xml
-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" " />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
list_item.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="a" />
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Black"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:padding="1dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
grid_item.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:padding="5dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
values/country.xml
---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item name="bangladesh">bangladesh</item>
<item name="usa">usa</item>
<item name="russia">russia</item>
<item name="england">england</item>
<item name="japan">japan</item>
<item name="korea">korea</item>
<item name="china">china</item>
</string-array>
</resources>
MainActivity.java
----------------------------------
package home.ListViewBaseAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends ListActivity
{
String[] countries;
int c=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countries = getResources().getStringArray(R.array.countries_array);
//Toast.makeText(this,"Main class",Toast.LENGTH_LONG).show();
Log.d("------------main class-----------", "start");
setListAdapter(new MyListAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1,countries));
//Toast.makeText(this,"end of Main class",Toast.LENGTH_LONG).show();
Log.d("------------main class-----------", "end");
ListView listView = getListView();//getListView() from ListActivity //public class ListView
listView.setTextFilterEnabled(true);
}
}
MyListAdapter.java
---------------------------------
package home.ListViewBaseAdapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyListAdapter extends ArrayAdapter<String> {
Context context;
Integer resource, textViewResourceId;
String[] strings;
GridView myGrid;
ImageView imageViewCityImage;
ImageAdapter imageAdapter;
static final String[] MOBILE_OS = new String[] { "Android", "iOS",
"Windows", "Blackberry" };
int c = 0;
public MyListAdapter(Context _context, int _resource, int _textViewResourceId,
String[] _strings) {
super(_context, _resource, _textViewResourceId, _strings);
context = _context;
resource = _resource;
textViewResourceId = _textViewResourceId;
strings = _strings;
// Toast.makeText(Main.this,"getView() row="+c,Toast.LENGTH_LONG).show();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
c++;
// Toast.makeText(Main.this,"getView() c="+c,Toast.LENGTH_LONG).show();
Log.d("------------getView()-----------c=" + c, "c=" + c);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = context.getResources().getStringArray(
R.array.countries_array);
TextView tv = (TextView) row.findViewById(R.id.textView1);
myGrid=(GridView) row.findViewById(R.id.myGrid);
tv.setText(items[position]);
myGrid.setAdapter(imageAdapter=new ImageAdapter(context, MOBILE_OS) );
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
Log.d("------------row=-----------" + row, "row=" + row);
return row;
}
}
ImageAdapter.java
---------------------------------------
package home.ListViewBaseAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter
{
private Context context;
private final String[] mobileValues;
public ImageAdapter(Context context, String[] mobileValues)
{
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_item, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.windows_logo);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return mobileValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
main.xml
-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" " />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
list_item.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="a" />
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Black"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:padding="1dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
grid_item.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:padding="5dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
values/country.xml
---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item name="bangladesh">bangladesh</item>
<item name="usa">usa</item>
<item name="russia">russia</item>
<item name="england">england</item>
<item name="japan">japan</item>
<item name="korea">korea</item>
<item name="china">china</item>
</string-array>
</resources>
MainActivity.java
----------------------------------
package home.ListViewBaseAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends ListActivity
{
String[] countries;
int c=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countries = getResources().getStringArray(R.array.countries_array);
//Toast.makeText(this,"Main class",Toast.LENGTH_LONG).show();
Log.d("------------main class-----------", "start");
setListAdapter(new MyListAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1,countries));
//Toast.makeText(this,"end of Main class",Toast.LENGTH_LONG).show();
Log.d("------------main class-----------", "end");
ListView listView = getListView();//getListView() from ListActivity //public class ListView
listView.setTextFilterEnabled(true);
}
}
MyListAdapter.java
---------------------------------
package home.ListViewBaseAdapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyListAdapter extends ArrayAdapter<String> {
Context context;
Integer resource, textViewResourceId;
String[] strings;
GridView myGrid;
ImageView imageViewCityImage;
ImageAdapter imageAdapter;
static final String[] MOBILE_OS = new String[] { "Android", "iOS",
"Windows", "Blackberry" };
int c = 0;
public MyListAdapter(Context _context, int _resource, int _textViewResourceId,
String[] _strings) {
super(_context, _resource, _textViewResourceId, _strings);
context = _context;
resource = _resource;
textViewResourceId = _textViewResourceId;
strings = _strings;
// Toast.makeText(Main.this,"getView() row="+c,Toast.LENGTH_LONG).show();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
c++;
// Toast.makeText(Main.this,"getView() c="+c,Toast.LENGTH_LONG).show();
Log.d("------------getView()-----------c=" + c, "c=" + c);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = context.getResources().getStringArray(
R.array.countries_array);
TextView tv = (TextView) row.findViewById(R.id.textView1);
myGrid=(GridView) row.findViewById(R.id.myGrid);
tv.setText(items[position]);
myGrid.setAdapter(imageAdapter=new ImageAdapter(context, MOBILE_OS) );
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
Log.d("------------row=-----------" + row, "row=" + row);
return row;
}
}
ImageAdapter.java
---------------------------------------
package home.ListViewBaseAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter
{
private Context context;
private final String[] mobileValues;
public ImageAdapter(Context context, String[] mobileValues)
{
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_item, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.windows_logo);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return mobileValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
No comments:
Post a Comment