Cant able to detect which position is clicked in recycler view - android

I am creating a contact list app using SQL. So I want a OnItemClickListener to detect which item is being clicked and pass it to another activity.
My work is at:
https://github.com/divyank00/ContactList
The error always is NullPointerException.

The problem with your code is that you are putting the position as an Integer Extra in your MainAcitivity, but you are trying to fetch it as a String in contact_details.java which isn't possible. To solve this:
Go to your contact_details.java class, move to Line 59:
//String position=intent.getStringExtra("pos");
//int pos=Integer.parseInt(position);
Replace these lines with this:
int pos = intent.getIntExtra("pos", 0);

Related

How to get certain field from the arrayList object on spinner selection

I just created a spinner and make this spinner read from the database. and display them in the spinner. I already did that. But I want to right down the Id of the selected item.
for example : when I select the first raw called (1, tyat abdullah, sergurey )
I need the Id (1) only to be written down without the full record.
I need the Id (1) only to be written down without the full record.
Then when you are doing labels.add, only put the data you want to see
You can also use doctorsList.get(position) within the selection listener, and set the other text view accordingly.
For example, doctorsList.get(position).getId() seems to get the information you are asking for
simply use this to get the selected item index
int index = spinner.getSelectedItemPosition(); //this will give you the seleected item index
is that what you want ?
leme Know :)

Android, how to create layout from code

I have a problem. In my app I have an Activity where I need to create x Button, then add to all this Button an ActionListener that start another activity.
In details. I have a Database table where I store x name.
In my Activity I need to have a Button for each name stored in the db and to add to each button an actionListener so when you click it it start another activity (which is the same for all buttons) with a putExtra String (which is unique for each button).
I thought that I can get all the name via an AsyncTask. But I can't figure out how to add the Buttons to the basic layout and add the Action Listener to them.
Anyone can help?
P.S. The "putExtra" String is the name I got from the db. So I also thought to get them in String[] array. Then create a Button[] array (don't know if it's possible) and create a new Button() foreach element of the String[] array. Then foreach element in the Button[] array I would putExtra the relative index String. Like Button[0] have as a putExtra the String[0], Button[1] the String[1] and so on.
The solution is a RecyclerView layout with the listener that you can find at this thread

How to Hide the Gridview specific positions based on conditions

I am passing arraylist, which contains integer values, After checking some conditions I want to hide the previous values present in the arraylist how to achieve this one. If anyone knows please help me to resolve this.
Try this:
View v = gridview.getChildAt(ChildPosition);
v.setVisibility(GONE);
Before the attach the value to the control (setText etc.) in get view method remove that specific position or if you have specific position like you want to hide 5 number position from ArrayList than trying like
here check this condition inside get view method
if(!(position == 5))
{
mTextView.setText(String.values(bean.getValues));
}
final BookingSlotTimeAdapter bookingSlotTimeAdapter = new BookingSlotTimeAdapter(getActivity(), stringArrayList, 1);
moringGridSlot.setAdapter(bookingSlotTimeAdapter);
This way I am passing arraylist to adapter. I am checking current time as a string if current time less than passing arraylist values disable the previous times of the arraylist values.

How to display database contents in a new activity at onclick of a button?

thing is, if i click a button GETDATA - then a new activity starts new.java and i want it to display the contents of my database.
in this new.java i have strings name and id. and i have lined database and this class, such that name and id have the corresponding details that i want to display.
now the ques is, how to display them ?
if i have a
EditText ta;
ta=findViewBy(R.id.edittext1);
ta.setText(name);
should this work ?
it isnt working. also, i dont want user to change the contents and want to make this filed un-editable.
should i use
TextView tf;
ta=findViewBy(R.id.textview1);
tf.setText(name);
?
even this isnt happening.
what is the procedure for this ?
it is done by using append.
TextView ta;
ta=(TextEdit)findViewById(Ri.id.textView1);
String a=getData(1); // goto database, id is 1
ta.append(a);

How to save the state of an item in a listview android

How will i go about saving the state of a listview item during scrolling, so that the recycler does not use it when displaying the next row being. i basically have a listview with 4 textviews and one of the textview is displayed based on a condition, derived from a database. For clarity sake, i will call that dependant textview "A".
my problem is that, simplest case: if "A" is being displayed for only the first item or any other row in the list and its not displayed anywhere else, when scrolling, "A" is displayed on other rows that should not have it. i understand the concept of listview reusing rows, but i can't figure out how to save it on an item, so it doesn't get used in another row. Here is a simplified code with just 2 textview:
holder.viewItemName = (TextView)view.findViewById(R.id.nameId);
holder.viewdescriptionStatus = (TextView)view.findViewById(R.id.viewdescriptionId);
int namecolumn = c.getColumnIndex(DBAdapter.NAME);
String name = c.getString(namecolumn);
holder.viewItemName.setText(name);
int description = c.getColumnIndex(DBAdapter.description);
String descriptionstatus = c.getString(description);
/*problem am having here*/
if(description != null){
holder.viewdescriptionStatus.setText("description available");
holder.viewdescriptionStatus.setTextColor(Color.BLUE);
}
as you can see, am using a viewHolder, but i have come to realise that viewHolder doesn't hold the logic as well. i don't know how to save the state of the holder.viewdescriptionStatus based on that condition. Most of the examples i have seen are based on checkboxes. Please anyone with ideas?.. it will be really appreciated.
P.S : i am using bindView() and newView() since i am using an SQLdatabase and SimpleCursorAdapter. i have the same issue with clicking, but i want to solve the scrolling part first. Thank you for your time.
Try this
holder.viewdescriptionStatus.setText(description != null ? "description available" : "");

Categories

Resources