I can populate my spinner with the array list from API. But unable to select or populate the selected item from spinner and display to the user.
The onItemSelected method does not get the position of the selected item in the spinner.
CustomSpinnerAdapter.java
public class CustomSpinnerAdapter extends BaseAdapter {
Context context;
List<String> userNames;
LayoutInflater inflter;
public CustomSpinnerAdapter(Context applicationContext, List<String> userNames) {
this.context = applicationContext;
this.userNames = userNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return userNames.size();
}
#Override
public Object getItem(int i) {
return userNames.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#NonNull
#SuppressLint({"ViewHolder", "InflateParams"})
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_item, null);
CustomTextView names = (CustomTextView) view.findViewById(R.id.tv_spinner_item);
names.setText(userNames.get(i));
return view;
}
}
My logic in fragment.
private SpinnerAdapter customAdapter;
private List<String> eqIds = new ArrayList<>;
apiInterface = ApiRequest.createService(ApiInterface.class);
Call<EquipmentTypeModel> call = apiInterface.getEquipmentType("application/json", token, id);
call.enqueue(new Callback<EquipmentTypeModel>() {
#Override
public void onResponse(Call<EquipmentTypeModel> call, Response<EquipmentTypeModel> response) {
if (response.isSuccessful()) {
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
}
}
#Override
public void onFailure(Call<EquipmentTypeModel> call, Throwable t) {
}
});
customAdapter = new CustomSpinnerAdapter(mContext, eqIds);
spTypeModel.setAdapter(customAdapter);
spTypeModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, String.valueOf(parent.getAdapter().getItem(position)), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
To get the selected value from spinner you should you
String selectedValue = spTypeModel.getSelectedItem().toString();
or
String selectedValue = String.valueOf(parent.getAdapter().getItem(position));
instead of
String selectedValue = String.valueOf(position);
Update: Change getItem method in your custom adapter to
#Override
public Object getItem(int i) {
return userNames.get(i);
}
Update 2: I saw your problem, please change your code.
private SpinnerAdapter customAdapter;
to
private CustomSpinnerAdapter customAdapter;
then add this line after you add new data to your adapter.
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
customAdapter.notifyDataSetChanged(); // Add this line to notify your adapter about new data
Update 3: From your xml file, because your spinner height is 18dp.
In spinner you set padding to 4dp (will be 8dp for top and bot)
In custom text view you also set padding 4dp (will be 8dp for top and bot)
So you only have equal or less than 2dp to display text content, it really small that why you can't see the content.
You can set your spinner height to wrap_content or keep current height but remove padding from spinner or from the custom text view. It's up to you.
Try if this works (It will show you the value, not the position)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
Related
I have 2 spinners in my recycler view. Now my 2nd spinner value depends on the 1st spinner selected item.
I am able to bind data in spinner 1 but when I try to bind spinner 2 data based on setOnItemSelectedListener of spinner 1 it will only bind for a visible item on the screen.
I want to bind spinner 2 data even if it is not visible on the screen. I have the following code for the adapter :
public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CardViewHolder> {
private ArrayList<MainDetails> mMainDetails;
private Context mContext;
public CustomerAdapter(Context mcontext, ArrayList<MainDetails> mainDetails) {
mContext = mcontext;
mMainDetails = mainDetails;
}
private Context getContext() {
return mContext;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
View itemView = LayoutInflater.from(context).inflate(R.layout.row_bind_data, viewGroup, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(final CardViewHolder cardViewHolder, int position) {
final MainDetails testMainDetails = mMainDetails.get(position);
ArrayAdapter<Products> spinnerUserAdapter = new ArrayAdapter<Products>(getContext(), R.layout.dropdown_item, testMainDetails.getUsers());
spinnerUserAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardViewHolder.spinner1.setAdapter(spinnerUserAdapter);
cardViewHolder.spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
try {
Products pp = (Products) parent.getSelectedItem();
ArrayAdapter<Orders> spinnerPPAdapter = new ArrayAdapter<Orders>(getContext(), R.layout.pc_estimator_dropdown_item, pp.getOrders());
spinnerPPAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cardViewHolder.spinner2.setAdapter(spinnerPPAdapter);
} catch (Exception e) {
Log.e("error", e.toString());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
cardViewHolder.spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//other operation
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public int getItemCount() {
return mMainDetails.size();
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
public Spinner spinner1;
public Spinner spinner2;
public CardViewHolder(View v) {
super(v);
spinner1 = (Spinner) v.findViewById(R.id.spinner1);
spinner2 = (Spinner) v.findViewById(R.id.spinner2);
}
}
}
My spinner cardViewHolder.spinner1.setOnItemSelectedListener method only calls for visible items on screens but I want to bind all data even if it not visible.
I think you need to implement everything inside ViewHolder. Let me show you an example:
#Override
public void onBindViewHolder(#NonNull SomeHolder holder, int position) {
if (SomeDataList.size() > 0) {
SomeData data = SomeDataList.get(position);
holder.bindData(data);
}
}
Then in holder class:
public class SomeHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private final TextView tvAddress;
private final TextView tvAccountNumber;
private final TextView tvName;
private SomeData someData;
public SomeHolder(#NonNull View itemView) {
super(itemView);
tvAccountNumber = itemView.findViewById(R.id.item_point_account_number);
tvName = itemView.findViewById(R.id.item_point_subscr_name);
tvAddress = itemView.findViewById(R.id.item_point_address);
itemView.setOnClickListener(this);
}
public void bindData(SomeData data) {
someData = data;
tvAddress.setText(data.getAddress());
//other stuff
}
#Override
public void onClick(View v) {
if (someData!=null){
tvName.setText(someData.getName());
//code for click event
}
}
}
straight inside onBindViewHolder you are setting only one adapter, for spinner1 and nothing for spinner2 - it got its adapter only after onItemSelected call of spinner1
if you want to set some initial values for spinner2 you have to initiate its adapter inside onBindViewHolder, not only in onItemSelected
move your adapter initiation code to separated method
public void setUpSpinner2(Spinner spinner, Products pp) {
try {
ArrayAdapter<Orders> spinnerPPAdapter = new ArrayAdapter<Orders>(getContext(), R.layout.pc_estimator_dropdown_item, pp.getOrders());
spinnerPPAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerPPAdapter);
} catch (Exception e) {
Log.e("error", e.toString());
}
}
then inside onItemSelected call it
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Products pp = (Products) parent.getSelectedItem();
setUpSpinner2(cardViewHolder.spinner2, pp);
}
and also call setUpSpinner2 straight inside onBindViewHolder with null check (is item selected on start)
if (spinnerUserAdapter.getSelectedItem() != null) {
Products pp = (Products) spinnerUserAdapter.getSelectedItem();
setUpSpinner2(cardViewHolder.spinner2, pp);
}
btw. I see you aren't storing spinner1 selected value anywhere, so after recycling both spinners will be empty again. so inside onItemSelected get position of selected item (there is method getSelectedItemPosition) and store it in some array
//top of adapters code
private ArrayList<MainDetails> mMainDetails;
private Context mContext;
private SparseArray<Integer> selectedItemsForPositions = new SparseArray<>();
inside onItemSelected and onNothingSelected put/clear selected value with this line (don't mix positions of onBindViewHolder and onItemSelected, name them differently)
selectedItemsForPositions.put(onBindViewHolderPosition, parent.getSelectedItemPosition());
// or put null if nothing selected
when you have this array which is keeping selected positions then restore this position just after spinner1 adapter set up
cardViewHolder.spinner1.setAdapter(spinnerUserAdapter);
Integer selectedPosition = selectedItemsForPositions.get(onBindViewHolderPosition);
if(selectedPosition != null) spinnerUserAdapter.setSelection(selectedPosition);
I have a ListFragment's list that contains complex layouts as items, these items contain custom spinners (MaterialSpinner).
MaterialSpinner chooseSubjectSpinner = adapter.getItem(position).findViewById(R.id.myCustomSpinner);
chooseSubjectSpinner.setAdapter(spinnerAdapter);
I've tried getChildAt(position), adapter.getItem(postition), adapter.getView(position, null, parent), and list.get(position) with the added prefix of .findViewById(R.id.myview), but it doesn't work. (It either returns NullPointerException at runtime or doesn't do anything at all!).
EDIT:
if you guys want the whole fragment code:
private ArrayList<View> listViews = new ArrayList<>();
private ViewAdapter adapter;
private List<String> spinnerItems = new ArrayList<>();
private MaterialSpinnerAdapter<String> spinnerAdapter;
public FragmentUnitTest() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_unit_test, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState){
spinnerItems.add("Hey!");
spinnerItems.add("Hello!");
spinnerItems.add("Hi!");
spinnerAdapter = new MaterialSpinnerAdapter<>(getContext(), spinnerItems);
adapter = new ViewAdapter(listViews, view.getContext());
setListAdapter(adapter);
addView(R.layout.cardview_with_spinner);
MaterialSpinner spinner = adapter.getItem(0).findViewById(R.id.custom_spinner);
RLogger.e("FragmentUnitTest", spinner);
spinner.setAdapter(spinnerAdapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Clicked: " + position + " .", Toast.LENGTH_SHORT).show();
}
Create a Custom spinner like this:
public class PTCustomSpinner extends ArrayAdapter {
public PTCustomSpinner(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
int count = super.getCount();
return count>0 ? count-1 : count ;
}
Handle it like below // BELOW XML LAYOUT CONTAINS ONLY TEXTVIEW WITH ID "temp"
PTCustomSpinner spinnerArrayAdapter = new PTCustomSpinner(PTExceptionMainPage.this, R.layout.xml_with_only_textview);
spinnerArrayAdapter.addAll(team_array);
spinnerArrayAdapter.add("Pick Team");
spinnerTeam.setAdapter(spinnerArrayAdapter);
spinnerTeam.setSelection(spinnerArrayAdapter.getCount());
spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinnerTeam.getSelectedItem() != "Pick Team") {
teamIdSelected = apiResponseTeam.get(i).getTeamID();
}
else if( spinnerTeam.getSelectedItem().equals("Pick Team"))
{
View v = spinnerTeam.getSelectedView();
((TextView) v).setTextColor(getResources().getColor(R.color.ash));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I made a spinner in which im using a arrayadapter to populate it with images and text. on OnItemSelected i want to select only and only the text not the image.
Here is my code for the MainActivity.java
ArrayList<ItemData> list = new ArrayList<>();
list.add(new ItemData("Select Plan Category",R.drawable.ic_bars));
list.add(new ItemData("Hookah",R.drawable.ic_006_hookah));
list.add(new ItemData("Drinks",R.drawable.ic_005_pint));
list.add(new ItemData("Gedi",R.drawable.ic_004_racing));
list.add(new ItemData("Snacks",R.drawable.ic_003_chips));
list.add(new ItemData("Shopping",R.drawable.ic_002_cart));
list.add(new ItemData("Bownling",R.drawable.ic_001_bowling));
SpinnerAdapter adapter = new SpinnerAdapter(getActivity(), R.layout.spinner_layout,R.id.categoryText,list);
categorySpinnerjava.setAdapter(adapter);
Here is my code for the ItemData.java
public class ItemData {
private String planCategorytext = "";
Integer imageId;
public ItemData(String text, Integer imageId)
{
this.planCategorytext=text;
this.imageId=imageId;
}
public String getPlanCategorytext() {
return planCategorytext;
}
public void setPlanCategorytext(String planCategorytext) {
this.planCategorytext = planCategorytext;
}
public Integer getImageId() {
return imageId;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
}
}
And here is my code for the SpinnerAdapter.java(that is extending the ArrayAdapter
int groupid;
Activity getActivity;
ArrayList<ItemData> list;
LayoutInflater inflater;
public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemData> list) {
super(context, id,list);
this.list=list;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groupid=groupid;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = inflater.inflate(groupid,parent,false);
ImageView imageView=(ImageView)itemView.findViewById(R.id.categoryVector);
imageView.setImageResource(list.get(position).getImageId());
TextView textView = (TextView)itemView.findViewById(R.id.categoryText);
textView.setText(list.get(position).getPlanCategorytext());
return itemView;
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getView(position,convertView,parent);
}
I want to only select the text not the image. And when i normally do the
String spinnerselection = categorySpinnerjava.getSelectedItem().toString();
It gives me a output of "applicationpackage.ItemData#randomnumbers"
you are getting this error because
"applicationpackage.ItemData#randomnumbers"
your categorySpinnerjava.getSelectedItem(); return your model class ItemData not a String
Try this
ItemData itemData = (ItemData) categorySpinnerjava.getSelectedItem();
String data= itemData.getPlanCategorytext();
Integer imageId = itemData.getImageId();
You can get the selected spinner item's text in the following way:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
String selectedText=list.get(pos).getPlanCategorytext();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
OR
String selectedItemText=list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();
Where list is the arraylist with ItemData which you used above to populate the data in spinner.
Also about this:
String spinnerselection = categorySpinnerjava.getSelectedItem().toString();
You have to replace categorySpinnerjava.getSelectedItem().toString(); with list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();
Try implementing using Interface.
Set text data inside SpinnerAdapter and extend Interface in Activity class to get the selected text
Try the following:
String spinnerselection = ((ItemData) categorySpinnerjava.getSelectedItem()).getPlanCategorytext();
Didn't try it yet, but in theory it should work.
Try like this,
ItemData spinnerselection = (ItemData)categorySpinnerjava.getSelectedItem();
String plan = spinnerselection.getPlanCategorytext();
Override getItem() in your adapter. Cause getSelectedItem() return :
adapter.getItem(position);
#Override
public String getItem(int position) {
return list.get(position);
}
I have a ListView that is within a Fragment. In the onCreateView section I have set a onItemClickListener for the list, which highlights the selected item in the ListView. I have set two ImageButtons that navigate up and down the list. On selection a new Row is inflated that has its TextView's set to the content of the select item (for the purpose of retaining the highlighted selected state). However I am having difficulty adding that item back to the list. The app will not compile due to the line routines.add(selectedPos-1, str); it wants wants int location, Routine object. I believe the issue is with my construction of my SelectedAdapter class, but I have been unable to determine what to change/pass with regard to the Object.
IE:
public SelectedAdapter(Context context, int textViewResourceId,List objects) {
super(getActivity(), R.layout.listview_routines, routines); }
I would greatly appreciate any input as how to correct this issue; as well as any advice if there is a better way to maintain a selected state. Thanks for your help.
Fragment:
public static class FragmentRoutine extends Fragment {
DatabaseHandler db;
private ListView routineListView;
private List<Routine> routines = new ArrayList<Routine>();
ArrayAdapter<Routine> routineAdapter;
Routine longClickedItemRoutines;
private SelectedAdapter selectedAdapter;
public FragmentRoutine() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.routines,
container, false);
db = new DatabaseHandler(getActivity().getApplicationContext());
routineListView = (ListView) rootView.findViewById(R.id.routineList);
registerForContextMenu(routineListView);
db.closeDB();
if (db.getExerciseCount() != 0)
routines.clear();
routines.addAll(db.getAllRoutines());
populateList();
selectedAdapter = new SelectedAdapter(this.getActivity(), 0, routines);
selectedAdapter.setNotifyOnChange(true);
routineListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemRoutines = routines.get(position);
return false;
}
});
routineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
selectedAdapter.setSelectedPosition(position);
}
});
routineListView.post(new Runnable() {
#Override
public void run() {
routineListView.setItemChecked(0, true);
}
});
// move up event handler
ImageButton btnMoveUp = (ImageButton) rootView.findViewById(R.id.btnMoveUp);
btnMoveUp.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveUp();
}
});
// move down event handler
ImageButton btnMoveDown = (ImageButton) rootView.findViewById(R.id.btnMoveDown);
btnMoveDown.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
moveDown();
}
});
setHasOptionsMenu(true);
return rootView;
}
// Move selected item "up" in the ViewList.
private void moveUp(){
Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos > 0 ){
routines.remove(selectedPos);
String str = currentToDoSave.getTagName();
//Problem Line Below
routines.add(selectedPos-1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos-1);
}
}
// Move selected item "down" in the ViewList.
private void moveDown(){
Routine currentToDoSave = routines.get(selectedAdapter.getSelectedPosition());
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos < routines.size()-1 ){
routines.remove(selectedPos);
String str = currentToDoSave.getTagName();
routines.add(selectedPos+1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos+1);
}
}
public class SelectedAdapter extends ArrayAdapter<Routine>{
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List objects) {
super(getActivity(), R.layout.listview_routines, routines);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi
= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.txtExample);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
}else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
return(v);
}
}
private void populateList() {
routineAdapter = new SaveListAdapterT();
routineListView.setAdapter(routineAdapter);
}
public class SaveListAdapterT extends ArrayAdapter<Routine> {
public SaveListAdapterT() {
super(getActivity(), R.layout.listview_routines, routines);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.listview_routines, parent, false);
Routine currentToDoSave = routines.get(position);
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(currentToDoSave.getTagName());
return view;
}
}
}
i've noticed an extrage behavior on my app that has a ListView with three EditTexts,
the problem is that whenever i select one textedit and move away from focus and come back the text i wrote in the first row i selected either desapears or moves to a different row, also when an edittext is focused and i go down in the list it seems that i have selected the edittext in the same position but 10 or 11 rows after the one im actually focusing(the one i can write to).
any knowledge on that case?
also im new to android so i dont know if thats supposed to happen.
this is the List im using.
public class In_List {
private int id;
private String text;
private float a;
private float Qty;
public In_List (int id, String text, float a, float Qty) {
this.id = id;
this.text = text;
this.a = a;
this.Qty= Qty;
}
public String get_text() {
return text;
}
public float get_a() {
return a;
}
public int get_id() {
return id;
}
public float get_Qty() {
return Qty;
}
}
here is the adapter:
public abstract class List_Adapter extends BaseAdapter {
private ArrayList<?> ins;
private int R_layout_IdView;
private Context context;
public Lista_adaptador(Context context, int R_layout_IdView, ArrayList<?> ins) {
super();
this.context = context;
this.ins = ins;
this.R_layout_IdView = R_layout_IdView;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R_layout_IdView, null);
}
onInsert (ins.get(position), view);
return view;
}
#Override
public int getCount() {
return ins.size();
}
#Override
public Object getItem(int position) {
return ins.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public abstract void onInsert (Object insert, View view);
}
and here is the main activity. it has a popup window that i used to fill the value of Qty but i its not included.
public class MainActivity extends Activity {
private ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing);
ArrayList<In_List> data = new ArrayList<>();
for(int i=0; i<100; i++){
data.add(new In_List(i, "Item Number :"+i+1, i*2,0));
}
list = (ListView) findViewById(R.id.ListView_listing);
list.setAdapter(new List_Adapter(this, R.layout.entry, data){
#Override
public void onInsert(Object entr, View view) {
if (entr != null) {
TextView id_Text = (TextView) view.findViewById(R.id.textView_id);
if (id_Text != null)
id_Text.setText(((In_List) entr).get_id());
TextView info_Text = (TextView) view.findViewById(R.id.textView_info);
if (info_Text != null)
info_Text.setText(((In_List) entr).get_text());
TextView inside_Text = (TextView) view.findViewById(R.id.textView_inside);
if (inside_Text != null)
inside_Text.setText(((In_List) entr).get_a());
TextView Qty_Text = (TextView) view.findViewById(R.id.textView_qty);
if (Qty_Text != null || Qty_Text.getText().toString().equals(0))
Qty_Text.setText(((In_List) entr).get_Qty());
Qty_Text.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Add_Qty();
}
});
}
}
});
// list.setOnItemClickListener(new OnItemClickListener() {
// #Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// In_List chosen = (In_List) parent.getItemAtPosition(position);
//
// CharSequence text = "Selected: " + chosen.get_textoDebajo();
// Toast toast = Toast.makeText(MainActivity.this, texto, Toast.LENGTH_LONG);
// toast.show();
// }
// });
}
}
now, when i scroll down through the list the Qtys that i have entered either disappear or move to another row.
fixed. i wasn't stablishing Qty_Text value at all. also made a method to save into the adapter array.
EditTexts in generally are very tricky. And using them inside a ListView is almost impossible. The special behaviour of EditTexts for example to always automatically assume focus combinded with the View recycling of the ListViews messes with the ListView and causes a lot of problems. I would suggest you look for another solution. For example placing the EditText inside the HeaderView of the ListView is fine, as the HeaderView is not recycled as you scroll through the ListView.