Step 1. Create the XML file
<?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="vertical"> <ListView android:id="@+id/lvItems" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Step 2: Studnet.java
package com.scancode.acutesoft.telephonymanagerapp; public class Student { String email,phone,address; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Step 3: MainActivity.java
package com.scancode.acutesoft.telephonymanagerapp; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends Activity { ListView lvItems; ArrayList<Student> studentArrayList ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvItems = (ListView) findViewById(R.id.lvItems); studentArrayList = new ArrayList<Student>(); dataSaving(); CustomAdapter adapter = new CustomAdapter(MainActivity.this,studentArrayList); lvItems.setAdapter(adapter); } private void dataSaving() { Student student = new Student(); student.setEmail("abc@gmail.com"); student.setPhone("1234567890"); student.setAddress("Hyderabad"); studentArrayList.add(student); student = new Student(); student.setEmail("xyz@gmail.com"); student.setPhone("1234567890"); student.setAddress("Banglore"); studentArrayList.add(student); student = new Student(); student.setEmail("xyz@gmail.com"); student.setPhone("1234567890"); student.setAddress("Banglore"); studentArrayList.add(student); student = new Student(); student.setEmail("xyz@gmail.com"); student.setPhone("1234567890"); student.setAddress("Banglore"); studentArrayList.add(student); } }
Step 4: CustomAdapter.java
package com.scancode.acutesoft.telephonymanagerapp; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; public class CustomAdapter extends BaseAdapter { ArrayList<Student> studentList; Context mContext; public CustomAdapter(Context context, ArrayList<Student> studentArrayList) { this.mContext = context; this.studentList = studentArrayList; } @Override public int getCount() { return studentList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Student student = studentList.get(position); convertView = LayoutInflater.from(mContext).inflate(R.layout.student_row,null); TextView tvStudEmail = (TextView) convertView.findViewById(R.id.tvStudEmail); TextView tvStudPhone = (TextView) convertView.findViewById(R.id.tvStudPhone); TextView tvStudAddress = (TextView) convertView.findViewById(R.id.tvStudAddress); tvStudEmail.setText(student.getEmail()); tvStudPhone.setText(student.getPhone()); tvStudAddress.setText(student.getAddress()); return convertView; } }
Manikanta Reddy Apr 18 '16 at 11:01 2016-04-18 11:01
source share