Following example displays the list of items and on selection of particular item this item is passed to second activity using intents
package com.example.listadapter;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
protected static final int requestCode = 0;
VideoGet getObj = new VideoGet();
ArrayList<VideoGet> sample = new ArrayList<VideoGet>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getObj.setId("1");
getObj.setName("one");
sample.add(getObj);
for (VideoGet display : sample) {
System.out.println("inside for loop " + display.getId() + " "
+ display.getName());
}
VideoGet getObj1 = new VideoGet();
getObj1.setId("2");
getObj1.setName("two");
sample.add(getObj1);
for (VideoGet display : sample) {
System.out.println("inside second for loop " + display.getId()
+ " " + display.getName());
}
VideoGet getObj2 = new VideoGet();
getObj2.setId("3");
getObj2.setName("three");
sample.add(getObj2);
VideoGet getObj3 = new VideoGet();
getObj3.setId("4");
getObj3.setName("four");
sample.add(getObj3);
System.out
.println("getobj name is in mainactivity " + getObj.getName());
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomAdapter(this, sample));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
// TODO Auto-generated method stub
Object o = lv1.getItemAtPosition(position);
VideoGet video_items = (VideoGet)o;
Toast.makeText(MainActivity.this, "selected item is " +" "+video_items.getName()+" "+video_items.getId() , Toast.LENGTH_LONG).show();
Intent my_intent = new Intent(MainActivity.this,Detail_list.class);
my_intent.putExtra("name", video_items.getName());
//startActivity(my_intent);
startActivityForResult(my_intent, requestCode);
}
});
}
}
package com.example.listadapter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
public class MyCustomAdapter extends BaseAdapter {
private int imgid = R.drawable.arrow;
private LayoutInflater l_Inflater;
private ArrayList<VideoGet> listdisplay;
public MyCustomAdapter(Context context, ArrayList<VideoGet> sample) {
// TODO Auto-generated constructor stub
l_Inflater = LayoutInflater.from(context);
listdisplay = sample;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listdisplay.size();
}
@Override
public Object getItem(int position) {
return listdisplay.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.TextView01);
holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.TextView02);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(listdisplay.get(position).getName()+" ");
holder.txt_itemDescription.setText(listdisplay.get(position).getId());
holder.itemImage.setImageResource(imgid);
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
}
}
package com.example.listadapter;
public class VideoGet {
String name;
String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package com.example.listadapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Detail_list extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);
TextView tv1 = (TextView) findViewById(R.id.textView1);
Bundle mydata = getIntent().getExtras();
String name = mydata.getString("name");
tv1.setText(name);
}
}
package com.example.listadapter;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
protected static final int requestCode = 0;
VideoGet getObj = new VideoGet();
ArrayList<VideoGet> sample = new ArrayList<VideoGet>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getObj.setId("1");
getObj.setName("one");
sample.add(getObj);
for (VideoGet display : sample) {
System.out.println("inside for loop " + display.getId() + " "
+ display.getName());
}
VideoGet getObj1 = new VideoGet();
getObj1.setId("2");
getObj1.setName("two");
sample.add(getObj1);
for (VideoGet display : sample) {
System.out.println("inside second for loop " + display.getId()
+ " " + display.getName());
}
VideoGet getObj2 = new VideoGet();
getObj2.setId("3");
getObj2.setName("three");
sample.add(getObj2);
VideoGet getObj3 = new VideoGet();
getObj3.setId("4");
getObj3.setName("four");
sample.add(getObj3);
System.out
.println("getobj name is in mainactivity " + getObj.getName());
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomAdapter(this, sample));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
// TODO Auto-generated method stub
Object o = lv1.getItemAtPosition(position);
VideoGet video_items = (VideoGet)o;
Toast.makeText(MainActivity.this, "selected item is " +" "+video_items.getName()+" "+video_items.getId() , Toast.LENGTH_LONG).show();
Intent my_intent = new Intent(MainActivity.this,Detail_list.class);
my_intent.putExtra("name", video_items.getName());
//startActivity(my_intent);
startActivityForResult(my_intent, requestCode);
}
});
}
}
package com.example.listadapter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
public class MyCustomAdapter extends BaseAdapter {
private int imgid = R.drawable.arrow;
private LayoutInflater l_Inflater;
private ArrayList<VideoGet> listdisplay;
public MyCustomAdapter(Context context, ArrayList<VideoGet> sample) {
// TODO Auto-generated constructor stub
l_Inflater = LayoutInflater.from(context);
listdisplay = sample;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listdisplay.size();
}
@Override
public Object getItem(int position) {
return listdisplay.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.TextView01);
holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.TextView02);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(listdisplay.get(position).getName()+" ");
holder.txt_itemDescription.setText(listdisplay.get(position).getId());
holder.itemImage.setImageResource(imgid);
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
}
}
package com.example.listadapter;
public class VideoGet {
String name;
String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
package com.example.listadapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Detail_list extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);
TextView tv1 = (TextView) findViewById(R.id.textView1);
Bundle mydata = getIntent().getExtras();
String name = mydata.getString("name");
tv1.setText(name);
}
}
activity_main.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" >
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
detail_list.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" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello detail activity"
android:textColor="#0099CC"
android:textSize="20px"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/TextView01"
android:textColor="#0099CC"
android:textSize="15px"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello detail activity"
android:textColor="#0099CC"
android:textSize="20px"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/TextView01"
android:textColor="#0099CC"
android:textSize="15px"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
list_item.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" >
<ImageView
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150px"
android:paddingTop="10px" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:textColor="#0099CC"
android:textSize="20px"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#0099CC"
android:textSize="20px"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
No comments:
Post a Comment