I have a simple PaneTemplate I am setting up like this:
Row row1 = new Row.Builder().setTitle("Do Thing 1").build();
Row row2 = new Row.Builder().setTitle("Do Thing 2").build();
Row row3 = new Row.Builder().setTitle("Do Thing 3").build();
Row row4 = new Row.Builder().setTitle("Do Thing 4").build();
return new PaneTemplate.Builder(new Pane.Builder().addRow(row1).addRow(row2).addRow(row3).addRow(row4).build()).setTitle("AA Hello!!").build();
and it all works fine and looks like this:
but if I add setOnClickListener to one of the rows:
Row row1 = new Row.Builder().setTitle("Do Thing 1").setOnClickListener(this::onClick).build();
I get an exception:
"a click listener is not allowed on the row"
I get an error:
I have not read about this restriction in any of the AA documentation. I tried making the onClick function anything number of things, including a blank function, and it makes no difference. Any idea what is going on here? How can I get around it? I want the press of this item to do something.
It's restricted on PaneTemplate.
Using ListTemplate for such simple list is better decision.
Related
I am trying to interact with dynamically generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tried. I've found an id property of a button in XML which led me to idea that I can make a text key to refer to a programmatically generated button. But when I've tried to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tried to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index).
Alternatively you can assign ids saved in xml like this.
Basically, I've been trying to place a bunch of EditText and TextViews programmatically, all within a RelativeLayout (I have to do it programmatically because the amount of stuff is variable depending on how many "employees" the user has entered). Now, I only need ten pieces of data per "employee", so I decided to keep track of the data using id's in base 10 (ie Employee 1 gets id 0-9, Employee 2 gets id 10-19, etc.). However, every time I use LayoutParams.addRule(int,int) function and manually input my own id, it fails to pick it up. If I use the addRule(int,int) function using "R," it works. The only reason I can come up with that would explain addRule's failure to respond to the manually inputed id values is if my math (for the id-values) is wrong, but if you look at my code, the math is pretty self-explanatory. Please tell me what I'm doing wrong because this is maddening.
Here's what I have so far:
for(int i=0;i<u.getTemp().size();i++){
int index=10*i;
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
if(i==0)
layoutParams.addRule(RelativeLayout.BELOW,R.id.start_date);
else
layoutParams.addRule(RelativeLayout.BELOW,index-1);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView empName=new TextView(rl.getContext());
empName.setTextSize(26);
empName.setText(u.getTemp().get(i).getName());
empName.setId(index++);
empName.setLayoutParams(layoutParams);
rl.addView(empName);
TextView empNum=new TextView(rl.getContext());
empNum.setText("Employee Number: " + u.getTemp().get(i).getNum());
empNum.setId(index++);
RelativeLayout.LayoutParams empNumLayout=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
empNumLayout.addRule(RelativeLayout.BELOW,empNum.getId()-1);
empNumLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
empNum.setLayoutParams(empNumLayout);
rl.addView(empNum);
EditText regHours=new EditText(rl.getContext());
regHours.setHint("Regular Hours");
regHours.setId(index++);
RelativeLayout.LayoutParams regHoursLayout=new RelativeLayout.LayoutParams(300,RelativeLayout.LayoutParams.WRAP_CONTENT);
regHoursLayout.addRule(RelativeLayout.BELOW,regHours.getId()-1);
regHoursLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
regHours.setLayoutParams(regHoursLayout);
rl.addView(regHours);
}
*Notes: rl is the relativeLayout I placed in the xml file.
I recently lost the reputation to comment :) so I am posting it as an answer. You can try using Linear Layout instead of Relative Layout. If you require any further assistance let me know. I'll help you out. :)
The error was that ids have to be positive. The first TextView had an id of 0 which explains why the addRule didn't respond.
I would like to display a gray star image if column "status"==0 and display a golden star if column "status"==1. My code can display the record to listview, but without checking the "status" column first:
public void loadPertanyaan(){
Cursor soal = DBAdapter.fetchSemuaSoal();
String[] from = new String[]{DBAdapter.KEY_SOAL_PERTANYAAN};
int[] to = new int[]{R.id.txt_PilihSoal_Pertanyaan};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row, soal, from, to);
setListAdapter(dataAdapter);
}
Then I add code to check status column using moveToNext() after setListAdapter(dataAdapter). But the application always force stopped, code to check "answered" column below -
ImageView img = (ImageView) findViewById(R.id.imgBintang);
while(soal.moveToNext()){
if(soal.getInt(soal.getColumnIndex("status"))==1)
img.setImageResource(R.drawable.bintang_mas);
else
img.setImageResource(R.drawable.bintang_abu);
}
How can I check "status" column before displaying to ListView. Thanks in advance.
Agi, You need a good tutorial like link Populating a ListView with a CursorAdapter . Look at code bindView similar to getView method, mentioned in a previous answer. getView is less intensive so it may be good enough for you.
Look at LinearLayout resource XML. In it, there are 2 TextView elements. Instead, I think you need more columns in your ListView, like TextView, popular Checkbox, and an image for the star (bintang).
The code is different than your thoughts in design. It gets and displays data in virtual mode, and you can check the data by the position or Cursor parameter.
Good luck and tell me how it goes for you...
I have a for loop that each time prints out a row with a TextView within. I want this TextView to be clickable, and when clicked it is supposed to start another activity and send an ID with the Intent. But it's this that I can't manage to work. All links shows up nicely in a table and the textviews are clickable, but all links sends me to the next activity with the same ID, the ID of the last link.
Here is a simplified version of the content of the for loop:
TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
title.setText(titleLine);
title.setClickable(true);
title.setId(rows);
rows++;
title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(a.this, b.class);
myIntent.putExtra("id", idLine);
a.this.startActivity(myIntent);
}
});
contRight.addView(title);
I suppose the problem is that all textViews looks the same, so the last setOnClickListener works on all links. However, I have added an ID to each title, with this code: title.setId(rows);, so they are all supposed to be unique.
Anybody who can give some help? :) Thanks!
Shouldn't idLine be v.getId()? It's good for this value to be as "fresh" as possible so that you aren't reading an old value: in this case, the value of the last row which had probably been lurking in idLine. Hence, getting the value from the view directly is the safest method.
Also, the "tag" might be a better place to store information than the id. You can access the tag, and indeed store any number of arguments as tags on any view, without changing the id:
title.setTag(rows);
and then later:
intent.putExtra((Integer) v.getTag());
To store multiple objects (with keys) you can use:
title.setTag("rowid", rows);
and then retrieve it using:
intent.putExtra((Integer) v.getTag("rowid"));
and then you could add additional keys if you requirements changed.
In any case, the intention of the tag is to store information for later retrieval, whereas the id is meant to serve as an identifier for finding views in a hierarchy.
How will i go about saving the state of a listview item during scrolling, so that the recycler does not use it when displaying the next row being. i basically have a listview with 4 textviews and one of the textview is displayed based on a condition, derived from a database. For clarity sake, i will call that dependant textview "A".
my problem is that, simplest case: if "A" is being displayed for only the first item or any other row in the list and its not displayed anywhere else, when scrolling, "A" is displayed on other rows that should not have it. i understand the concept of listview reusing rows, but i can't figure out how to save it on an item, so it doesn't get used in another row. Here is a simplified code with just 2 textview:
holder.viewItemName = (TextView)view.findViewById(R.id.nameId);
holder.viewdescriptionStatus = (TextView)view.findViewById(R.id.viewdescriptionId);
int namecolumn = c.getColumnIndex(DBAdapter.NAME);
String name = c.getString(namecolumn);
holder.viewItemName.setText(name);
int description = c.getColumnIndex(DBAdapter.description);
String descriptionstatus = c.getString(description);
/*problem am having here*/
if(description != null){
holder.viewdescriptionStatus.setText("description available");
holder.viewdescriptionStatus.setTextColor(Color.BLUE);
}
as you can see, am using a viewHolder, but i have come to realise that viewHolder doesn't hold the logic as well. i don't know how to save the state of the holder.viewdescriptionStatus based on that condition. Most of the examples i have seen are based on checkboxes. Please anyone with ideas?.. it will be really appreciated.
P.S : i am using bindView() and newView() since i am using an SQLdatabase and SimpleCursorAdapter. i have the same issue with clicking, but i want to solve the scrolling part first. Thank you for your time.
Try this
holder.viewdescriptionStatus.setText(description != null ? "description available" : "");