I have an Intent that extends a ListActivity. In my onCreate method after having populated the list adapter I use registerForContextMenu(getListView()); to register for a context menu.
Now it is working and the context menu has its original function which is; once I click and hold down on an item the context menu opens.
Can I open the context menu on a single click (without having to hold down on the list)?
All help is appreciated.
Here is another simpler way to show context menu on single click.
private void addOnClickListener()
{
contactList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.showContextMenu();
}
})
}
replace contactList by your ListView and make sure to call this method after the initialization of ListView.
call activity.openContextMenu(l) onitem click event to open contextmenu on single click and onLongClick call activity.closeContextMenu()
Example
import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MyListView extends ListActivity implements OnItemLongClickListener {
/** Called when the activity is first created. */
Activity activity = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS);
setListAdapter(arrayAdapter);
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
this.registerForContextMenu(lv);
lv.setOnItemLongClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("...11configuration is changed...");
}
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"item1",
"item2",
"item3",
"item4",
"item5",
"item6",
"item7",
"item8",
"item9",
"item10",
"item11"
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
activity.openContextMenu(l);
System.out.println("...context is called");
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
System.out.println("...on create context menu...");
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("...on long click close context menu...");
activity.closeContextMenu();
// TODO Auto-generated method stub
return false;
}
I don't think it's working smoothly. Calling openContextMenu(l) will cause item.getMenuInfo() to be null (inside method onContextItemSelected(MenuItem item)).
You should call l.showContextMenuForChild(v) instead of openContextMenu(l).
this work perfect....
listmp3 = (ListView) findViewById(R.id.results_mp3);
registerForContextMenu(listmp3);
listmp3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listmp3.showContextMenuForChild(view);
}
});
Related
I've created an array list and display it in a list view with simple list item multiple choice but i cannot check or tick the items on the list, when i click on the items nothing happens. Please check my code below and tell me what i am doing wrong.
package com.example.arrays;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
public class MainActivity extends Activity {
ListView showList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView show = (TextView)findViewById(R.id.txtShow);
final Random generate = new Random();
showList = (ListView)findViewById(R.id.listView1);
final String[] myAttraction = new String[4];
myAttraction[0]= "Walter Sisulu National Botanical Garden ";
myAttraction[1]= "Coca-Cola Dome";
myAttraction[2]= "Promusica Theatre";
myAttraction[3]= "Unisa Science Campus";
Button arrays = (Button)findViewById(R.id.button1);
arrays.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*int random = generate.nextInt(4);
String display = myAttraction[random];
show.setText(display);*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
}
});
showList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Add a OnItemClickListener like this to check/uncheck the CheckedTextView when user click on an item
showList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// change the checkbox state
CheckedTextView checkedTextView = ((CheckedTextView)view);
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
change your code as following....
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
arrays.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*int random = generate.nextInt(4);
String display = myAttraction[random];
show.setText(display);*/
}
});
showList.setOnItemClickListener(new OnItemClickListener() {
public boolean onItemClick(AdapterView<?> arg0, View arg1, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
Set choiceMode property of your list to multipleChoice. I'm implementing multiple choice list in such a way in my applications, and it surely works.
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setAdapter(adapter);
To this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice, myAttraction);
showList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
showList.setAdapter(adapter);
Just Add this line if you have a ListView and have selected simple_list_item_multiple_choice and you are unable to interact with the checkbox.
YourListViewName.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
In my widget, when an item in a listview is long-pressed, I want that item to be launched (in my coding currently however, a normal click does this also but for right now I am making the long click do something so that I have it set up for later when I need to change what the long click does).
Here is my coding:
package com.example.awesomefilebuilderwidget;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
// implement event when an item on list view is selected via long-click for drag and drop
mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public void onItemLongClick(AdapterView parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}
As you can see, I am basically setting up the long click to do the same thing as a normal click but I get the error that the return type is compatible on this line:
public void onItemLongClick(AdapterView parent, View view,
Quick fix tells me to change the return type to boolean (which isn't right).
How can I fix this?
Quick fix tells me to change the return type to boolean (which isn't right).
Why isn't this right?
According to the Docs
The quick fix is correct. Give it a return type of boolean and return true. This let's it know that the long click was successful.
How to use ActionBarActivity of "android-support-v7-appcompat" in the activity which Extends the ListActivity.
For Example I have an Activity
public class xxxxxListActivity
extends ListActivity implements OnItemSelectedListener {
// ...................
}
In the above activity i want to use "ActionBarActivity" but as java dosent support multiple inheritance I am not able to get it working.
Here's an implementation of ActionBarListActivity:
public abstract class ActionBarListActivity extends ActionBarActivity {
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
}
Just like regular ListActivity, you'll need a layout that contains a ListView with the ID android.R.id.list ("#android:id/list" in XML).
The spiel in getListAdapter() is to handle cases where header views have been added to the ListView. Seems like ListView sets its own adapter to a HeaderViewListAdapter, so we have to try and get the wrapped adapter to prevent casting errors.
Edit: Try adding this function to satisfy the need for onListItemClick:
protected void onListItemClick(ListView lv, View v, int position, long id) {
getListView().getOnItemClickListener().onItemClick(lv, v, position, id);
}
Maybe you can try to extend ActionBarActivity and for default layout for that activity set some layout that has ListView.
Something like this:
public class AlarmListActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_list_view);
ListView lv = (ListView) findViewById(R.id.listView1);
// populate list view
}
}
and correcponding layout file:
<LinearLayout>
<ListView
android:id="#+id/listView1">
</ListView>
</LinearLayout>
Instead of creating the layout in XML, I decided to do it in code. The file a is a single drop in replacement in your code.
package com.stackoverflow.free.examples;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
/**
* based of https://raw.githubusercontent.com/android/platform_frameworks_base/d6c1919779acb042392615637b9007e0c4b89023/core/java/android/app/ListActivity.java
* Created by elcuco on 5/27/2014.
*/
#SuppressWarnings("UnusedDeclaration")
public class SupportListActivity extends ActionBarActivity {
protected ListAdapter mAdapter;
protected ListView mList;
protected TextView mEmptyMessage;
#Override
protected void onCreate(Bundle savedBundle)
{
super.onCreate(savedBundle);
mEmptyMessage = new TextView(this);
mEmptyMessage.setText("No results");
mList = new ListView(this);
mList.setEmptyView(mEmptyMessage);
LinearLayout ll = new LinearLayout(this);
ll.addView(mEmptyMessage, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
ll.addView(mList, TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
setContentView(ll);
}
public void setListAdapter(ListAdapter adapter) {
synchronized (this) {
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
/**
* Get the activity's list view widget.
*/
public ListView getListView() {
return mList;
}
/**
* Set the currently selected list item to the specified
* position with the adapter's data
*
* #param position the position on list to select
*/
public void setSelection(int position) {
mList.setSelection(position);
}
/**
* Get the position of the currently selected list item.
*/
public int getSelectedItemPosition() {
return mList.getSelectedItemPosition();
}
/**
* Get the cursor row ID of the currently selected list item.
*/
public long getSelectedItemId() {
return mList.getSelectedItemId();
}
private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
onListItemClick((ListView)parent, v, position, id);
}
};
/**
* This method will be called when an item in the list is selected.
* Subclasses should override. Subclasses can call
* getListView().getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* #param l The ListView where the click happened
* #param v The view that was clicked within the ListView
* #param position The position of the view in the list
* #param id The row id of the item that was clicked
*/
protected void onListItemClick(ListView l, View v, int position, long id) {
}
}
my answer based on accepted one, and also contains onListItemClick implementation. But it has a problem with empty view.
public abstract class ActionBarListActivity extends ActionBarActivity {
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
mListView.setOnItemClickListener(mOnClickListener);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
} else {
return adapter;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) { }
private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
onListItemClick((ListView) parent, v, position, id);
}
};
}
This solution is based on the accepted solution by #patrick. Here is the full code:
First the XML layout file activity_main.xml. Notice that I have an ListView with ID entryList
<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="com.example.MainActivity" >
<ListView
android:id="#+id/entryList"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Next is my own ActionBarListActivity. You'd notice some changes. I wanted to make it generic and reusable as possible.
package com.example.api;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public abstract class ActionBarListActivity extends ActionBarActivity {
private final class ListOnItemClickListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> lv, View v, int position, long id) {
onListItemClick((ListView) lv, v, position, id);
// String str = ((TextView) arg1).getText().toString();
// Toast.makeText(getBaseContext(), str,
// Toast.LENGTH_LONG).show();
// Intent intent = new Intent(getBaseContext(),
// your_new_Intent.class);
// intent.putExtra("list_view_value", str);
// startActivity(intent);
}
}
private ListView mListView;
protected ListView getListView() {
if (mListView == null) {
initListView();
}
return mListView;
}
private void initListView() {
mListView = (ListView) findViewById(getListViewId());
if (mListView == null) {
throw new RuntimeException(
"ListView cannot be null. Please set a valid ListViewId");
}
mListView.setOnItemClickListener(new ListOnItemClickListener());
}
protected abstract int getListViewId();
protected void setListAdapter(ListAdapter adapter) {
getListView().setAdapter(adapter);
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
// No default functionality. To override
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
} else {
return adapter;
}
}
}
Next is my MainActivity extending the above class.
package com.example;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.api.ActionBarListActivity;
public class MainActivity extends ActionBarListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(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.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d("click", "Position click " + position);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
#Override
protected int getListViewId() {
return R.id.entryList;
}
}
Basically, by overriding onListItemClick() you can say what to do when user accepts something.
Let me know your thoughts/issues in the comments.
Cheers
Add following line of code to your class and make sure that the class implements AdapterView.OnItemClickListener:
getListView().setOnItemClickListener(this);
List_Remind.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="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_weight="1"
android:textSize="30px" >
</TextView>
<ImageButton
android:background="#null"
android:id="#+id/imageButton1"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10dp"
android:src="#drawable/delete_task" />
</LinearLayout>
ReminderListActivity.java
package com.dummies.android.taskreminder;
import com.dummies.android.taskreminder.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ReminderListActivity extends Activity implements OnClickListener{
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private RemindersDbAdapter mDbHelper;
ListView lvRemind;
ImageButton btnAdd,btnBack,btnSettings;
TextView text;
LinearLayout layout;
ArrayAdapterExample aAdapter;
int pos;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// setContentView(R.layout.reminder_list);
LayoutInflater inflate = LayoutInflater.from(this);
layout = (LinearLayout)inflate.inflate(R.layout.main, null);
this.setContentView(layout);
btnAdd = (ImageButton) findViewById(R.id.btnAdd);
btnBack = (ImageButton) findViewById(R.id.btnBackMain);
btnSettings = (ImageButton) findViewById(R.id.btnSettings);
btnAdd.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnSettings.setOnClickListener(this);
mDbHelper = new RemindersDbAdapter(this);
mDbHelper.open();
fillData();
lvRemind.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// Intent i = new Intent(ReminderListActivity.this, ReminderEditActivity.class);
// i.putExtra(RemindersDbAdapter.KEY_ROWID, arg2);
// startActivityForResult(i, ACTIVITY_EDIT);
// Toast.makeText(ReminderListActivity.this, "Hello", Toast.LENGTH_LONG).show();
}
});
}
private void fillData() {
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
// Now create a simple cursor adapter and set it to display
// SimpleCursorAdapter reminders =
// new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
// setListAdapter(reminders);
text = (TextView)findViewById(R.id.TextView02);
lvRemind = (ListView)findViewById(R.id.alist);
aAdapter = new ArrayAdapterExample(ReminderListActivity.this, mDbHelper.fetchAllReminders());
lvRemind.setAdapter(aAdapter);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu_item_longpress, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_delete:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteReminder(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createReminder() {
Intent i = new Intent(this, ReminderEditActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
// #Override
// protected void onListItemClick(ListView l, View v, int position, long id) {
// super.onListItemClick(l, v, position, id);
// Intent i = new Intent(this, ReminderEditActivity.class);
// i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
// startActivityForResult(i, ACTIVITY_EDIT);
// }
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
createReminder();
break;
case R.id.btnSettings:
Intent i = new Intent(this, TaskPreferences.class);
startActivity(i);
break;
case R.id.btnBackMain:
onBackPressed();
break;
default:
break;
}
}
#SuppressLint("NewApi")
#Override
public void onBackPressed() {
super.onBackPressed();
}
class ArrayAdapterExample extends CursorAdapter {
public ArrayAdapterExample(Context context, Cursor c) {
super(context, c);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView textViewPersonName = (TextView) view.findViewById(R.id.label);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
Log.d("Index: ", ""+cursor.getPosition());
pos = cursor.getPosition();
ImageButton ibDel =(ImageButton)view.findViewById(R.id.imageButton1);
ibDel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(ReminderListActivity.this, "Hello", Toast.LENGTH_SHORT).show();
lvRemind.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(ReminderListActivity.this, ""+lvRemind.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show();
}
});
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.list_remind, parent, false);
return retView;
}
}
}
How can I delete particular item from custom listview. I have added button in custom listview. I want to delete item using particular button on that position.
As for deleting stuff, you need to delete from the ADAPTER not a list. You don't need to keep your own copy of the items (unless you are using them for something else) because there is a list of item inside the array. you need to call adapter.remove(item) instead of list.remove(item)
Well you just remove the desired item from the list using the remove() method of your ArrayAdapter.
A possible way to do that would be:
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
Another way would be to modify the ArrayList and call notifyDataSetChanged() on the ArrayAdapter.
arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
Hi try the following code to delete the Item from Listview
Button button1 = (Button) findViewById(R.id.create_message);
button1.setOnClickListener(new OnClickListener()
public void onClick(View v)
{
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}
}
});
I currently have a ListActivity and I am looking to start a new activity based on the selection in the list. Based upon Intents and Extras, how can I make this possible? Here is my code so far:
package com.fragile.honbook;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class GuideSelection extends ListActivity{
private TextView selection;
private static final String[] heroes={ "Agility", "Intelligence",
"Strength"};
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.heroselect);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
heroes));
selection=(TextView)findViewById(R.id.select);
}
public void onListItemClick(ListView parent, View v,
int position, long id){
}
}
Like this
If you want to call an activity called NewActivity after clicking the list item with sending data as extra you can do this
public void onListItemClick(ListView parent, View v,
int position, long id){
Intent intent = new Intent(GuidSelection.this, NewActivity.Class());
intent.putExtra("DATA",heroes[position]);
GuideSelection.startActivity(intent);
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, NewActivity.class);
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
Update (with different activities per list item):
final Map<String, Class> activities = new HashMap<String, Class>();
{
activities.put("agility", AgilityActivity.class);
activities.put("intelligence", IntelligenceActivity.class);
// add more here
}
public void onListItemClick(ListView parent, View v, position, long id){
Intent intent = new Intent(GuideSelection.this, activities.get(heroes[position]));
intent.putExtra("hero", heroes[position]);
startActivity(intent);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position==1){
//First item clicked.
Intent intent = new Intent(this,NewActivity.class);
startActivity(intent));
}
// handle else ifs
}
This is just an idea. You may wish to improvise this to too much of if elses.