Android Accessing Elements from a Custom ListView
The title for this question might be poor, but but it was hard to nail down a title. So here is the question.
I have an application that opens a database and creates a custom ListView based on the contents. So there are a few files involved in this process:
Main.java - opens the database and stores the List<MyClass> of contents
main.xml - main activity layout with the ListView
MyAdapter.java - extends BaseAdapter and calls MyAdapterView based on the context
MyAdapaterView.java - inflates the View from MyAdapater based on row.xml
row.xml - layout of each custom row of the ListView
This works fine. I am new to Android, but structurally this seems to be how everyone recommends building custom ListViews.
How do I retrieve data from the ListView? For instance, part of the row is a checkbox. If the user presses the checkbox to activate/deactivate the particular row, how do I notify the main application about that?
Thanks,
EDIT:
Main.java
public class MyApplication extends Activity
implements OnItemClickListener, OnItemLongClickListener
{
private List<MyClass> objects;
private ListView lvObjects;
private MyAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
objects 开发者_运维技巧= new ArrayList<MyClass>(); // setup the list
lvObjects = (ListView)findViewById(R.id.lvObjectList);
lvObjects.setOnItemClickListener(this);
lvObjects.setOnItemLongClickListener(this);
loadDatabase(DATABASE);
myAdapter = new MyAdapter(this, objects);
lvObjects.setAdapter(myAdapter);
}
...
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// This is executed when an item in the ListView is short pressed
}
public void onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
// This is executed when an item in the ListView is long pressed
registerForContextMenu(lvObjects);
v.showContextMenu();
}
MyAdapter.java
public class MyAdapter extends BaseAdapter
{
private Context context;
private List<MyClass> list;
public RuleAdapter(Context context, List<MyClass> list)
{
this.context = context;
this.list = list;
}
...
public View getView(int position, View view, ViewGroup viewGroup)
{
MyClass entry = list.get(position);
return new MyAdapterView(context,entry);
}
}
MyAdapterView.java
public class MyAdapterView extends LinearLayout
{
public MyAdapterView(Context context, MyClass entry)
{
super(context);
this.setOrientation(VERTICAL);
this.setTag(entry);
View v = inflate(context, R.layout.row, null);
// Set fields based on entry object
// When this box is checked or unchecked, a message needs to go
// back to Main.java so the database can be updated
CheckBox cbActive = (CheckBox)v.findViewById(R.id.cbActive);
addView(v);
}
}
How do I retrieve data from the ListView?
If you want to know which item in the ListView
was clicked, you use an OnItemClickListener
and Listview.setOnItemClickListener
. There's an example of this in the Hello ListView tutorial.
Once you know which item is selected, you then know the position of that item in the overall Adapter
, and you have the View
. You can use the View to get sub-elements if needed, generally though the position is enough to let you know what part of your "model" was clicked. In your example it would tell you exactly which item in your List<MyClass>
was clicked.
If you want to distinguish between the list item being clicked, and a focusable view in the list item (like a checkbox), then that's a little more involved. The checkbox, by default, will hikack the focus from the list item. You can toggle this though and make it false, depending on what you're trying to do. See this question for more info.
If you need to distinguish between the ListView item click and the CheckBox click, there are several ways. One way is to just maintain the state of whether or not an item is clicked inside the adapter.
For example:
MovieAdapter
MyMovies (ListView)
Then you just use the normal "onListItemClick" and the adapter to tell what is clicked and what isn't. (This is just one way to do this, there are several.)
So you have a custom cursoradapter that creates the views? When you create the view in getView()
, just set up a listener on that view to listen for whatever you want. Probably onClick
精彩评论