Finally after we run this project will show screenshot like this.
Lets start all together with Eclipse tool
1. Create new project File-->New--> Android Application Project rename to AndroidListView
choose blank activity and rename file main to ListMainActivity
2. After project has been created open it change extends class from Activity class to ListActivity class.
public class ListMainActivity extends ListActivity {}
3. In ListView each list item will be an xml layout, so we can customize each list item. Under folder /res/layout we create file call list_item.xml. After completed follow with this code.
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/labelitems" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" android:textSize="16dip" android:textStyle="bold" > </TextView>
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="mylist"> <item>iPhone</item> <item>Samsung</item> <item>LG</item> <item>Sony</item> <item>Nokia</item> <item>Black Battery</item> </string-array>
5. Now go to ListMainActivity.java and type follow with this code. In this case it read string resource and store as array of string.
package com.simple.www.listviewandroid; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class ListMainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // storing string resources into Array String[] phone = getResources().getStringArray(R.array.mylist); // Binding resources Array to ListAdapter this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, R.id.labelitems,phone)); } }
6. Now we start run this project you can see some item on list but they didn't do some thing. So we need to create activity for selecting single item on list.
7. Create new activity class just right click on folder package and select on New--> Class rename as SingleListActivity.java
8. Open ListMainActivity.java and type follow with this code
package com.simple.www.listviewandroid;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListMainActivity extends ListActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array String[] phone = getResources().getStringArray(R.array.mylist);
// Binding resources Array to ListAdapter this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items, R.id.labelitems,phone));
ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String phone = ((TextView) view).getText().toString();
Intent intent = new Intent(getApplicationContext(),SingleSelect.class);
intent.putExtra("phone",phone);
startActivity(intent);
}
});
}
}
We need to create activity for receive data from list when we click on each items.
9. Go to folder /layout right click on folder New-->XML file and type name to simple_list_item_single.xml follow with this code
<?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" android:background="#000" > <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25dp" android:textColor="#fff" android:id="@+id/tvlist" /> </LinearLayout>
This layout for SingleSelect.java
10. Open class SingleSelect.java and type follow with this code
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; /** * Created by Android on 3/23/2017. */ public class SingleSelect extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_list_item_single); TextView listphone = (TextView) findViewById(R.id.tvlist); Intent intent = getIntent(); String phone = intent.getStringExtra("phone"); listphone.setText(phone); } }
11. Finally we go to AndroidManifest,xml and type below with this code.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.simple.www.listviewandroid"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".ListMainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SingleSelect"/> </application> </manifest>
0 Comments