update listView from ActionBarActivity - android

My MainActivity, which extends ActionBarActivity, has a ListView, which I want to reload using notifyDataSetChanged(), but I do not know how to do this.
My code so far:
public class MainActivity extends ActionBarActivity {
private JournalEntryDataSource datasource;
private ListView listView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new JournalEntryDataSource(this);
datasource.open();
listView = (ListView) findViewById(R.id.list);
List<JournalEntry> values = datasource.getAllJournalEntries();
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Log.i("app", "Deleting item number " + position);
datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));
//I would like to do this here:
//ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
//adapter.notifyDataSetChanged();
}
});
}
But I get an error when calling adapter.notifyDatasetChanged()...
Any tips? I've been looking at similar questions and no luck so far...
--------EDITED CODE---------
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new JournalEntryDataSource(this);
datasource.open();
listView = (ListView) findViewById(R.id.list);
values = datasource.getAllJournalEntries();
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
// use the SimpleCursorAdapter to show the
// elements in a ListView
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
listView.setLongClickable(true);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.i("hola","borrando la posiciĆ³n "+position);
JournalEntry removeMe = (JournalEntry)listView.getAdapter().getItem(position);
values.remove(removeMe);
datasource.deleteJournalEntry(removeMe);
//adapter.notifyDataSetChanged();
reloadMe();
return true;
}
});
}
public void reloadMe(){
//need to do this better... but meh.
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
}
protected ListView getListView() {
if (listView == null) {
listView = (ListView) findViewById(android.R.id.list);
}
return listView;
}
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;
}
}
}

Updated Answer for updated code
instead of creating the same activity again, you should choose to set another new adapter object with the new values object.
public void reloadMe(){
//need to do this better... but meh.
//Intent intent = new Intent(this, MainActivity.class);
//this.startActivity(intent);
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
}
Perhaps, you are deleting from
datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));
But you did not set "datasource" as the data source of the adapter. If you look carefully you did set the "values" list object-
listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
Perhaps you may need to also remove from the values list also.

Related

How to show different activities for different ListView items?

I have a ListView with several items and every item has it own Activity class to show details. How can I switch to an appropriate activity when user selects and item in this ListView?
My current code is following:
ListView listView;
ArrayAdapter<String> adapter;
String[] hotel = {"one room", "double room", "suit", "vip"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hotel);
listView.setAdapter(adapter);
}
Add setOnItemClickListener to your listView in onCreate under listView.setAdapter(adapter); :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(position == 0){
Intent intent = new Intent(MainActivity.this, OneRoomActivity.class);
startActivity(intent);
}
else if(position == 1){
Intent intent = new Intent(MainActivity.this, DoubleRoomActivity.class);
startActivity(intent);
}
//Do as above for rest of the list items
}
});
Hope this helps.

Set first Item checked of listview using ListFragment

I want to set first item checked of listview as default whenever an activity created. My activity consist of ListFragment which has ListView. The first item needs to be checked. I've write
listView.setItemChecked(0, true);
listView.setSelection(0);
listView.setSelected(true);
above code for check but on activity created it not get checked. kindly tell why and how i can achieve?
Full code:
public class LeftFilterContents extends ListFragment implements AdapterView.OnItemClickListener {
Get get;
View view;
ListView listView;
public interface Get {
void getData(int s);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
get = (Get) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> arrayList = new ArrayList<>();
listView = (ListView) view.findViewById(android.R.id.list);
arrayList.add("Type");
arrayList.add("Date");
arrayList.add("From to Date");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
setListAdapter(adapter);
listView.setItemChecked(0, true);
listView.setSelection(0);
listView.setSelected(true);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
get.getData(position);
}
ListView:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
Try this
yourListView.setSelection(0);
// updated the code
listView.setItemChecked(0,true)
//yourListView.getSelectedView().setSelected(true);
Add this line :
adapter.notifyDataSetChanged();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
So you will have :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
setListAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemChecked(0, true);
getListView().setOnItemClickListener(this);
adapter.notifyDataSetChanged();

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

how to declare another spinner id in one class

public class StartCalvert extends Activity {
Spinner spnr,spnr2;
String[] numsys = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
// this numsys how will i declare
String[] numsys2 = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startcalvert);
spnr2 = (Spinner)findViewById(R.id.spinner2);
spnr = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, numsys);
//declare here , like what above alike .
spnr.setAdapter(adapter);
spnr.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener()
Each Spinner has it's adapter has bind its Datasource with view on it so , most likely you need to make one for each Spinner and any other Data Views
in onCreate
spinner1 = (Spinner) findViewById(R.id.spinner1 );
spinner2 = (Spinner) findViewById(R.id.spinner2 );
ArrayAdapter<String> adapterSpinner1 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys);
spinner1.setDropDownViewResource(R.layout.simple_spinner_item);
spinner1.setAdapter(adapterSpinner1);
ArrayAdapter<String> adapterSpinner2 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys2);
spinner2.setDropDownViewResource(R.layout.simple_spinner_item);
spinner2.setAdapter(adapterSpinner2);
ItemSelectedListener itemSelectedListener = new ItemSelectedListener();
spinner1.setOnItemSelectedListener(itemSelectedListener);
spinner2.setOnItemSelectedListener(itemSelectedListener);
Declare inner class
class ItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long arg3) {
LoggerGeneral.info("OnItemselected pressed");
switch (parent.getId()) {
case R.id.spinner1:
break;
case R.id.spinner2:
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}

Listview item click issue

I have added leadbolt ad(entry ad). The advertising works correctly but listview.click doesn't work when I close ad from close sign. (Listview.click does not do anything, It works when I remove AdController)
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
private AdController myController;
final Activity act = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myController = new AdController(act, "111111111");
myController.loadAd();
soundfile= new int[] {R.raw.sound1,R.raw.sound2.....};
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked.
To fix this issue add following code to row view.
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

Categories

Resources