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();
}
}
});
Related
I'm trying to make a search function with listview. Each item in listView have it own activity to be start. My problem was I'm using index position for search. If the I search "Mango", the index will automatically change to 0 (which the problem is) and start activity1. "Mango" should start activity2, not 1. So how to keep it to its own respective activity after being search?
package com.try2.androidlistviewwithsearch;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
public static final String products[] = new String [3];
// ArrayList for Listview
static ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Listview Data
products[0]="Apple";
products[1]="Mango";
products[2]="Orange";
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
//ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try1.class);
startActivityForResult(myIntent, 0);
}
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try2.class);
startActivityForResult(myIntent, 0);
}
}
});
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.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
}
});
}
}
Try to change your listener
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem vo value je sprava
String prodname = ((TextView) view.findViewById(R.id.product_name)).getText().toString();
if( prodname.equals("Apple") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try1.class);
startActivityForResult(myIntent, 0);
}
if( prodname.equals("Mango") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try2.class);
startActivityForResult(myIntent, 0);
}
if( prodname.equals("Orange") {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), try3.class);
startActivityForResult(myIntent, 0);
}
}
});
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);
Can someone please help me? I am trying to access a "position" variable itemclicked. I want the content of the other class to display a different thing depending on the list I clicked.
What I want is this: if I click position 0, then it takes me to another page and the TextView I created on that class changes to "you clicked position 0". If I click position 1, it changes to "you clicked position 1", etc.
I've attempted to do this, but it only works when I click position 0. If I click item 1, it shows "you have clicked position 0". I am very confused. I have tried using putexraa and other methods, but they are not working. Your help would be greatly appreciated.
package com.obi.arinzeapp;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<list> mymusic = new ArrayList<list>();
public list currentsong;
public int man;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populatemusiclist();
populateListView();
registerclick();
}
private void registerclick() {
ListView mus = (ListView) findViewById(R.id.musiclistview);
mus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long arg3) {
man = position;
try {
Class ourClass = Class.forName("com.obi.arinzeapp.Music");
Intent ourIntent = new Intent(MainActivity.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
private void populatemusiclist() {
mymusic.add(new list(R.drawable.ff, " Mr.J.Mederiors \"Constance\"",
R.drawable.medi));
mymusic.add(new list(R.drawable.ff, " Mr.J.Mederiors \"Constnce\"",
R.drawable.medi));
mymusic.add(new list(R.drawable.ff, " Mr.J.Mederiors \"Constnce\"",
R.drawable.medi));
}
private void populateListView() {
ArrayAdapter<list> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.musiclistview);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<list> {
public MyListAdapter() {
super(MainActivity.this, R.layout.viewitem, mymusic);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View veiwitem = convertView;
if (veiwitem == null) {
veiwitem = getLayoutInflater().inflate(R.layout.viewitem,
parent, false);
// return super.getView(position, convertView, parent);
}
list currentsong = mymusic.get(position);
ImageView imageview = (ImageView) veiwitem
.findViewById(R.id.imageView2);
imageview.setImageResource(currentsong.getNum());
TextView wor = (TextView) veiwitem.findViewById(R.id.word);
wor.setText(currentsong.getNameofsong());
ImageView imageview2 = (ImageView) veiwitem
.findViewById(R.id.imageView1);
imageview2.setImageResource(currentsong.getPicture());
return veiwitem;
}
}
#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;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Music extends Activity{
TextView name,song,album,about;
String nam,son,albu,abou;
RelativeLayout picc;
public int ye ;
MainActivity maa = new MainActivity();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stu
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
name= (TextView) findViewById(R.id.name);
song= (TextView) findViewById(R.id.song);
album=(TextView) findViewById(R.id.album);
about=(TextView) findViewById(R.id.about);
picc=(RelativeLayout) findViewById(R.id.picc);
maa.man= ye;
if(ye==0){
name.setText("You clicked position 1") ;
}else{
name.setText("You clicked position 2") ;
}
}
}
Pass the value of man in putExtra while setting the intent for that class in mainActivity
Class ourClass = Class.forName("com.obi.arinzeapp.Music");
Intent ourIntent = new Intent(MainActivity.this, ourClass);
ourIntent.putExtra("man",man);
startActivity(ourIntent);
And get this extra in Music Activity
extras = getIntent().getExtras();
newInt= extras.getInt("man");
This is the standard way to pass the values from one activity to another.
But the way you doing you can try making man as static variablebut it is not recommended.
Ok, so I have an ArrayAdapter and I have this displaying rows, each row includes some text and a delete button. The delete button is my problem... I have a listener listen for click and then it calls remove(position) which removes the view. (which is what I want)... although when I call onNotifyDataSetChanged() in the UI thread it re-adds the deleted row, presumable because it's not been deleted from that list (only the view)... I'm unaware on how I can fix this.
So to summarize....
I can delete the view which I want to delete using the delete button but after onNotifyDataSetChanged() is called the view is re-added.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
render = new tweetView(this);
LinearLayout canvas = (LinearLayout)findViewById(R.id.content_frame);
final ListView tweetList = (ListView)findViewById(R.id.tweetView);
tweetListFull = new ArrayList<Tweet>();
int resID = R.layout.tweet;
aa = new tweetAdapter(this, resID, tweetListFull);
Log.w("Check","Here1");
tweetList.setAdapter(aa);
Log.w("Check","Here1");
//tweetList.addView()
canvas.addView(render);
tweetList.setClickable(true);
tweetList.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
try {
//Nothing here yet.
}
catch(Exception e) {
// System.out.println("Nothing here yet.);
}
}
});
// UI //
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item, drawerListViewItems));
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
int updateTime = 5000; // 5 seconds.
int distance = 1; // meters
LocationListener myLocListener = new LocationListener(){
#Override
public void onLocationChanged(Location arg0) {
Log.w("location",String.valueOf(arg0));
twitterTest tweet = new twitterTest();
Log.w("Test","Location Changed");
Log.w("test","Long: "+String.valueOf(arg0.getLongitude())+ "Lat: "+String.valueOf(arg0.getLatitude()));
tweet.execute(arg0.getLongitude(),arg0.getLatitude()); // The argument for this is the query to search for.
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(provider, updateTime,distance, myLocListener);
}
tweetAdapter
package com.example.networkingcoursework;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
public class tweetAdapter extends ArrayAdapter<Tweet>{
int resource;
Tweet tweet = null;
Context context;
//ArrayList<Tweet> tweetListFull;
public tweetAdapter(Context _context, int _resource,ArrayList<Tweet> _objects) {
super(_context, _resource, _objects);
resource = _resource;
//tweetListFull = _objects;
context = _context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(final int position, View convertView, ViewGroup parent){
LinearLayout tweetView;
tweet = getItem(position);
String tweetStatus = tweet.getStatus();
if(convertView == null){
tweetView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource,tweetView, true);
}
else{
tweetView = (LinearLayout) convertView;
}
TextView idView = (TextView)tweetView.findViewById(R.id.tweetText);
idView.setText(String.valueOf(tweetStatus));
Button tweetClose = (Button)tweetView.findViewById(R.id.tweetClose);
tweetClose.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int i = (Integer)v.getTag();
remove(getItem(position));
notifyDataSetChanged();
}
});
tweetClose.setTag(position);
return tweetView;
}
}
It seems that you don't need notifyDataSetChanged because it holds an instance of the list from the activity. That is why the list is returning to its original state. By calling remove, it is changing the list. See this similar topic for a better explanation: notifyDataSetChanged example
As seen here, ArrayAdapter.remove() would not work with plain java arrays such as String[], and you do in MainActivity:
drawerListViewItems = getResources().getStringArray(R.array.items);
Consider to use getStringArrayList instead.
Also you don't need to manually call notifyDataSetChange(), you can see this
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);
}
});