AutoCompleteTextView causes items in ListView to disappear - android

I'm doing the Extra Credit section of the Adding a List section in Android Tutorials. I tried populating the AutoCompleteTextView (ACTV) with the items contained in the ArrayAdapter, but if the addr field has characters in it above the threshold limit, the list doesn't show any items. Here is the code:
public class MyActivity extends Activity
{
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView)findViewById(R.id.restaurants);
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.addr);
textView.setAdapter(adapter);
}
private View.OnClickListener onSave= new View.OnClickListener(){
...
};
}
And the XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="1"
android:layout_alignParentBottom="true"
>
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<AutoCompleteTextView android:id="#+id/addr"
android:completionThreshold="5"
/>
</TableRow>
<TableRow>
<TextView android:text="Type:"/>
<RadioGroup android:id="#+id/types">
<RadioButton android:id="#+id/take_out"
android:text="Take Out"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
<RadioButton android:id="#+id/sit_down"
android:text="Sit Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/delivery"
android:text="Delivery"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RadioGroup>
</TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/save"
android:text="Save"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"/>
</RelativeLayout>
Changing the completionThreshold changes the number of characters I can enter before the items in the List disappear.
Thanks for the help

You are attempting to use the same adapter for both the ListView and the AutoCompleteTextView. That is not a good idea. Please use separate adapters. You might even want a separate layout for the adapter used with the AutoCompleteTextView (e.g., android.R.layout.simple_dropdown_item_1line, as I used in this sample project).

Related

Accessing element from same layout parent

i have a listview with different components (2 linear layouts and a button) , what i want to do is when i click on a button that's inside one of those linear layouts , i want to access a textview that's inside the other linearlayout , is it possible ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listV2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="85dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textCname"
android:textSize="21dp"
android:textStyle="bold" />
<TextView
android:id="#+id/numero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#color/textCother"
android:textStyle="italic" />
<TextView
android:id="#+id/ville"
android:layout_width="match_parent"
android:textColor="#color/textCother"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="italic" />
</LinearLayout>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="invisible" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
>
<ImageView
android:id="#+id/remove"
android:onClick="contactRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/edit"
android:onClick="contactEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"/>
<ImageView
android:onClick="contactCall"
android:id="#+id/call"
android:layout_marginLeft="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
what i want to do is access the value of android:id="#+id/numero"
when i click on one of the image views
You can set OnClickListener interface provided by View class. Something like this.
Implement listener on your activity class
public class SpinnerActivity extends AppCompatActivity implements
View.OnClickListener
Declare class level variables for your views
private TextView tvNumero;
private ImageView ivRemove, ivEdit, ivContactCall;
Find initialise value in onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gildi_spinner);
tvNumero = (TextView) findViewById(R.id.numero)
ivRemove = (ImageView) findViewById(R.id.remove);
ivEdit = (ImageView) findViewById(R.id.edit);
ivContactCall = (ImageView) findViewById(R.id.contactCall);
ivRemove.setOnClickListener(this);
ivEdit.setOnClickListener(this);
ivContactCall.setOnClickListener(this);
}
Implemented class from Onclickistener, where you get on click event for your registered views
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.remove:
case R.id.edit:
case R.id.contactCall:
Toast.makeText(this, tvNumero.getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
NOTE : tvNumero.getText().toString() gives you the value of your desired TextView.
UPDATE :
Firstly replace your ListView with RecyclerView
Refer RecyclerView Example tutorial.
Secondly to achieve onClick for your listed items
Refer recyclerview-onclick tutorial.
Pass context when you create your adapter, Use that context to get the inflated view.
Adapter adapter = new Adapter(this);
Then in Adpater Class constructor :
public Adapter(Context context){
context.findViewById(R.id.textview);//consider this as numero textview
}
Then access it as per your requirement. Hope it helps!

TextView taller than containing TableRow after calling spinner.setdapter() in same row

I have a xml view that I'm inflating. I'm then doing a network call and with the results populating all these spinners. This is what the view looks like before I call setadapter on the spinners.
However, when I call spinner.setadapter(), my last TextView gets cut off.
As you can see, the TextView is larger than the TableRow.
Here is snippet of xml layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bcast_table">
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_num_retx"
android:id="#+id/num_retx_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/num_retx_spinner" />
</TableRow>
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_cwm0"
android:id="#+id/cwm0_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/cwm0_spinner" />
</TableRow>
</TableLayout>
</LinearLayout>
And here is how I setadapter()
static void populateSpinner(int spinId, ArrayList<String> fields, String currPos, View template, Context context){
Spinner spinner = (Spinner) template.findViewById(spinId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, fields);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
int position = adapter.getPosition(currPos);
spinner.setSelection(position);
}
Thanks for the help!

How do I populate the contents from EditText in ListView?

I have a layout file in which I have defined a ListView and another layout file in which I have defined the style of the individual list element. OnClick how do I populate my ListView by using the other layout? Or is it even possible to do so?
This is my ListView
<ListView
android:id="#+id/chat_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
>
</ListView>
<RelativeLayout
android:id="#+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical">
</RelativeLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/chat"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/send_button"/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/icons_chat"
android:height="20dp"
android:width="20dp"
android:id="#+id/send_button"
android:onClick="sendChat"
android:layout_alignBottom="#+id/chat"
android:layout_alignParentRight="true"
/>
This is my other layout
<EditText
android:id="#+id/user_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
OnClick I want to getText from the EditText in the first layout and update the ListView using the style in the second layout. How can I do that?
I think that one way to do it is with an ArrayAdapter which will adapt your Collection (which you want to use) to your items in your layout ListView for your case. For example
public class YourActivity extends Activity {
private ListView listView;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
listView= (ListView) findViewById(R.id.you_list_id);
//Here you can set values from your edit texts
ArrayList<String> yourArrayList = new ArrayList<>();
yourArrayList.add("someTextFromYourEditTexts");
yourArrayList.add("someTextFromYourEditTexts");
//Create your ArrayAdapter with your already populate array list
ArrayAdapter<String> yourArrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
yourArrayList );
//And when your adapter is create set it to your list view
listView.setAdapter(yourArrayAdapter);
}
}

creating scrollable spinner to show some items

i used spinner to show items but it only works when the items are less than 5 items, and when i want to show 10 items this error happens : unfortunately stopped :( how can i use scrollable spinner to show all items in the spinner
here is:
spinner.xml :and main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/shw"
android:orientation="vertical" >
<TextView
android:id="#+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="which"
android:textColor="#ffffff" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/bt1help2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Spinner01"
android:layout_marginRight="100dp"
android:layout_toLeftOf="#+id/txt3"
android:text="ok"
android:textColor="#ffffff" />
<Button
android:id="#+id/btn2help2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="no"
android:textColor="#ffffff" />
</LinearLayout>
<Spinner
android:id="#+id/Spinner01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:verticalScrollbarPosition="left"
android:scrollbarThumbVertical="#drawable/scrollbar_style"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="22dp" />
final Dialog dl = new Dialog(Main.this);
dl.setContentView(R.layout.spinner);
dl.setTitle("show");
TextView txt2 = (TextView) dl.findViewById(R.id.txt2);
TextView txt3 = (TextView) dl.findViewById(R.id.txt3);
///تنظیم متن
txt2.setText(" which one do you want to show?");
Button btn1help2 = (Button) dl.findViewById(R.id.bt1help2);
btn1help2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String array_spinner[];
array_spinner=new String[5];
array_spinner[0]="letter_one";
array_spinner[1]="letter_2";
array_spinner[2]="letter_3";
array_spinner[3]="letter_4";
array_spinner[4]="letter_5";
array_spinner[5]="letter_6";
array_spinner[6]="letter_7";
array_spinner[7]="letter_8";
array_spinner[8]="letter_9";
array_spinner[9]="letter_10";
Spinner s = (Spinner) dl.findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
how can change it to scrollable spinner?
Thanks for your answers
You can use ScrollView. Put your RelativeLayout inside ScrollView because it just can have one child.
<ScrollView>
<RelativeLayout>
.... other views
</RelativeLayout>
</ScrollView>
your problem is this array_spinner=new String[5]; you set size of 5 items but you put 10 so, you initialize the array this String array_spinner[]; array_spinner=new String[10];

ListView is not working for android

So I am reading Commonsware's Android Programming Tutorials and I am stuck with the part where the book asks me to add a ListView. Here is my layout's xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="#+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="#+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="#+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/details"
/>
</RelativeLayout>
</ScrollView>
and this is my activity code
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import java.util.LinkedList;
import java.util.List;
public class LunchList extends Activity {
List<Restaurant> model = new LinkedList<Restaurant>();
ArrayAdapter<Restaurant> arrayAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView listView = (ListView) findViewById(R.id.restaurants);
arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
listView.setAdapter(arrayAdapter);
}
private View.OnClickListener onSave = new View.OnClickListener(){
public void onClick(View view) {
EditText nameField = (EditText) findViewById(R.id.name);
EditText addressField = (EditText) findViewById(R.id.address);
Restaurant restaurant = new Restaurant();
restaurant.setName(nameField.getText().toString());
restaurant.setAddress(addressField.getText().toString());
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);
switch (radioGroup.getCheckedRadioButtonId()) {
case (R.id.take_out):
restaurant.setType("take_out");
break;
case (R.id.sit_down):
restaurant.setType("sit_down");
break;
case (R.id.delivery):
restaurant.setType("delivery");
break;
case (R.id.korean):
restaurant.setType("korean");
break;
case (R.id.chinese):
restaurant.setType("chinese");
break;
case (R.id.japanese):
restaurant.setType("japanese");
break;
case (R.id.italian):
restaurant.setType("italian");
break;
case (R.id.indonesian):
restaurant.setType("indonesian");
break;
}
arrayAdapter.add(restaurant);
Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
}
};
}
I added a logging line to see whether the object is being added to the adapter or not and it looks like it is being added in there. The UI however is not showing the ListView and I do not see stuff getting added in the list.
Edit XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="#+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="#+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="#+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"
/>
</RelativeLayout>
</ScrollView>
As far as I can see, you initialise model:
List<Restaurant> model = new LinkedList<Restaurant>();
But don't put any content in it, so there is nothing for your list to show
EDIT: If you are adding content to your list dynamically, make sure that you are updating the list like this:
model.add(restaurant);
arrayAdapter.notifyDataSetChanged();
Or:
arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();
notifyDataSetChanged() lets the ListView know that the list contents have changed and it should redraw itself
EDIT: Also, you are adding the ListView above the TableLayout called details, which has its heigh and width set to fill_parent, so you may not see the ListView if the TableLayout is taking up the whole screen. Try changing the height of details to wrap_content
How exactly do you want this UI to look like? Your ListView says this:
android:layout_alignParentTop="true"
android:layout_above="#id/details"
So you're basically saying you want your ListView to be above details. But details is the first element, so that on is presumably on the top of the screen already!
EDIT: I wrote it in a comment, but I should amend my answer: use a LinearLayout instead (vertical orientation, of course), and give each element a weight of 1 (or adjust the weight as you wish). That way, one element can't drown out the other.

Categories

Resources