so I have an app with 3 fragments. One of the fragments involves adding characters to a listview and then giving them weapons and abilities. Weapons and abilities are given to a characters via a custom dialog that appears when the Add button is clicked.
This dialog contains a spinner with a predetermined list of names of characters and then 3 edit-texts that require the user to enter the number of abilities that the characters is supposed to have.
My problem is that the spinner in this dialog is not displaying its contents.
Everytime I click on the dialog I get this :
W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
Rewards.java
public class Rewards extends Fragment {
private Button button;
Context mContext;
int lesserCount;
int greaterCount;
int exaltedCount;
Dialog addCharDialog;
Dialog lesserDialog;
Dialog greaterDialog;
Dialog exaltedDialog;
Spinner SpnLesser1;
Spinner SpnLesser2;
Spinner SpnLesser3;
Spinner SpnGreater1;
Spinner SpnGreater2;
Spinner SpnExalted;
Spinner SpnChar;
ArrayList<Character> chars = new ArrayList();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rewards, container, false);
View addCharDialogView = inflater.inflate(R.layout.character_add, container, false);
button = (Button) rootView.findViewById(R.id.btnadd);
mContext = getActivity();
// custom dialogs
createAddDialog(addCharDialogView);
createLesserDialog();
createGreaterDialog();
createExaltedDialog();
SpnLesser1 = (Spinner) rootView.findViewById(R.id.lesserspinner1);
SpnLesser2 = (Spinner) rootView.findViewById(R.id.lesserspinner2);
SpnLesser3 = (Spinner) rootView.findViewById(R.id.lesserspinner3);
SpnGreater1 = (Spinner) rootView.findViewById(R.id.greaterspinner1);
SpnGreater2 = (Spinner) rootView.findViewById(R.id.greaterspinner2);
SpnExalted = (Spinner) rootView.findViewById(R.id.exaltedspinner);
return rootView;
}
public View createAddDialog(View addCharDialogView) {
LayoutInflater inflater = LayoutInflater.from(this.getActivity());
addCharDialog = new Dialog(mContext);
addCharDialog.setContentView(R.layout.character_add);
addCharDialog.setTitle("Add a character");
SpnChar = (Spinner) addCharDialogView.findViewById(R.id.dialog_spinner);
ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),R.array.item_array, R.layout.spinner_layout);
//ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(addCharDialogView.getContext(),R.array.item_array, R.layout.spinner_layout);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
SpnChar.setAdapter(dataAdapter);
//spinner is populated but it's not displaying and won't do anything when clicked on
SpnChar.setSelection(2);
String Text = SpnChar.getSelectedItem().toString();
Log.d("2", Text);
Button dialogOkButton = (Button) addCharDialog.findViewById(R.id.dialogOK);
// if button is clicked, close the custom dialog
dialogOkButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lesserCount = R.id.lessertext;
greaterCount = R.id.greatertext;
exaltedCount = R.id.exaltedtext;
Character c;
if (lesserCount > 0) {
lesserDialog.show();
}
if (greaterCount > 0) {
greaterDialog.show();
}
if (exaltedCount > 0) {
exaltedDialog.show();
}
}
});
Button dialogCancelButton = (Button) addCharDialog.findViewById(R.id.dialogCancel);
// if button is clicked, close the custom dialog
dialogCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addCharDialog.dismiss();
}
});
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addCharDialog.show();
}
});
return addCharDialogView;
}
}
}
character_add.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--<TextView-->
<!--android:id="#+id/dialog_label"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_width="fill_parent"-->
<!--android:hint="Please select an option"-->
<!--/>-->
<Spinner
android:id="#+id/dialog_spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="#+id/dialogOK"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_alignTop="#+id/dialogOK"
android:layout_toRightOf="#+id/dialogOK" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/lessertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText8"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/greatertext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText9"
android:text="0" />
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/exaltedtext"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/editText10"
android:text="0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:layout_below="#+id/dialog_spinner"
android:layout_alignParentStart="true"
android:text="L" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/editText9"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/lessertext"
android:text="G" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText10"
android:layout_below="#+id/dialog_spinner"
android:layout_toRightOf="#+id/greatertext"
android:text="E" />
If I understand correctly, you want a spinner that can display the input the user types in? If so, you could use an Arraylist and create a method that will destroy your spinner and recreate it every time the contents changes. You could set it up as follows:
ArrayList<String> thing = new ArrayList<String>();
ViewGroup layout = null;
layout = (ViewGroup) spinner.getParent();
public void addSpinner(ArrayList thing){
layout.removeView(spinner);
layout.addView(spinnersy2);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, thing);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
Create a second method that will add the user input into the array.
Related
When I click EditText, I want spinner to open the menu. When I click on textviewplaces, I want the spinner menu to be opened. How can I do this?
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener {
private Spinner spinnerDevice;
private static final String[] paths = {"......"};
TextView textviewPlaces;
spinnerDevice = (Spinner)findViewById(R.id.spinnerDevice);
ArrayAdapter<String>adapterSpinnerDevice = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
textviewPlaces = findViewById(R.id.places_textView);
adapterSpinnerDevice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDevice.setAdapter(adapterSpinnerLocation);
spinnerDevice.setOnItemSelectedListener(this);
XML File:
<AutoCompleteTextView
android:id="#+id/places_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="44dp"
android:layout_marginTop="31dp"
android:text="TextView"
android:drawableLeft="#drawable/location"
android:drawablePadding="2dp"
android:ems="10"
android:textStyle="bold" />
Use AutoCompleteTextView instead of it
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_dropdown_item_line,
paths);
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item, paths);
final AutoCompleteTextView textView;
textView = (AutoCompleteTextView) findViewById(R.id.textView);
textView.setAdapter(arrayAdapter);
/* textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
textView.showDropDown();
}
}); */
Define UI element in your layout file
<AutoCompleteTextView
android:id="#+id/textView"
android:layout_width="match_content"
android:layout_height="wrap_content"
android:ems="10" />
If you want something like this image
AutoCompeleteTextView Exmaple
Arwy Shelke answer can help you, but if you want to open a real spinner you can use this code:
spinner.performClick();
I'm developing with android studio.
I have a ListView that the elements in it are aligned from left to right and I want to align them from right to left, how can I do so?
the java class is:
public class ProductList extends ListActivity {
ArrayList<String> list = new ArrayList<String>(Arrays.asList("במבה אוסם", "בשר טחון", "קוקה קולה שישייה", "קפה עלית", "שמיר", "מילקי","רבעיית דניאלה"));
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
Button btnDel = (Button) findViewById(R.id.btnDel);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
/** Defining a click event listener for the button "Delete" */
View.OnClickListener listenerDel = new View.OnClickListener() {
#Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the delete button */
btnDel.setOnClickListener(listenerDel);
/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
}
and the layout is:
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore">
</ListView>
</LinearLayout>
Thank you !
Use RecyclerView instead of ListView.
LinearLayoutManager(Context context, int orientation, boolean reverseLayout)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" ><br>
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/><br>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_toLeftOf="#+id/textView_name"/>
</RelativeLayout>
Manifest.xml
<application
....
android:supportsRtl="true"
...
</application>
you should use custom layout like
myTextView.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="right"
android:background="#bdbdbd"/>
pass this layout to adapter like
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.nameOfLayout, list);
I am adding the View to the Header of ListView in the ListActivity, after inflating which has AutoCompleteTextView and not showing the suggestions on entering text. Though it works absolutely fine in normal case but not working fine when added to Header of ListView.
header_route.xml :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/lblride_From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_alignParentTop="true"
android:text="#string/lblride_Route"
/>
<AutoCompleteTextView
android:id="#+id/txtFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lblride_From"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:completionThreshold="1"
android:ems="10"
android:width="290dp"
android:imeOptions="actionGo"
android:popupBackground="#android:color/black" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/hdbtnOkRoute"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtFrom"
android:layout_below="#+id/txtFrom"
android:layout_marginTop="2dp"
android:minHeight="40dp"
android:text="#string/txtBtnOk"
android:width="290dp" />
</RelativeLayout>
public class BusRouteActivity extends ListActivity implements TextWatcher {
AutoCompleteTextView actvFrom;
Button buttonOK;
TextView txtViewRouteItem;
ListView lstViewRoute;
ArrayAdapter<String> listAdapter;
DataBaseHelper databaseHelper;
TextView tvFooter;
Context appContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.nameBusRouteActivity);
appContext = getApplicationContext();
lstViewRoute = getListView();
LayoutInflater inflater = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.header_route, null);
DataBaseHelper dbh = new DataBaseHelper(getApplicationContext());
try {
dbh.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbh.openDataBase();
databaseHelper = dbh;
actvFrom = (AutoCompleteTextView) view.findViewById(R.id.txtFrom);
actvFrom.addTextChangedListener(this);
actvFrom.setOnEditorActionListener(actionListener);
ArrayAdapter<String> aroutes = new ArrayAdapter<String>(appContext, R.layout.routeitem, dbh.GetRoutes());
actvFrom.setAdapter(aroutes);
buttonOK = (Button)view.findViewById(R.id.hdbtnOkRoute);
buttonOK.setOnClickListener(OkOnClickListener);
tvFooter = new TextView(this);
tvFooter.setTextAppearance(getApplicationContext(), R.style.footerStyle);
String text = getResources().getString(R.string.footer);
tvFooter.setText(Html.fromHtml(text));
listAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routeitem);
lstViewRoute.addHeaderView(view, null, false);
lstViewRoute.addFooterView(tvFooter, null, true);
lstViewRoute.setClickable(true);
lstViewRoute.setSelected(true);
lstViewRoute.setAdapter(listAdapter);
}
Please let me know if I am missing any setting? Thanks in advance.
Here is one code that might help you!!
http://www.javatpoint.com/android-autocompletetextview-example
I have problem with adding 2 numbers in list view. This is my row with 2 text view (for each numer). textView2 ignore
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
When user add number it will display in list view. That second number needs to be 11 - that number that user inserted. It's all working but I dont know how to display that second number.
public class MainActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
Button bt;
EditText et;
TextView tv;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inflate the menu; this adds items to the action bar if it is present.
bt = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
lv = (ListView) findViewById(R.id.lista);
m_adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.textView1);
lv.setAdapter(m_adapter);
//final String input = et.getText().toString();
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = et.getText().toString();
if(null!=input&&input.length()>0){
String maxpunti = "11";
int a = Integer.parseInt(maxpunti);
int b = Integer.parseInt(input);
int c = a-b;
String input2 = String.valueOf(c);
m_adapter.add(input);
m_adapter.add(input2);
m_adapter.notifyDataSetChanged();
}
}
});
}
}
The default implementation of ArrayAdapter<String> binds a single Stringvalue to a a single textfield. Since you have several views for each listitem or "row" in your listview ( you need to implement/extend your own ArrayAdapter and override the getView method.
Them you can set the values for any number of custom views that you may have in your list item layout.
Look at a simple example and also it might be a good idea to look som docs on how listviews/adpters work in android since they are very central.
I need to output data from lines cm1 and cm2 in a ListView, every time when I press the button. Line should be shown in various TextView.
Code TEST.java:
public String cm1;
public String cm2;
public class TEST extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyList = (ListView) this.findViewById(R.id.listStrings);
setListAdapter();
// button
Button setup = (Button) this.findViewById(R.id.send);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cm1 = "cm1text";
cm2 = "cm2text";
//xxxx
}
});
}
// adapter
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row //idknow what im must writehere to set cm1 string to cm1text tv
List.setAdapter(adapter);
}
Code TEST.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listStrings"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
Code row.xml:
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/cm1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/cm2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Should look ListView after 2 clicks:
cm1
cm2
cm1
cm2
Maybe what you should/can do is have this;
String cm = cm1 + "\n" + cm2
Which should output;
cm1
cm2
Then you change the xml to just have one textview and the adapter code as follows;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.textViewCM, listItems);
yourListView.setAdapter(aa);
Then, whenever you press the button;
// ...code to initialise/change cm
listItems.add(cm);
aa.notifyDataSetChanged(); // notifies listView to update it's view
That should do what you're after unless you've got to have the two individual textViews, in which case, you'll need to write a custom adapter, I've done a bit of a tutorial on my website if you follow that link.