List View
Listview is a view group that displays a list of
scrollable items.
The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and
converts each item result into a view that's placed into the list.
Example Coding:
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/emailid"
android:layout_below="@+id/name"
android:layout_alignParentStart="true" />
</RelativeLayout>
MainActivity:
public class MainActivity extends ActionBarActivity {
ListView list;
String[] name = {
"" +
"Lucy",
"Harry",
"Lizzy",
"Monty",
} ;
String[] emailid = {
""+"lucy@there.com","harry@sally.com","lizzy@dizzy.com","monty.python.com",
};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Adapter adapter = new Adapter(MainActivity.this, name, emailid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Adapter:
public class Adapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] name;
private final String[] emailid;
public Adapter(Activity context,
String[] web, String[] imageId) {
super(context, R.layout.list,web);
this.context = context;
this.name = web;
this.emailid = imageId;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
TextView imageView = (TextView) rowView.findViewById(R.id.img);
txtTitle.setText(name[position]);
imageView.setText(emailid[position]);
return rowView;
}
}
Output:
0 comments:
Post a Comment