Activity_main.xml
=============
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp">
</ListView>
mainActivity.java
============
public class MainActivity extends Activity {
String fruits[]={"Apple","Orange","Banana","Mango"};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyListAdapter(this,fruits));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You clicked:"+fruits[pos], Toast.LENGTH_SHORT).show();
}
});
}
}
list_item_view.xml
=============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_marginTop="20dp"
android:textColor="#008000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/priceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:textColor="#008000"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
MyListAdapter.java
==============
public class MyListAdapter extends BaseAdapter {
private Context context;
private final String[] values;
public MyListAdapter(Context context, String[] values) {
// TODO Auto-generated constructor stub
this.context=context;
this.values=values;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View listView;
if(convertView==null){
listView=new View(context);
listView=inflater.inflate(R.layout.list_item_view, null);
TextView textView1=(TextView) listView.findViewById(R.id.nameView);
TextView textView2=(TextView) listView.findViewById(R.id.priceView);
ImageView imageView=(ImageView) listView.findViewById(R.id.imageView1);
String str=values[position];
if(str.equalsIgnoreCase("Apple")){
imageView.setImageResource(R.drawable.apple);
textView1.setText(values[0]);
textView2.setText("180.00");
}
if(str.equalsIgnoreCase("Orange")){
imageView.setImageResource(R.drawable.orange);
textView1.setText(values[1]);
textView2.setText("80.00");
}
if(str.equalsIgnoreCase("Banana")){
imageView.setImageResource(R.drawable.banana);
textView1.setText(values[2]);
textView2.setText("40.00");
}
if(str.equalsIgnoreCase("Mango")){
imageView.setImageResource(R.drawable.mangos);
textView1.setText(values[3]);
textView2.setText("60.00");
}
}else{
listView=convertView;
}
return listView;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
=============
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp">
</ListView>
mainActivity.java
============
public class MainActivity extends Activity {
String fruits[]={"Apple","Orange","Banana","Mango"};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyListAdapter(this,fruits));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You clicked:"+fruits[pos], Toast.LENGTH_SHORT).show();
}
});
}
}
list_item_view.xml
=============
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_marginTop="20dp"
android:textColor="#008000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/priceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:layout_marginLeft="150dp"
android:layout_marginTop="20dp"
android:textColor="#008000"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
MyListAdapter.java
==============
public class MyListAdapter extends BaseAdapter {
private Context context;
private final String[] values;
public MyListAdapter(Context context, String[] values) {
// TODO Auto-generated constructor stub
this.context=context;
this.values=values;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View listView;
if(convertView==null){
listView=new View(context);
listView=inflater.inflate(R.layout.list_item_view, null);
TextView textView1=(TextView) listView.findViewById(R.id.nameView);
TextView textView2=(TextView) listView.findViewById(R.id.priceView);
ImageView imageView=(ImageView) listView.findViewById(R.id.imageView1);
String str=values[position];
if(str.equalsIgnoreCase("Apple")){
imageView.setImageResource(R.drawable.apple);
textView1.setText(values[0]);
textView2.setText("180.00");
}
if(str.equalsIgnoreCase("Orange")){
imageView.setImageResource(R.drawable.orange);
textView1.setText(values[1]);
textView2.setText("80.00");
}
if(str.equalsIgnoreCase("Banana")){
imageView.setImageResource(R.drawable.banana);
textView1.setText(values[2]);
textView2.setText("40.00");
}
if(str.equalsIgnoreCase("Mango")){
imageView.setImageResource(R.drawable.mangos);
textView1.setText(values[3]);
textView2.setText("60.00");
}
}else{
listView=convertView;
}
return listView;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
No comments:
Post a Comment