Detailed view of list row - android

In my project have to populate the list view from local database.I have implemented it.When I click the row in list item I need to show all the details in list row in next activity.I implemented custom list adapter.I not yet started to code for detailed list row.How can I pass all details in single row to another activity.Can anyone help me?

You can set the information in tags of textviews of your custom list and pass them through intents.

one thing that you can do is just pass the id (PK) of the item . Then on next activity you can fetch it again from database.
another options is that you can create a class with all the data you want to forward as class'members and serialize the object and send it along with the intent.
here is a example
public class ActivityExtra implements Parcelable {
public Integer a=0;
public String b="";
private GameActivityExtra(Parcel in) {
this.a = in.readInt();
this.b = in.readString();
}
public GameActivityExtra() {
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(a);
dest.writeString(b);
}
public static final Parcelable.Creator<GameActivityExtra> CREATOR = new Parcelable.Creator<GameActivityExtra>() {
public GameActivityExtra createFromParcel(Parcel in) {
return new GameActivityExtra(in);
}
public GameActivityExtra[] newArray(int size) {
return new GameActivityExtra[size];
}
};
}
now create instance of this class in your activity . and use intent.putextra(...) to put it . and get the same object while receiving it.

Use #nitesh goel answer to make your object class parcelabe.
Then onitemclick use
intent.putExtra("object", object);
to send your object to other activity.
And in corresponding activity use
intent.getParcelableExtra("object");
to get your object. then you can get everything of that object.

Related

Object List on RecyclerView

I'm having a little big problem on Android Studio. A have an object "Sport" that can have multiple lessons. So inside this object there is a list.
public class Sport implements Serializable {
private String cid;
private String cname;
String csportpic;
long ccreatedat;
List<Lesson> lessons;
public Sport() {
}
The object has gets and sets for each element. So the data coming from Firebase in well placed.
In a recycleview I want to display the lessons data and also some variable of the object sport(cname, cid), so I'm passing to the recycleview constructor the object Sport.
Do you have any ideia how to perform a loop through lesson List in a recyclerview? Can it be done inside onBindViewHolder?
Sorry, got it working.
For some reason the first loop was not working correctly. Perhaps the Object was not well written, or the query to the database.
public void onBindViewHolder(#NonNull final CoachVH holder, final int position)
{
for (Lesson lesson:sportlesson.get(position).getLessons())
{
holder.classid.setText(String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm").format(lesson.getLessonTime())));
holder.classname.setText(sportlesson.get(position).getCname());
holder.classaddress.setText(String.valueOf(lesson.getLessonAddress()));
holder.classprice.setText(String.valueOf(lesson.getLessonPrice()));
}
}

greenDao get auto-increment-ed ID

Is it any way for me to get the next available ID for a table row (that would be automatically created when inserting a row in the table), so I would not be forced to insert that row at a given time, to get it?
To be more precise: I have an activity which contains a listview and each of those items are added using a second activity. When I finish adding the item details in the second activity, I pass that item through a parcelable object (I implemented Parcelable Interface to one of the holders class that DaoGenerator created). The id value of that object can not be null, to pass it with writeLong(id), and receive it with readLong() in my Parcelable methods, so I have to auto-generate the id value, by inserting the current item already in the database.
What I would like to do is: generate those IDs (without inserting the item in the database), pass that item to first activity, and when user decides to save all those items from the list, I would add all of them to database in a single transaction.
some sample code I have atm:
public class Question implements Parcelable {
private Long id;
private String questionContent;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public Question() {
}
public Question(Long id) {
this.id = id;
}
public Question(Long id,String questionContent) {
this.id = id;
this.questionContent = questionContent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// KEEP METHODS - put your custom methods here
// begin Parcelable implementation
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(questionContent);
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
public Question createFromParcel(Parcel in) {
return new Question(in);
}
public Question[] newArray(int size) {
return new Question[size];
}
};
private Question(Parcel in) {
id = in.readLong();
questionContent = in.readString();
}
// end Parcelable implementation
// KEEP METHODS END
}
and this is how I create and send the item to list:
Question questionHolder = new Question(
null, etItemContent.getText().toString() .trim(),);
Log.d(LOG_TAG, "question id = "
+ questionHolder.getId());
// inserting it here, would auto-generate the ID I required,
// but I would like to do that to all of the items in the first Activity (containing the list of all of the items)
// questionDao.insert(questionHolder);
Log.d(LOG_TAG, "question id = "
+ questionHolder.getId());
// add item to intent
Bundle b = new Bundle();
b.putParcelable(IMPORTANCE_TAG, questionHolder);
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK, intent);
QuestionItemActivity.this.finish();
I would not suggest this as it create too much tight coupling.
a couple options that comes to my mind:
If a field is nullable, I would suggest adding another flag to parcelable to denote if that field is null or not.
so when writing
if(id == null) {
out.writeByte((byte)0);
} else {
out.writeByte((byte)1);
out.writeLong(id);
}
and when reading
boolean hasId = in.readByte() == 1;
if(hasId) {
id = in.readLong();
}
Another option, since db ids start from 1, you can set id to 0 and handle this logically. so when you receive the object in your first activity, you can check the id and set to null if it is 0.
Yes there's a mean to do that, if you're using an ORM, this would be easy as !##%.
All you have to do for example is:
Get the sequence name that generates the ID (there's always one even if you didn't create it manually).
Create an SQL query in your code for example :
session.createSQLQuery( "SELECT nextval( 'mySequenceName' )");
Then execute the query to retrieve the unique ID.
I hope this will help.
Cheers

Android Listview : Display Item details onItemClic

I have been working on a e-commerce application for a while, and now I have a ListView that displays a list of products - Each products = 1 ImageView and some TextViews-.
I set an onItemClick listener on that ListView, the event that I want to occur when I click on one of that listView products is, Start the i new Activity, 'ProductDetails' that displays more informations on the product I have clicked on.
I have already asked this question somewhere else, but I didn't get a clear answer,
They said that I have to create a new 'list\details' project, but this can't happen now, as I've working on that project for like 20 days, and can't start all over again.
You can't send a TextView or an ImageView from Activity to Activity. what you can send instead is it's contents. so for example if you want to pass the information from the TextView you will need to pass the source String that is displayed there (Or extract the String from the TextView) you do that by passing a Bundle from the calling Activity to the invoked one or simply by putting it as an Extra:
intent.putExtra("string name", value);
and in the following Activity you get this data:
Intent intent = getIntent();
bundle = intent.getExtras();
bundle.getString("string name");
Then in the following Activity create a TextView with the passed String.
Same way is handled with the ImageView by passing the it's path.
Sorry I have posted an answer but was only a link to another site so it was deleted :-(
Here I come again with what could be the solution:
In your first activity, you need to use intent.putExtra(...) and retrieve the datas in your second activity with intent.getExtra(...).
Example activity 1:
i.putExtra("title01", yourDataCollection.get(position).get(KEY_TITLE01));
Activity 2 :
this.title01 = i.getStringExtra("title01");
There is a full project here : http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text
Android uses intent to allow Activitys to interact. If you want to show a details of product, probably you have a Class that describe that product and with the information it holds you fill up your ListView. If you let the class Product implements Serializable you can use Intent.putExtra(String name, Serializable obj), to pass the Product description between Activitys. In ProductDetailsActivity you can use:
getIntent(). getSerializableExtra (String name)
to retrive the serializable you put inside the Intent you created to start ProductDetailsActivity
If you pass your model class around many times using Intent, perhaps it could implement the Parcelable interface for convenience. For example:
class ProductData implements Parcelable {
protected String name;
protected String someOtherData;
public static final Parcelable.Creator<ProductData> CREATOR = new Parcelable.Creator<ProductData>() {
#Override
public ProductData createFromParcel(Parcel source) {
return new ProductData(source);
}
#Override
public ProductData[] newArray(int size) {
return new ProductData[size];
}
};
public ProductData(){
}
public ProductData(Parcel source){
readFromParcel(source);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(someOtherData);
}
public void readFromParcel(Parcel source){
name = source.readString();
someOtherData = source.readString();
}
// Here goes the rest of your model code
}
Then you can easily pass the object to another Activity using an Intent:
intent.putExtra("product", productData);
and later get the data using
ProductData productData = intent.getParcelableExtra("product");

sorting custom object array on two fields

I'm sorting an array of custom objects (ListData[]) on two fields. I want it to be sorted by theme, and them by name. I thought i made a nice comparator in the custom object class and that i could use Arrays.sort(ld) to make my code working and sorting my array. But apparently im doing something wrong...
my custom object:
public class ListData implements Comparable<ListData>{
public int venueID;
public String name;
public String photoUrl;
public String tip;
public String theme;
#Override
public int compareTo(ListData ld0) {
return this.venueID- ld0.venueID;
}
public static Comparator<ListData> ListDataThemeAndNameComparator = new Comparator<ListData>() {
#Override
public int compare(ListData ld1, ListData ld2) {
String compareTheme1 = ld1.theme.toUpperCase();
String compareTheme2= ld2.theme.toUpperCase();
String compareName1 = ld1.name.toUpperCase();
String compareName2= ld2.name.toUpperCase();
//ascending
int comp = compareTheme1.compareTo(compareTheme2); // comp themes
if(comp==0){ // same theme
comp= compareName1.compareTo(compareName2); // compare names
}
return comp;
}
};
}
And in my main activity i have:
ListData ld[]= new ListData[jsonResponse.size()];
(some code filling my ListData array)
Arrays.sort(ld, ListData.ListDataThemeAndNameComparator); // compare by theme and then by name
Does anyone know what i'm doing wrong?
I edited my code But still it fails, now on a nullpointerexception on the compareTheme1 = ld1.theme.toUpperCase();. But i am sure my array is not empty, i logged it the line before sorting it and its filled with about 500 items.
Your ListData object should implements Comparable not Comparator interface.
EDIT:
To make things clear, you can sort an array by Array.sort(). To make custom sort, you can specify your comparator in Array.sort(), if you don't do that, array will be sorted in natural order which you can define by implementing Comparable interface. So you have two options how to custom sort:
by using custom comparator and specifying it in Array.sort()
by implementing Comparable interface to your items
I would suggest you to go with implementing Comparable. You save memory by not creating new comparator objects and Comparator is useful if you are comparing objects of different types which is not your case.

How can I read xml data from internet in Android ListView?

How can i read the categories education and work in a android ListView? Now on clicking any category its childs Google,IBM,etc. are shown in a new screen. How can it be implemented?
You have to follow the following steps to get your work done
Step-1 You need to parse the xml. see http://xmlpull.org/v1/src/java/samples/MyXmlPullApp.java
Step-2 Create a bean class for Category. means all the category related data will be stored in that bean class
Step-3 Create an arraylist of that bean class
Step-4 Create a list view using that arraylist ( the list view will display only name of categories)
Step-5 Implement on itemclik listener on the list view. get the position. retrieve the data pass to the intent call second activity
Step-6 In the second activity create a listview and display the data whatever is passed by first activity
NetworkBean
public class NetWorkBean {
String name;
int id;
NetWorkBean(String name, int id){
this.name = name;
this.id = id;
}
public String getNetWorkName(){
return name;
}
public int getNetWorkId(){
return id;
}
}
CategoryBean
import java.util.ArrayList;
public class CategoryBean {
String name;
ArrayList<NetWorkBean> networkList;
public CategoryBean(String name, ArrayList<NetWorkBean> networkList){
this.name = name;
this.networkList = networkList;
}
public String getNetWorkName(){
return name;
}
public ArrayList<NetWorkBean> getNetWorkList(){
return networkList;
}
}
you can see this
http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
After parsing the data set it to the list view.Then in the list onclick event implement
what you need.You can call an another activity and sending the corresponding address in a
bundle then retrieving that on the activity you can show in webview

Categories

Resources