i use following code to remove rows out off my tablelayout;
System.out.println(vi); gives me 4 unique rows and thats right but why my loop is not removing all rows at once. I need to click 3 times.
This code is placed in myOnclickHandler;
public void onClick(View v) {
TableLayout container = (TableLayout) v.getParent().getParent();
int childcount = container.getChildCount();
View vi;
for (int i = 0; i < childcount; i++) {
vi = container.getChildAt(i);
container.removeView(vi);
System.out.println(vi);
}
}
try it :
row = (TableRow)findViewById(R.id.row);
table.removeView(row);
use this it keep only header row.
if (table.getRootView() != null) {
int i = 1;
while (table.getChildCount() != 1) {
table.removeViewAt(i);
}
}
Related
I have a list with words. When I'm clicking on button "Next, i want to display the next word on TextView.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.dispalay);
worldList = new ArrayList<>();
worldList.add("cat");//i want to display this word firstly
worldList.add("dog");//it after click
worldList.add("monkey");//after it
worldList.add("bird");//after it
worldList.add("fish");//etc
worldList.add("home");//etc
worldList.add("car");//etc
public void onClickYes(View view) {
int i = 0;
while(i < worldList.size()){
display.setText(worldList.get(i));
i++;
//There is i get an incorrect resolte, because i'm getting last word only.
}
}
}
declare int i=0 as a global variable or outside onClickYes method. In onClickYes method, use if method instead of while. You will get next word
int i = 0;
public void onClickYes(View view) {
if(i < worldList.size()) {
display.setText(worldList.get(i));
i++;
}
}
declare int i = 0; outside onClickYes method, e.g. line above, and exchange while to if
int i = 0;
public void onClickYes(View view) {
if(i < worldList.size()){
display.setText(worldList.get(i));
i++;
//There is i get an incorrect resolte, because i'm getting last word only.
}
}
currently every time when button is clicked you are creating new local variable i=0, so every worldList.get(i) call will return item at position 0 for you. keep i outside - it will preserve its value (when you call i++) to next button press
Try This:
int i = 0;
public void onClickYes(View view) {
if(i>=worldList.size()) return;
display.setText(worldList.get(i));
i++;
}
This could work out.
int i = 0;
public void onClickYes(View view) {
display.setText(worldList.get(i));
if (i < worldList.size() - 1) {
i++;
} else {
i = 0;
}
}
I have receipt and logs model in android app. Receipt hasMany logs.
I made query for group_by logs by sortID and grade.
//recieve RecepitID and query to group logs
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();
This grouping works fine. Problem is next. I need to have output like this (part in red circle):
but I get it like this:
And this is part of code how I done this.
public class LogsRecapitulation extends AppCompatActivity {
private ListView mainListView;
private BaseAdapter listAdapter;
private TextView logsCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recapitulation_listview);
mainListView = (ListView) findViewById(R.id.ListViewItem);
//recieve RecepitID and query to group logs
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();
TextView result = (TextView) findViewById(R.id.LogMassResult);
double sum = 0.0;
for (int i = 0; i < logsList.size(); i++) {
sum += logsList.get(i).getM3();
}
result.setText(String.format("%.2f m3", sum));
for (int i = 0; i < logsList.size(); i++) {
if (logsList.get(i).receipt.priceType.equals("Na panju")) {
TextView stumpPriceKN = (TextView) findViewById(R.id.sumPriceKN);
double sumPricekn = 0.0;
for (int j = 0; j < logsList.size(); j++) {
sumPricekn += logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3();
}
stumpPriceKN.setText(String.format("%.2f KN", sumPricekn));
} else {
TextView roadKN = (TextView) findViewById(R.id.sumPriceKN);
double roadPrKn = 0.0;
for (int j = 0; j < logsList.size(); j++) {
roadPrKn += logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3();
}
roadKN.setText(String.format("%.2f KN", roadPrKn));
}
}
for (int i = 0; i < logsList.size(); i++) {
if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("Na panju")) {
TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
double correcSumKN = 0.0;
for (int j = 0; j < logsList.size(); j++) {
correcSumKN += (logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.stumpPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
}
corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
} else if (logsList.get(i).receipt.priceCorrection > 0 && logsList.get(i).receipt.priceType.equals("Šumska cesta")) {
TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
double correcSumKN = 0.0;
for (int j = 0; j < logsList.size(); j++) {
correcSumKN += (logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) + ((logsList.get(j).price.roadPrice_kn * logsList.get(j).getM3()) * logsList.get(j).receipt.priceCorrection / 100);
}
corecctionPriceKn.setText(String.format("%.2f KN", correcSumKN));
} else {
TextView priceHolder = (TextView) findViewById(R.id.KorekcijaCijene);
TextView corecctionPriceKn = (TextView) findViewById(R.id.correctionPriceKN);
priceHolder.setText("");
corecctionPriceKn.setText("");
}
}
listAdapter = new RecapitulationArrayAdapter(logsList);
mainListView.setAdapter(listAdapter);
//display logs count
logsCount = (TextView) findViewById(R.id.logsCount);
logsCount.setText(String.valueOf(logsList.size()));
}
private class RecapitulationArrayAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Logs> logsList;
public RecapitulationArrayAdapter(List<Logs> logsList) {
inflater = LayoutInflater.from(LogsRecapitulation.this);
this.logsList = logsList;
}
#Override
public int getCount() {
return logsList.size();
}
#Override
public Object getItem(int position) {
return logsList.get(position);
}
#Override
public long getItemId(int position) {
return logsList.get(position).getId();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.logs_recapitulation, parent, false);
}
Logs log = logsList.get(position);
((TextView) convertView.findViewById(R.id.rec_log_sort)).setText(log.sort_id);
((TextView) convertView.findViewById(R.id.rec_log_class)).setText(log.grade);
((TextView) convertView.findViewById(R.id.rec_log_count)).setText(String.valueOf(logsList.size()));
((TextView) convertView.findViewById(R.id.rec_logs_mass)).setText(String.format("%.2f m3", log.getM3()));
if (log.receipt.priceType.equals("Na panju")) {
((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.stumpPrice_kn));
} else {
((TextView) convertView.findViewById(R.id.rec_log_price_default)).setText(String.valueOf(log.price.roadPrice_kn));
}
if (log.receipt.priceType.equals("Na panju")) {
((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.stumpPrice_kn * log.getM3()));
} else {
((TextView) convertView.findViewById(R.id.rec_calculated_price)).setText(String.format("%.2f KN", log.price.roadPrice_kn * log.getM3()));
}
return convertView;
}
}
}
I'm using ActiveAndroid and displaying this in listview.
PROBLEM is with displaying these grouped items. I’m displaying them in BaseAdapter listView and it shows me only one item per group but it needs to show me multiple items (because it has more than 4 items in each group).
Can anyone give me advice what should I do to get output like on first image?
It looks like you are trying to get results similar to the following SQL:
select sort, grade, count(grade), sum(mass), price from logs where receiptid = forwardedId group by sort, grade;
You show the displayed count computed as logList.size(). Instead, if you are going to have the database group the records, you will want to get the count from the database as well.
A couple of things may also help.
1. Verify the SQL query directly against the database to verify you are starting with the intended result set. You can use the sqlite3 client to test assuming you are using an SQLite db.
2. Enable ActiveAndroid logging in ActiveAndroid.initialize
Also you might double check the parameter spelling to make sure you are referencing the intended intent parameter.
Once you verify that the query results are correct, you can focus on whether you are rendering it in the list as you wish.
Finally be wary of doing work on the UI thread.
User 'user650881' is right (I cannot vote since repo < 50).
Additionally to achieve the summary for the total table you may execute the following SQL query:
select /*sort, grade,*/ count(grade), sum(mass), price from logs where receiptid = forwardedId /*group by sort, grade*/;
if you would like to have the items grouped by different fields you may also try widow functions i.e.:
select count(grade) over(partition by fieldName)
and group by is eventually not necessary
In my activity, I am creating a dynamic radiogroup. I have some questions and answers in Sqlite database, retrieving them in activity and then set in RadioGroup. This is working fine. But after that, I want to get all ID of selected radio button by user to store them in database. In general, we do something like this when we have option's ID :
id1=rdgroup1.getCheckedRadioButtonId();
que1=(RadioButton)findViewById(id1);
ans1=que1.getId()+""; // so here I will get radio button's ID.
So my questions is, how will I get selected radio button's ID. Here's my code for dynamically creating radiogroup.
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
c1=db.rawQuery("SELECT * FROM QueTable WHERE AgeGroup='10-20'", null);
c1.moveToFirst();
int i = c1.getCount();
if(i > 0)
{
Log.w("START", "start");
while (i > 0)
{
TextView title = new TextView(this);
questions = c1.getString(1);
title.setText(questions);
title.setTextColor(Color.BLACK);
mLinearLayout.addView(title);
// create radio button
answers=c1.getString(2);
String[] answer = answers.split(",");
rb = new RadioButton[5];
rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
int k = answer.length;
for (int j = 0; j < k; j++)
{
rb[j] = new RadioButton(this);
rg.addView(rb[j]);
rb[j].setText(answer[j]);
}
mLinearLayout.addView(rg);
c1.moveToNext();
i--;
}
}
The place where you create RadioButton set and Id to it
for (int j = 0; j < k; j++)
{
RadioButton rb = new RadioButton(this);
rb.setId(Somenumber + j)
rb[j] = rb;
rg.addView(rb[j]);
rb[j].setText(answer[j]);
}
Like this you can setId or also if you want you can add tag to process.
rb.setTag(SomeObject)
You need to set an ID for your programmatically created RadioButton in order to find it using findViewById as you show in your first code.
To set the ID for your RadioButton, you can use View.setId(). The documentation says:
Sets the identifier for this view. The identifier does not have to be
unique in this view's hierarchy. The identifier should be a positive
number.
The identifier does not have to be unique, but if it's not unique, findViewById will return the first occurence, so you can use the static method View.generateViewId added on API level 17.
If you are using lower APIs, you may want to write your own function to find a suitable (unused) ID. You can take a look at Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Ok..So finally I got the solution. I am going to share complete code here that will create dynamically radiogroup and radiobuttons from database. Here is code below :
private static final int RB_ID = 100;
int k=0,len=0,r=0,p = 0,q=0, len = 0;
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
c1.moveToFirst();
int i = c1.getCount();
rg = new RadioGroup[i];
rb = new RadioButton[i*5];
if (i > 0) {
Log.w("START", "start");
while (i > 0)
{
TextView title = new TextView(this);
questions = c1.getString(1);// retrive from database
title.setText(questions);
title.setTextColor(Color.BLACK);
title.setTypeface(null, Typeface.BOLD);
title.setPadding(0, 0, 0, 10);
mLinearLayout.addView(title);
answers = c1.getString(2);// retrive from database
String[] answer = answers.split(",");// will create options for radio button.
rg[p] = new RadioGroup(this);
rg[p].setOrientation(RadioGroup.VERTICAL);
len=len+answer.length;
for (int j = 0; j < answer.length; j++)
{
rb[q] = new RadioButton(this);
rg[p].addView(rb[q]);
rb[q].setId(k + RB_ID);
rb[q].setText(answer[j]);
k++;
q++;
}
mLinearLayout.addView(rg[p]);
c1.moveToNext();
i--;
p++;
}
//Submit button click
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (rg[0].getCheckedRadioButtonId() == -1)
{
Log.e("Nothing selected", "Nothing selected");
}
else
{
for (r = 0; r < len; r++) {
if (rb[r].isChecked()) {
RadioButton id = (RadioButton) findViewById(rb[r].getId());
String radioText = id.getText().toString();
c3.moveToFirst();
Log.e("RadioString", radioText);
} else {
Log.e("RadioString", "nothing");
}
}
}
}
});
Stackflowers!
Little question.. I load text into a textView who is in a listView. I load the text out of an url. Everything works fine.. But.. Sometimes in the text there is an new url with an image. I can filter the text, and show the url only in a Log or something, no problem. But now.. I have many textView in my listView.. But now, when I detect a new image url, I want to create or show the image in an imageView. (To load the image is not a problem to..) But the problem is, how to hide all the imageViews.. Untill I (or my code..) detects an image url (.jpg)?
But sometimes there is one image.. on the 1st position of my listView.. Sometimes there are maybe 2 or 3 images.. On position 1, 5, 7.. I don't know that of beforehand..
This is my View Class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item_view_second = convertView;
post_second current_post = my_post_second.get(position);
Typeface TF = Typeface.createFromAsset(getAssets(), global.fontType_title);
Typeface tf = Typeface.createFromAsset(getAssets(), global.fontType_text);
if (item_view_second == null) {
item_view_second = getLayoutInflater().inflate(R.layout.item_view_second, parent, false);
}
// Title:
TextView title_text = (TextView) item_view_second.findViewById(R.id.item_title);
if(position == 0) {
title_text.setVisibility(View.VISIBLE);
title_text.setText(Html.fromHtml(current_post.get_title()));
title_text.setTypeface(TF);
} else {
title_text.setVisibility(View.GONE);
}
// Blog:
TextView blog_text = (TextView) item_view_second.findViewById(R.id.item_blog);
blog_text.setText(Html.fromHtml(current_post.get_blog()));
Linkify.addLinks(blog_text, Linkify.WEB_URLS);
blog_text.setTypeface(tf);
// Image:
img_loader = new image_loader(second_activity.this);
img_view = (ImageView) item_view_second.findViewById(R.id.item_image);
// img_view.setTag(current_post.get_image_id());
img_loader.DisplayImage(current_post.get_image(), img_view);
// Log.e("img_view.ID-Tag", "Found:" + img_view.getTag());
return item_view_second;
}
}
This is the part where I detect the url with:
for (int j = 0; j < counter_blog; j++) {
if (s != null) {
s = dis.readLine();
s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));
if(s.contains("<p><a href=")) {
image[j] = s.substring(s.indexOf("<a href=") + 9, s.indexOf(".jpg")+ 4);
blog[j] = image[j];
} else {
blog[j] = s;
}
}
counter_blog_stop = j + 1;
}
And over here I use the counter_blog_stop :)
private void populate_post_list() {
for (int i = 0; i < 1; i++) {
for (int j = 0; j < counter_blog_stop; j++) {
my_post_second.add(new post_second(title[i], image[j], blog[j], image_id[j]));
}
}
}
private void populate_list_view() {
ArrayAdapter<post_second> adapter = new my_list_adapter_second();
global.list = (ListView) findViewById(R.id.post_second_list_view);
global.list.setAdapter(adapter);
}
I hope my question was clear enough.. When it is not.. Please make a comment, and I'll answer is so soon as possible!
P.S. When I load my activity now, I see the Title on the first position in my listview.. below, an imageview (with a standard image?!) below the Blog, below again a standard image, blog, the whole listview.. Untill there are no Blogs anymore..
Edit:
If there are urls detected, they shown perfect in my listView.. But every time there is no image url.. He post an standard imageView.. And I use only a textView called id:item_title and a imagrView called item_image in my listView.
I'm not sure I totally understand, but I think I can help:
Adapters have a built in way to deal with multiple types of views. Since it sounds like you're dealing with two different layouts (text only or text with image), add this to your adapter:
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
if (hasImageUrl(position)){
return 1;
} else {
return 0;
}
}
The hasImageUrl function would check to see if that particular item has an image url. getView() will use this to return the correct type of view to you, so you could write something like this:
#Override
public View getView(int position, View convertView, ViewGroup c) {
switch (getItemViewType(position)){
case 0:
//do your text only stuff
break;
case 1:
//do your image stuff
break;
}
...
Hope that helps.
how to hide all the imageViews.. Untill I (or my code..) detects an
image url (.jpg)?
By default in the layout, set the visibility of ImageView as GONE
android:visibility="gone"
So, the ImageView will not be visible on the ListView items
When you detect the url for the Image, change the visibility as visible in the Adapter
img_view.setVisibility(View.VISIBLE);
This will make sure that only the items that have the url will show the ImageView
Made an array on top:
Integer img_detected[] = new Integer[counter_blog];
I add the two bold lines to my loop (shown below):
for (int j = 0; j < counter_blog; j++) {
if (s != null) {
s = dis.readLine();
s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));
if(s.contains("<p><a href=")) {
image[j] = s.substring(s.indexOf("<a href=") + 9, s.indexOf(".jpg") + 4);
Log.e("TattlerMX Found url", "Found url:" + image[j]);
blog[j] = image[j];
**img_detected[j] = 1;**
} else {
blog[j] = s;
**img_detected[j] = 0;**
Log.e("TattlerMX Found blog", "Found blog:" + blog[j]);
}
}
counter_blog_stop = j + 1;
}
change my view class:
// Image:
img_view = (ImageView) item_view_second.findViewById(R.id.item_image);
if(img_detected[position] == 1) {
img_view.setVisibility(View.VISIBLE);
img_loader = new image_loader(second_activity.this);
img_loader.DisplayImage(current_post.get_image(), img_view);
} else {
img_view.setVisibility(View.GONE);
}
I used my class int position to read my array.
I inflate an EditText and I setId(i++) | static int i = 0;
and this is the code :
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addOne = layoutInflater.inflate(R.layout.row, null);
final EditText textOut = (EditText)addOne.findViewById(R.id.textout);
Button buttonRemove = (Button)addOne.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addOne.getParent()).removeView(addOne);
}
});
container.addView(addOne);
textOut.setId(i++); }});
so, if I add an EditText, the Id will be 0 and if I add another EditText, the Id will be 1 .. etc
so, the user can select how many EditText he want, right ?
if the user add 3 EditText for example
How can my code knows ?
for example, if I want sum all the values of EditText, and the user add 3 EditTixt
I will do this
( EditText one = (EditText)findViewById(0) ) + ( EditText dodo = (EditText)findViewById(1) ) + ( EditText dodo = (EditText)findViewById(2) )
But, if I don't know how many EditTixt the user will add !
what can I do ?
thank you.
int count = 0;
int childcount = container.getChildCount();
for (int i=0; i < childcount; i++){
View v = container.getChildAt(i);
if(view.getTag.equals("edittext"){
count ++;
}
}
and change this line in your code
final View addOne = layoutInflater.inflate(R.layout.row, null);
addOne.setTag("edittext");
count return you total edittext added by user
take look on this code it will count edit text for you in your layout container
first you need to making changed in your code like this and it worked with me fine :
final ViewGroup addOne = (ViewGroup) layoutInflater.inflate(R.layout.test, null);
int count = 0 ;
for (int i = 0; i < addOne .getChildCount(); i++) {
if(addOne .getChildAt(i).getClass().getSimpleName().equals("EditText"))
count ++ ;
}