So i have a listview and adapter set up. When i add an item trough adapter.add() in the oncreate() method everything is fine. But when i add it trough a listener it creates this weird grey space as if the items are underneath the other items:
This is the code that adds the items:
ArrayList<Task> newTasks = new ArrayList<>(Arrays.asList(parser.ParseTasksJson(result)));
for (int i =0; i < 3;i++)
{
Task task = new Task(i, "Item" + i, 0);
tasks.add(task);
}
for(Task t : newTasks)
{
adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));
}
adapter.add(new Task(99,"Task after forloop", 0));
adapter.notifyDataSetChanged();`enter code here`
And this is my layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar_home"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview">
</ListView>
</LinearLayout>
EDIT: I was not clear enough. The real problem is in the fact that the items from the parser dont get shown in the listview although they are added to the adapter. SEE updated code.
Your code is working properly, however the problem is in this line:
adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));
Make sure that the values are correct, to me it seems that your data does not contain a valid title, and your layout items have a height of wrap_content. That being said, the items are added, but not high enough since they don't have any content.
That's why you see that weird grey space, they are actually several items, but not big enough to see.
Related
I use autoComplete in my project.
Here is the usage of autoComplete:
In Code:
val arrayAdapter = context?.let {
ArrayAdapter<String>(
it, // Context
android.R.layout.simple_list_item_1,
autocompleteOptions // Array
)
}
autoCompleteTextView.setAdapter(arrayAdapter)
// Set an item click listener for auto complete text view
autoCompleteTextView.onItemClickListener =
AdapterView.OnItemClickListener { parent, _, position, _ ->
//some logic
}
In XML:
<AutoCompleteTextView
android:id="#+id/actvSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true""
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:completionThreshold="0"
android:dropDownHeight="200dp"
android:layout_gravity="center_vertical" />
My question is there any way to adjust the size of the dropdown list according to the current options displayed in the dropdown?
You would need to set it manually in code, based on the number of entries in your arrayAdapter:
autoCompleteTextView.setDropDownHeight(arrayAdapter.getCount() * rowHeight)
where you either calculate each row height, or just have it pre-set as a static height
In my Android App that where i use Java in Android Studio I want to use a loop to get the inserted Texts from my EditText fields
Could you please help me?
for (int i=0; i==player; i++ ){
myArray[i] = "player"+i.getText().toString();
}
I have EditTextfields which can be declared as player0 until player15.
And Player is my total amount of EditTextfields.
I want to write all the inputs into an array but i get always an error for the part"player"+i" is there an other solution how to handle that?
Simply put, Java does not work that way. You can't generate a variable name from a string.
The easiest way to get what you want is an array.
EditText[] players = {
player0, player1, player2, player3, player4,
player5, player6, player7, player8, player9,
player10, player11, player12, player13, player14, player15
};
List<String> texts = new ArrayList<>(players.length);
for (EditText player : players) {
texts.add(players[i].getText().toString());
}
if you have all the edit text fields in the same layout (Linear layout....)
you can simply access every EditText using a loop by accessing them as childs of the layout
suppose in the xml file you have a linear layout
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
notice that we gave the layout an id (linearLayout)
now from your java code you can simply populate the array by using the following loop
LinearLayout linear= (LinearLayout)findViewById(R.id.linearLayout);
for(int i=0;i<linear.getChildCount();i++)
{
EditText field=linear.getChildAt(i);
array[i]=field.getText().toString();
}
unlike the method you are trying to use this method is more dynamic where you dont have to provide ids for all the editTexts just a single id for your main layout and you would not have to declare a static array with all the ids in your java code
I've tried searching other questions, and most of them are to do with having the listview inside a scrollview, but I have no scrollview so it wasn't of much help. I'm sure it's something small I'm missing but I can't seem to pinpoint it. The below is supposed to print out the 3 animalNames to the listview, but is only printing the first.
JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
ArrayList<String> animalsArray = new ArrayList<String>();
if(mainNode != null) //puts the values into an array
{
for(int i=0;i<mainNode.length();i++)
{
JSONObject eachObject = mainNode.getJSONObject(i);
animalsArray.add(eachObject.getString("animalName"));
}
//prints array to check
for(String stuff : animalsArray)
{
Log.i("name", stuff);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, animalsArray);
animalsList = (ListView) findViewById(R.id.animalList);
animalsList.setAdapter(arrayAdapter);
}
This prints out the 3 values within my JSON to logcat, so I'm sure it's getting the values, I'm just unsure as to why it's only printing the first position into the list.
In case it's of any use, my current layout is below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/animalList"/>
Any ideas as to why it's only printing the first would be greatly appreciated.
Thanks.
It's because the height of your linear layout correspond to the height of one listView item :
android:layout_height="?android:attr/listPreferredItemHeight"
Set the height with other value (like match_parent if you want full screen height)
I am working on an android application.
In an XML Layout I need to do the following:
I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).
The list view should take as much space as it has items. And the rest is taken by the receiptSection.
So for example if I have 2 items in the listViewProducts:
The list view is as big as the 2 items and the rest is taken by the receiptView.
If I add another item, the list view now take more space and push the receiptView lower:
However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:
As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.
What I have tried
I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content".
This will make it grow with its content and its at the top of the view
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.
For this view I have android:layout_alignParentBottom="true" and android:layout_below="#id/listViewProducts" that will make it go under the list view and align with the bottom of the view.
I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/listViewProducts"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.
The problem
however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.
Is there a way I can make the listView stop growing when the receiptView reaches 50dp?
Thanks a lot for any help.
Unfortunately I think your best bet is to do this by making a custom view that extends ListView and overrides onMeasure.
public class CustomView extends ListView {
#Override
int maxHeight = 0;
View parentView = (RelativeLayout) (or whatever) getParent();
if (parentView != null){
maxHeight = parentView.getHeight() - 50;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Try swapping the 'layout_below'.
What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/receiptSection"
android:layout_alignParentTop="true" >
</ListView>
And your relativelayout:
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
Problem
I've been trying to place a couple of TextViews underneath eachother, but I can't seem to get it working. They always overlap eachother in stead of getting placed beneath eachother.
What have I tried
I've tried messing with gravity (which doesn't work with a RelativeLayout), and all sorts of layout parameters. I've concluded that the best solution for me would be to use the RelativeLayout.BELOW parameter. The only problem is I'm trying to find the id of the previous TextView.
I assign the id's for the TextViews using the iterator. It seems I can't use the iterator - 1 to count as an id (even though other answers on SO suggest this works). I've even tried assigning the current TextView to a "previous_tv" variable to use in the next iteration.
I tried finding the TextView I just placed using this.findViewByID and the correct iterator value for the id, this also did not work.
Code
I'm trying to figure out what I should place as an id for my parameter rule. This is the code I'm reffering to -edit, placed full code as per request-:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Don't show application name on the top of the screen (valuable space)
setContentView(R.layout.activity_task_play); //Set which layout file to use for this activity
//Get the Task that was sent by TaskActivity
this.task = (Task) this.getIntent().getSerializableExtra("TASK_OBJECT");
//Get the Sums for this Task
this.sums = this.task.getSums();
//GridView setup
this.gridview = (GridView) this.findViewById(R.id.gridView_task_play);
ArrayAdapter<String> adapter = this.task.getArrayAdapterForGridView(this);
this.gridview.setAdapter(adapter);
//TextView setup
RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.activity_task_play_relative);
for(int i = 0; i < this.sums.length; i++)
{
//Layout parameters
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
//This variable counts how many times the placeholder text is skipped and a operator (+-/ etc.) is placed in the sum_text.
//This is usefull for placing the correct placeholder for each number in the sum (a, b, c etc.)
int times_skipped = 0;
TextView tv = new TextView(this);
String sum_text = "";
for(int j = 0; j < this.sums[i].getVariables().length; j++)
{
if(this.isParsable(this.sums[i].getVariables()[j]))
{
sum_text += TaskPlayActivity.PLACEHOLDERS[(j - times_skipped)] + " ";
}
else
{
sum_text += this.sums[i].getVariables()[j] + " ";
times_skipped++;
}
}
if(i > 0)
{
params.addRule(RelativeLayout.BELOW, i - 1);
}
tv.setId(i);
tv.setText(sum_text + "= " + this.sums[i].getAnswer());
tv.setTextColor(TaskPlayActivity.COLOURS[i]);
tv.setTextSize(25);
tv.setLayoutParams(params);
layout.addView(tv);
}
}
XMLAdded the XML. I'm not very good in layouts so it could very well be that the fault resides here.
<RelativeLayout
android:id="#+id/activity_task_play_relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="220dip"
android:layout_alignParentBottom="true">
<GridView
android:id="#+id/gridView_task_play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#EFEFEF"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:paddingTop="1dp"
android:verticalSpacing="1dp" >
</GridView>
</RelativeLayout>
</RelativeLayout>
Any other suggestions are welcome as well, though I would prefer keeping a RelativeLayout. Thanks in advance.
The documentation of View#setId says the identifier should be a positive number so you should make sure not to use zero as identifier value.
Also you have to create a new LayoutParams instance for each TextView. As it is all your TextViews share the same LayoutParams object and changes to that one object affect all TextViews.
And you could use View#generateViewId to generate an ID and remember the ID of the last iteration.
Seems like you are calling this code in a loop. In that case just put i - 1 (previous view id) as id for the rule.