ListView with Context menu in android
1,Define the xml file:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
2, Define the layout for items in the ListView (listitem.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="35dip"
android:padding="8dip"
android:textColor="#990000"
/>
3, Define the array in string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Listview_contextmenuActivity!</string>
<string name="app_name">Listview_contextmenu</string>
<string-array name="Countries"><item>jisho</item>
<item>aju</item>
<item >gino</item>
<item >binish</item>
<item>alex</item>
<item >vishnu</item>
<item >arun</item>
</string-array>
</resources>
4, Retrieve the array element from strings.xml: and Sort the string;
Resources res = getResources();
Countries = getResources().getStringArray(R.array.Countries);
Arrays.sort(Countries);
5, Define the Array adapter and set the register for Context menu.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
6, Override the Context menu:-
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
if (v.getId()==R.id.list)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(Countries[info.position]);
String[ ] menuItems = getResources().getStringArray(R.array.Countries);
for (int i = 0; i<menuItems.length; i++)
{
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
--------------------------------------------------------------------
package l.c;
import java.util.Arrays;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Listview_contextmenuActivity extends Activity {
/** Called when the activity is first created. */
private String[] Countries;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
Countries = getResources().getStringArray(R.array.Countries);
Arrays.sort(Countries);
ListView list = (ListView)findViewById(R.id.list);
//ImageView icon=(ImageView) findViewById(R.id.icon);
// ArrayAdapter<String> a=new ArrayAdapter<String>(this, R.layout.main, R.id.offerdetails,name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(Countries[info.position]);
String[] menuItems = getResources().getStringArray(R.array.Countries);
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
}
----------------------------------------------------------------------------------
1,Define the xml file:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
/>
</LinearLayout>
2, Define the layout for items in the ListView (listitem.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="35dip"
android:padding="8dip"
android:textColor="#990000"
/>
3, Define the array in string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Listview_contextmenuActivity!</string>
<string name="app_name">Listview_contextmenu</string>
<string-array name="Countries"><item>jisho</item>
<item>aju</item>
<item >gino</item>
<item >binish</item>
<item>alex</item>
<item >vishnu</item>
<item >arun</item>
</string-array>
</resources>
4, Retrieve the array element from strings.xml: and Sort the string;
Resources res = getResources();
Countries = getResources().getStringArray(R.array.Countries);
Arrays.sort(Countries);
5, Define the Array adapter and set the register for Context menu.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
6, Override the Context menu:-
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
if (v.getId()==R.id.list)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(Countries[info.position]);
String[ ] menuItems = getResources().getStringArray(R.array.Countries);
for (int i = 0; i<menuItems.length; i++)
{
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
--------------------------------------------------------------------
package l.c;
import java.util.Arrays;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Listview_contextmenuActivity extends Activity {
/** Called when the activity is first created. */
private String[] Countries;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
Countries = getResources().getStringArray(R.array.Countries);
Arrays.sort(Countries);
ListView list = (ListView)findViewById(R.id.list);
//ImageView icon=(ImageView) findViewById(R.id.icon);
// ArrayAdapter<String> a=new ArrayAdapter<String>(this, R.layout.main, R.id.offerdetails,name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(Countries[info.position]);
String[] menuItems = getResources().getStringArray(R.array.Countries);
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
}
----------------------------------------------------------------------------------
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete