Accessing content in a TableLayout
I am populating a TableLayout (i.e. rows and columns) with data (strings).
When I click on a cell, I would like the data stored in this cell to be display开发者_开发百科ed in my console.
How can I do that? Is there any other way than to go over IDs?
As stated in http://developer.android.com/resources/tutorials/views/hello-tablelayout.html , there is no Column or TD (Table Data) or Cell equivalent in android. Each element is treated as an individual cell unless otherwise noted.
Having this in mine, and given the fact that you don't specify what kind of View you're using inside your rows, may I guess it's a Button which you can of course click with something like this:
Button b = (Button)findViewById(R.id.oneOfMyButtons); // You could create this "on the go"
b.setOnClickListener(new View.OnClickListener{
public void onClick(View v){
System.out.println(v.getText());
}
}
Hope this helps.
TableLayout
extends LinearLayout
, which do not have an OnItemClickListener
method. You will need to implement OnClickListener
in the child View
s instead.
What you can do instead is to use a GridView
, that implements AdapterView
and thus you can use OnItemClickListener
http://developer.android.com/resources/tutorials/views/hello-gridview.html
http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29
abstract void onItemClick(AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
精彩评论