cell click event on text (cell's text) of dynamically created table layout in android - android-activity

where allValues is array list and i am creating 5 column table.So help me to implement text click event on each text of each cell.I searched and found click event of table row but by this way i am not able to implement cell click event in table layout.
TableLayout tl = (TableLayout) findViewById(R.id.tlparent);
for (int j = 0, k = 0; j < allValues.size() / 5; j++)
{
final TableRow tableRow = new TableRow(this);
for (int y = 1; y <= 5; y++)
{
TextView tv = new TextView(this);
tv.setText(allValues.get(k));
tableRow.addView(tv);
k++;
}
tl.removeView(tableRow);
tl.addView(TABROW);
}

Simply add an onClickListener to your TextView:
TextView tv = new TextView(this);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
String text = tv.getText().toString();
// process your text
}
});
If you want to pass your table "coordinates" (j,k,y, etc.) to your calling function, make additional final variables before setting the OnClickListener and use these inside the onClick method.

Related

Create a list prefab buttons in the script in Unity

I'm currently working on a school project and have encountered the following issue.
I'm trying to create a gameboard made out of a prefab (the prefab is a button). For that I have the following code:
public GameObject tilePrefab;
public Transform newParent;
public int xAxis = 0;
public int yAxis = 0;
private int boardWidth = 9;
public int boardHeight = 5;
private void GenerateBoard()
{
int buttonNumber = 0;
for (int i = 0; i < boardWidth; i++)
{
for (int j = 0; j < boardHeight; j++)
{
GameObject button = Instantiate(tilePrefab, new Vector3(xAxis, yAxis, 0), Quaternion.identity);
button.transform.SetParent(newParent);
boardButtons[buttonNumber] = button; //.GetComponentInChildren<Button>()
yAxis += 300;
}
yAxis = 0;
xAxis += 300;
}
}
tilePrefab is the Button prefab, newParent is the canvas. This works for the most part, but when I try to add the created buttons to a list or an button[] array, there is a conflict. Because even though my tilePrefab is a ui button obect, generating it like this means that "button" is a GameObject.
How can i fix that so that i can create a list or an button[] array and successfully store the newly created button in that list/array? I tried "public BoardPiece tilePrefab" or "public Button tilePrefab", but then the Instantiate doesn't work. (BoardPiece is the name of the Prefab)
Thanks for any help that you can give.
Because even though my tilePrefab is a ui button obect
The tilePrefab is a GameObject that has a Button Component attached, they are two different things that seem to be causing confusion. I'd suggest renaming GameObject Button to GOButton to make it clear that it is a GameOject and not a Button.
If you want to store a list of Button, you have to make sure you are adding a Button, in order to do this, I'd suggest you create a list of Buttons:
List<Button> buttonList = new List<Button>();
Then every time you instantiate the GOButton, you then add the Button Component to your list:
buttonList.Add(GOButton.GetComponent<Button>());
This way you then have a list of Buttons that you can loop and handle how you please:
foreach (Button button in buttonList)
{
//change button state here
}

Getting value from dynamic table in Android

This is my code for creating a table and appending data dynamically:
table = (TableLayout)findViewById(R.id.table);
inflater = getLayoutInflater();
for(int i = 0; i < 5; i++) {
row = (TableRow)inflater.inflate(R.layout.row,table, false);
TextView text = (TextView)row.findViewById(R.id.text1);
text.setText("sss:" + i);
text1 = (EditText)row.findViewById(R.id.text2);
text2 = (TextView)row.findViewById(R.id.text3);
text2.setText("");
text1.addTextChangedListener(new MyWatcher(text2));
table.addView(row);
}
This code is working fine, but my problem occurs when I click the button. I want to get the values from each row which is to be only filled.
NxtBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
Please help me.

Is there any way to find a string's position and click on it using robotium?

I have the following list appearing on UI of an android application which tells about the date and the temperature on that date.
temp degree
11/01 --.-- c
11/02 21.7 c
11/03 22.5 c
Here I want to find the position of string "--.--" and then click on it, so that I can update the list. Is there any way to find the position of string? I know that solo.searchText() will tell whether the string is present or not, but what about the position?
// i think it will help you
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
// TODO Auto-generated method stub
if(yourliststring.trim().equals("--.--"))
{
//your condition
}
else
{
//your condition
}
}
});
I got a simple way to find and click on the string
if(solo.searchText("--.--")){
solo.clickLongOnText("--.--");
}
All I need is to click on the string "--.--".
I'm not sure what position you need, however if you need position on screen you can simple do:
TextView textView = solo.getText("--.--"); // or solo.getText(Pattern.quote("--.--")); if needed
int[] xy = new int[2];
textView.getLocationOnScreen(xy);
final int viewWidth = textView.getWidth();
final int viewHeight = textView.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
final float y = xy[1] + (viewHeight / 2.0f);
this way you have central point of view (x, y).
If you need position of text in list, you can do:
private int getPosition(ListView listView, String text) {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
Object o = listView.getAdapter.getItemAtPosition(i);
if (o instanceof TextView) {
if (((TextView)o).getText().toString().equals(text)) {
return i;
}
}
}
return -1; // not found
}
you can call it for instance this way (set proper parameters):
int position = getPosition((ListView) solo.getView(ListView.class, 0), "--.--");
Everything written without testing and even compiling, however main idea should be fine.

How to clear the Overlay using GWT

I have a class which I used to overlay rectangles on maps. But I am not able to figure out how to remove the previous overlay's to draw new rectangles if a new set of results are provided to my displayOnMap method.
To provide more insight on the draw method. It takes in PlotSetOutput as an argument which contains centers and each center contains a set of lat/long co-ordinates. Hence the logic for looping over it and creating lat/long bounds and assigning it to rectangle objects.
public class displayOnMap extends Composite {
private final VerticalPanel pWidget;
private MapWidget mapWidget;
private static Rectangle rectangle;
private RectangleOptions rectOpts;
private static final LatLng USCENTER = LatLng.newInstance(33.68,-116.17);
public displayOnMap(PlotSetOutput result) {
pWidget = new VerticalPanel();
initWidget(pWidget);
draw(result);
}
private void draw(PlotSetOutput result) {
MapOptions mapOpts = MapOptions.newInstance();
mapOpts.setZoom(4);
mapOpts.setCenter(USCENTER);
mapOpts.setMapTypeId(MapTypeId.TERRAIN);
mapWidget = new MapWidget(mapOpts);
pWidget.add(mapWidget);
mapWidget.setSize("800px", "800px");
ArrayList<Centers> listOfCenters = new ArrayList<Centers>();
List<ResultClusterPlots> finalCluster = result.getFinalcluster();
int totalNumberOfClusters = result.getTotalNumberOfClusters();
for (int i = 0; i < totalNumberOfClusters; i++) {
listOfCenters.add(i, new Centers());
}
for (int j = 0; j < finalCluster.size(); j++) {
Centers p = listOfCenters.get(finalCluster.get(j).getClusterID()-1);
LatLng ne = LatLng.newInstance(finalCluster.get(j).getLatitude()
.get(0), finalCluster.get(j).getLongitude().get(0));
LatLng sw = LatLng.newInstance(finalCluster.get(j).getLatitude()
.get(1), finalCluster.get(j).getLongitude().get(1));
p.setLatLongArr(LatLngBounds.newInstance(ne,sw));
}
for (int k = 0; k < listOfCenters.size(); k++) {
ArrayList<LatLngBounds> ltlgBound = listOfCenters.get(k).getLatLongArr();
String color = getRandomColor();
for (int l = 0; l < ltlgBound.size(); l++) {
rectOpts = RectangleOptions.newInstance();
rectOpts.setStrokeColor("#FF0000");
rectOpts.setStrokeOpacity(0.3);
rectOpts.setStrokeWeight(2);
rectOpts.setFillColor(color);
rectOpts.setFillOpacity(0.35);
rectOpts.setMap(mapWidget);
rectOpts.setBounds(ltlgBound.get(l));
rectangle = Rectangle.newInstance(rectOpts);
rectangle.setMap(mapWidget);
}
}
}
}
Output when the method (displayOnMap) is invoked for the first time. Everything works fine.
Output when the displayOnMap method is called with a second query.
I tried to do rectangle.setMap(null); pWidget.removeFromParent(); but I kept getting the same result.
I have had the same problem, the only way I could hide / delete / show the overlays was through the OverlayCompleteMapEvent.
As I understand it isn't possible to get a hook to an overlay before it is completed, once it is completed the only way to get a hook on it is handling "OverlayCompleteMapEvent" event. I used a list to store the Overlays and then I could hide/show/delete them.

Inserting a Button to JFace Table

How can I insert a SWT Button control into JFace TableViewer ?
The answer given is nice a nice way to implement your own buttons with custom drawings, in or outside the a table. However, you can put SWT controls in JFace Tables.
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/PlacearbitrarycontrolsinaSWTtable.htm
The solution for building a table with columns containing comboboxes, text fields, and buttons provided by the link is:
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(100);
}
for (int i = 0; i < 12; i++) {
new TableItem(table, SWT.NONE);
}
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++) {
TableEditor editor = new TableEditor(table);
CCombo combo = new CCombo(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(combo, items[i], 0);
editor = new TableEditor(table);
Text text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
editor.setEditor(text, items[i], 1);
editor = new TableEditor(table);
Button button = new Button(table, SWT.CHECK);
button.pack();
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor(button, items[i], 2);
}
You can't. More generally, you can't insert any widgets in tables and trees in SWT, because not all platforms support it. What you can do instead is
Take two screenshots of the button in normal and clicked states;
Put the normal screenshot in table as an image;
Handle clicks on the TableItem.
Here is an example for checkboxes: http://tom-eclipse-dev.blogspot.com/2007/01/tableviewers-and-nativelooking.html