Since I'm getting a few questions about the list view itself and what
I'm doing with it, here is that code. This code worked perfectly with
a simple_list_view_item_1 (or whatever it is called) and a regular
ArrayAdapter. All I did was switch out the layout resource I specify
for the adapter and I use my own custom adapter.
I have tried removing the CheckBox also because I was wondering if
that was stealing focus somehow but that made no difference. I think
the problem must have something to do with nesting LinearLayouts
inside the top level item.
package net.esalazar.alarmsutta;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
public class Alarmsutta extends Activity {
private ArrayList<Alarm> alarms = new ArrayList<Alarm>();
private Alarm selectedAlarm;
// Options menu codes
public final int MENU_NEW = Menu.FIRST;
public final int MENU_EDIT = Menu.FIRST + 1;
public final int MENU_DELETE = Menu.FIRST + 2;
// Intent codes for starting activities
public final int NEW_ALARM = 1;
public final int EDIT_ALARM = 2;
ListView alarmsListView;
AlarmListAdapter aa;
private final Alarmsutta selfRef = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up array adapter and viewer for alarms list
alarmsListView = (ListView)findViewById(R.id.alarmsListView);
// keep selectedAlarm updated and available for other methods
to use.
alarmsListView.setOnItemClickListener(new OnItemClickListener
() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int
index,long arg3) {
// Tell the NewAlarm activity what to expect
Intent i = new Intent(selfRef,NewAlarm.class);
i.putExtra("expiry",
alarms.get(index).getExpiry() );
i.putExtra("message",
alarms.get(index).getMessage() );
i.putExtra("enabled",
alarms.get(index).isEnabled() );
i.putExtra("index", index);
startActivityForResult(i, EDIT_ALARM);
}
});
alarmsListView.setOnItemSelectedListener(new
OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View
arg1, int
index, long arg3) {
selectedAlarm = alarms.get(index);
}
public void onNothingSelected(AdapterView<?> arg0) {
selectedAlarm = null;
}
});
aa = new AlarmListAdapter(this, R.layout.alarmlist_item,
alarms);
alarmsListView.setAdapter(aa);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if( resultCode != Activity.RESULT_OK )
return;
String expiryString = data.getStringExtra("expiry");
String title = data.getStringExtra("title");
String message = data.getStringExtra("message");
// enabled is not editable and is turned on by default
// in new alarms.
int index = data.getIntExtra("index", -1);
switch( requestCode ) {
case( NEW_ALARM ): {
Alarm newAlarm = new Alarm(expiryString, title,
message);
alarms.add(newAlarm);
aa.notifyDataSetChanged();
break;
}
case( EDIT_ALARM ): {
// If we only get the default value of -1 then
something is wrong
if( index == -1 )
throw new RuntimeException();
alarms.get(index).setExpiry(expiryString);
alarms.get(index).setTitle(title);
alarms.get(index).setMessage(message);
aa.notifyDataSetChanged();
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Add menu item for creating a new alarm and
// one for deleting selected alarm
menu.add(0, MENU_NEW, Menu.NONE, R.string.menu_new );
menu.add(0, MENU_EDIT, Menu.NONE, R.string.menu_edit );
menu.add(0, MENU_DELETE, Menu.NONE, R.string.menu_delete );
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem editItem = menu.findItem(MENU_EDIT);
MenuItem deleteItem = menu.findItem(MENU_DELETE);
boolean showEditOptions = (selectedAlarm != null);
editItem.setVisible(showEditOptions);
deleteItem.setVisible(showEditOptions);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Figure out which item this is and act accordingly
// If it is to create or edit
switch( item.getItemId() ) {
case( MENU_NEW ): {
Intent i = new Intent(this,NewAlarm.class);
startActivityForResult(i, NEW_ALARM);
return true;
}
case( MENU_EDIT ): {
// Tell the NewAlarm activity what to expect
Intent i = new Intent(this,NewAlarm.class);
i.putExtra("expiry",
selectedAlarm.getExpiryString() );
i.putExtra("title", selectedAlarm.getTitle() );
i.putExtra("message",
selectedAlarm.getMessage() );
i.putExtra("index",
alarms.indexOf(selectedAlarm));
startActivityForResult(i, EDIT_ALARM);
return true;
}
case( MENU_DELETE ): {
alarms.remove(selectedAlarm);
aa.notifyDataSetChanged();
selectedAlarm = null;
return true;
}
}
return false;
}
}
On Jul 9, 8:52 am, "nEx.Software" <[email protected]> wrote:
> You'll need an ItemClickListener set for your ListView - Though I am
> sure that you already know that...
>
> Something like:
> alarmsListView.setOnItemClickListener(ItemClickListener);
>
> On Jul 9, 8:46 am, extrapedestrian <[email protected]> wrote:
>
> > sory not onKey but onClickListener...
>
> > On Jul 9, 5:45 pm, extrapedestrian <[email protected]> wrote:
>
> > > do you have set OnKeyListener?
> > > like:
> > > alarmsListView.setOnKeyListener(this);
>
> > > On Jul 9, 5:28 pm, Moto <[email protected]> wrote:
>
> > > > I don't see your ListView under the xml file... where is that?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---