It's my first time face to face with Gson. My question will be a long one, hope you'll understand and I there are more problems wich got me stuck for about 1 week.
So, I have a ListView where I get selected items from another Activtity. Under this ListView I have a Button where the seller save items to FireBase, bought by customer.
Now the problem is, each time when seller save bought items to FireBase I want to enumerate(like an increment) each customer and give a customer number. And show the number of customer in TextView in top of the page where number 1 is in the picture. But next day enumeration have to starts from 1. So every day has to starts from 1.
In this ListView, I am counting selected items and calculate sum of prices.
But second problem is I have no idea how to send customer number, counted items and sum of items to another ListView with a card view. I want to put it inside of Gson. I tried something, but I'm completely lost. And my code looks really messy in this moment. Hope you'll understand.
Here is the code where I get selected items and display counted items and sum of items with Toast :
int totalPrice = 0;
for(VanzatorProduse v : Util.getInstance().getVanzatorProduse())
{
int vPrice = Integer.parseInt(v.getPret());
totalPrice = totalPrice + vPrice;
}
Toast.makeText(getApplicationContext(), "Selected products : " + listaProdusePreview.getAdapter().getCount() +"\n"+"Total sum : " + totalPrice , Toast.LENGTH_LONG).show();
Intent sumaProduse = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
sumaProduse.putExtra("sumaProduse", totalPrice);
startActivity(sumaProduse);
String produseSelectate = String.valueOf(listaProdusePreview.getAdapter().getCount());
Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
intent.putExtra("produseSelectate", produseSelectate);
startActivity(intent);
Util.getInstance().getVanzatorProduse().removeAll(Util.getInstance().getVanzatorProduse());
Object class for customer number, counted items and sum of items :
public class Card {
private Integer numarCumparator;
private String listaProduse;
private Integer sumaProduse;
public Card(Integer numarCumparator, String listaProduse, Integer sumaProduse) {
this.numarCumparator = numarCumparator;
this.listaProduse = listaProduse;
this.sumaProduse = sumaProduse;
}
public Integer getNumarCumparator() {
return numarCumparator;
}
public void setNumarCumparator(Integer numarCumparator) {
this.numarCumparator = numarCumparator;
}
public String getListaProduse() {
return listaProduse;
}
public void setListaProduse(String listaProduse) {
this.listaProduse = listaProduse;
}
public Integer getSumaProduse() {
return sumaProduse;
}
public void setSumaProduse(Integer sumaProduse) {
this.sumaProduse = sumaProduse;
}
Here is code for listview adapter :
public class CardArrayAdapter extends ArrayAdapter<Card> {
private static final String TAG = "CardArrayAdapter";
private List<Card> cardList = new ArrayList<Card>();
static class CardViewHolder {
TextView line1;
TextView line2;
TextView line3;
}
public CardArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public void add(Card object) {
cardList.add(object);
super.add(object);
}
#Override
public int getCount() {
return this.cardList.size();
}
#Override
public Card getItem(int index) {
return this.cardList.get(index);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CardViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_card, parent, false);
viewHolder = new CardViewHolder();
viewHolder.line1 = (TextView) row.findViewById(R.id.txtViewNumarCumparator);
viewHolder.line2 = (TextView) row.findViewById(R.id.listaProduse);
viewHolder.line3 = (TextView) row.findViewById(R.id.sumaProduse);
row.setTag(viewHolder);
} else {
viewHolder = (CardViewHolder)row.getTag();
}
Card card = getItem(position);
viewHolder.line1.setText(card.getNumarCumparator());
viewHolder.line2.setText(card.getListaProduse());
viewHolder.line3.setText(card.getSumaProduse());
return row;
}
Layout for card view :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:descendantFocusability="afterDescendants"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp">
<TextView
android:id="#+id/txtViewNumarCumparator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="NumarCumparator" />
<TextView
android:id="#+id/listaProduse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtViewNumarCumparator"
android:layout_marginTop="10dp"
android:text="NumarProduse" />
<TextView
android:id="#+id/sumaProduse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="SumaProdsue" />
Here, is my attempt for Gson, I put it in a JSONManager class :
public class JSONManager {
private static JSONManager instance = null;
protected JSONManager() {
// Exists only to defeat instantiation.
}
public static JSONManager getInstance() {
if (instance == null) {
instance = new JSONManager();
}
return instance;
}
//Create list of Cards because we need to show list for date selected
public List<Card> readJSON(String dateFromCalendar) throws JSONException {
String JSON = SharedPreference.getString(Util.get(), SharedPreference.APP_DATA, SharedPreference.JSON_DATA, "");
String numeVanzator = SharedPreference.getString(Util.get(), SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR,"");
JSONObject jsonObj = new JSONObject(JSON);
JSONArray contacts = jsonObj.getJSONArray("jsonData");
List<Card> listCard = null;
Card card = null;
listCard = new ArrayList<>();
for (int i =0; i< contacts.length();i++) {
JSONObject c = contacts.getJSONObject(i);
String data = c.getString("data");
if (data.equals(dateFromCalendar)){
String numeVanzato = c.getString("numeVanzator");
if (numeVanzato.equals(numeVanzator)){
Integer numarClient = c.getInt("numarClient");
JSONObject detaliiCos = c.getJSONObject("detaliiCos");
Integer sumaProduse = detaliiCos.getInt("sumaProduse");
String produseselectate = c.getString("produseSelectate");
card = new Card(numarClient, produseselectate, sumaProduse);
listCard.add(card);
}
}
}
return listCard;
} }
Code for activity where i tried to get data inside from Gson.
#Override
protected void onResume() {
super.onResume();
c = Calendar.getInstance();
String numeVanzator = SharedPreference.getString(this, SharedPreference.USER_DATA, SharedPreference.NUME_VANZATOR, "");
if (!numeVanzator.equals("")) {
textViewNumeVanzator.setText(numeVanzator);
}
String jsonData = SharedPreference.getString(getApplicationContext(), SharedPreference.APP_DATA, SharedPreference.JSON_DATA, "");
System.out.println("JSON data" + jsonData);
textViewDataCurenta.setText(getDate(c.getTimeInMillis()));
cardArrayAdapter = new CardArrayAdapter(this, R.layout.list_item_card);
Card card = null;
List<Card> listCard = new ArrayList<Card>();
c = Calendar.getInstance();
try {
String date = getDate(c.getTimeInMillis());
listCard = JSONManager.getInstance().readJSON(date);
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i <= listCard.size()-1; i++) {
card = listCard.get(i);
cardArrayAdapter.add(card);
}
listViewVanzatorActivity.setAdapter(cardArrayAdapter);
}
Right now, everything is working perfectly but when click on save Button nothing appears in next activity and no errors.
Hope, you understand my question, I really need your help. Thank you !!
In your code you created two times Intent for call activity that is not good way.
just remove second intent from this
Intent sumaProduse = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
sumaProduse.putExtra("sumaProduse", totalPrice);
startActivity(sumaProduse);
Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
intent.putExtra("produseSelectate", produseSelectate);
startActivity(intent);
now create new bundle and add pass to this with key like this:
Intent intent = new Intent(FormCumparareActivity.this, VanzatorActivity.class);
Bundle bundle = new Bundle();
bundle.put("key",totalPrice);
bundle.put("key",produseSelectate);
intent .putExtras(bundle)
startActivity(intent);
you are getting list from JSON which are not store in shared preferences.
hope you are get it my point.
Related
I'm Very new to Java and android... I'm Try to create an ListView using BaseAdapter List being created successfully i have a EditText along with button for each list item but the real problem is when i put some data into editText Field and scroll down to change value of last list item then i go back to the top it refreshes the data to default value it doesn't contain the value which was entered by user before scrolling down
My BaseAdaper Code
class CoustomAdptr extends BaseAdapter{
String[] dates;
Integer[] inventory;
Integer totalrooms;
public CoustomAdptr(RoomFragment roomFragment, String[] dates, Integer[] inventory, Integer totalrooms) {
this.dates = dates;
this.inventory = inventory;
this.totalrooms = totalrooms;
}
#Override
public int getCount() {
return dates.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.inventory_listview,null);
TextView textView = (TextView) view.findViewById(R.id.roomListViewText);
final EditText editText = (EditText) view.findViewById(R.id.roomListInventory);
final Button updateButton = (Button) view.findViewById(R.id.roomListViewInventoryUpdateButton);
if(inventory[i] == 0){
editText.setBackgroundColor(getResources().getColor(R.color.SoldOut));
editText.setTextColor(getResources().getColor(R.color.SoldOutTextColor));
} else if(inventory[i] < totalrooms){
editText.setBackgroundColor(getResources().getColor(R.color.invetory));
editText.setTextColor(getResources().getColor(R.color.invetoryTextColor));
} else if(inventory[i] == totalrooms){
editText.setBackgroundColor(getResources().getColor(R.color.fullInventory));
editText.setTextColor(getResources().getColor(R.color.fullInventoryTextColor));
}
editText.setText(String.valueOf(inventory[i]));
textView.setText(dates[i]);
updateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//String name = editText.getText().toString();
//String name1 = dates[i];
//String name2 = getArguments().getString("room_id");
updateButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done_black_24dp,0,0,0);
//updateButton.setBackgroundColor(getResources().getColor(R.color.SoldOut));
updateButton.setText("Updated");
updateButton.setEnabled(false);
Toast.makeText(getContext(), "Update Inventory Button Clicked", Toast.LENGTH_LONG).show();
}
});
return view;
}
}
This is How Im Passing Data to My Adapter
JSONObject jObj = parentObject.getJSONObject("success");
JSONObject jObj2 = jObj.getJSONObject("data");
JSONArray arrajson = jObj2.getJSONArray("inventories");
String arrayCount = Integer.toString(arrajson.length());
String[] dates = new String[arrajson.length()];
Integer[] inventory = new Integer[arrajson.length()];
Integer totalrooms = new Integer(jObj2.getInt("total_room"));
for (int i=0; i<arrajson.length();i++){
JSONObject jsonObject = arrajson.getJSONObject(i);
dates[i] = jsonObject.getString("date");
inventory[i] = jsonObject.getInt("inventory");
}
CoustomAdptr coustomAdptr = new CoustomAdptr(RoomFragment.this,dates,inventory,totalrooms);
listView.setAdapter(coustomAdptr);
Help Needed :- I Want to retain same visible and Value of edittext as users enters on scroll up or down... i hope i was able to explain my problem clearly
After clicking a button, save it's state in a boolean array or somewhere else. And inside getView method, check if this button was previously clicked or not then setup your view accordingly.
It would be better if you create a model class for rows.
I have an array which contains values like {seat 1,seat 1,seat 1,seat 2, seat 2, seat 3, seat 4,seat 4,seat 4}. ie Left side of image
I need to group them according to its seat values ie. Right side of image.
For given environment seat list array will be: {"1", "-1", "-1", "2", "-1", "3", "4", "-1", "-1"}
currently i am using recyclerview and checking this with below condition:
if(seat_list.get(holder.getAdapterPosition()).equals("-1"))
{
Log.d("aaa", "if :" + seat_list.get(holder.getAdapterPosition()) );
}
else
{
Log.d("aaa", "else :" + seat_list.get(holder.getAdapterPosition()) );
holder.ll_header.setVisibility(View.VISIBLE);
holder.tv_seat_num.setText(seat_list.get(holder.getAdapterPosition()));
}
With above code list is getting destroyed on scrolling the recyclerview.
i already sorted but i want to give common header or heading to all orders with same seat
First of all, I don't think your data ready to be displayed. So it`d be better to prepare it.
And it is better to have some typed data model, that contains info about seats and orders. OOP, you know ?
To keep example simple, I prepare and map data within activity, but it's not very good idea for real world projects.
So, from your description, we have some array, where items could be duplicated and their position in array - it is orderId. So, in other way we want to have some grouped structure with key-value pair, where key -> № of Seat, and value -> collection of orderIds. But it will be much better to have a class, that represent such relations and have info about things described above.
With data like that, it will be not very big problem to just display them in recyclerView.
At the begining, I wanted to place just couple methods here, but after I realized that it may be confused without full code list.
So, here you go all stuff.
This is result of what you will get with listed below code:
= = = = = time to see some code = = = = =
Activity -> (with only RecyclerView in xml layout)
public class SeatsActivity extends AppCompatActivity {
RecyclerView recycler;
SeatsAdapter seatsAdapter;
final String[] orders =
{ "seat 1", "seat 1", "seat 1", "seat 2", "seat 2", "seat 3", "seat 4", "seat 4", "seat 4" };
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seats);
setTitle("Seats example");
recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
final Map<String, List<Integer>> groupedOrders = groupData(orders);
final List<SeatItem> seats = mapToSeatsItems(groupedOrders);
bindToRecycler(seats);
}
private void bindToRecycler(List<SeatItem> seatItems) {
seatsAdapter = new SeatsAdapter(seatItems);
recycler.setAdapter(seatsAdapter);
}
private Map<String, List<Integer>> groupData(final String[] items) {
final Map<String, List<Integer>> grouped = new LinkedHashMap<>();
for (int i = 0; i < items.length ; i++) {
// I took it from your description. I image that orderId is the place of the seat in array
final Integer orderId = i+1;
final String headerKey = items[i];
if (!grouped.containsKey(headerKey)){
List<Integer> list = new ArrayList<>();
list.add(orderId);
grouped.put(headerKey, list);
} else {
List<Integer> list = grouped.get(headerKey);
list.add(orderId);
}
}
return grouped;
}
private List<SeatItem> mapToSeatsItems(Map<String, List<Integer>> groupOrders) {
final List<SeatItem> list = new ArrayList<>(groupOrders.size());
for (String key : groupOrders.keySet()) {
final SeatItem item = new SeatItem(key, groupOrders.get(key));
list.add(item);
}
return list;
}
}
Then SeatItem, that represent data model (POJO, Bean, whatever)
public class SeatItem {
public SeatItem(String header, List<Integer> orderIds) {
this.header = header;
this.orderIds = orderIds;
}
final String header;
final List<Integer> orderIds;
public String getHeader() {
return header;
}
public List<Integer> getOrderIds() {
return orderIds;
}
}
SeatsAdapter ->
public class SeatsAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
final List<SeatItem> dataList;
public SeatsAdapter(final List<SeatItem> items){
dataList = items;
}
#Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) {
final View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seat, parent,
false);
return new SeatsViewHolder(view);
}
#Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int
position) {
final SeatsViewHolder seatsViewHolder = (SeatsViewHolder) holder;
final SeatItem item = dataList.get(holder.getAdapterPosition());
final StringBuilder contentBuilder = new StringBuilder();
for (int i = 0; i < item.getOrderIds().size(); i++) {
// here is not production ready, but for just example will be ok
contentBuilder
.append("order Id: ")
.append(item.getOrderIds().get(i));
if (i + 1 != item.getOrderIds().size()){
contentBuilder.append("\n");
}
}
seatsViewHolder.tvHeader.setText(item.getHeader());
seatsViewHolder.tvContent.setText(contentBuilder);
}
#Override public int getItemCount() { return dataList.size(); }
private static class SeatsViewHolder extends RecyclerView.ViewHolder {
final TextView tvHeader;
final TextView tvContent;
SeatsViewHolder(View itemView) {
super(itemView);
tvHeader = (TextView) itemView.findViewById(R.id.tvHeader);
tvContent = (TextView) itemView.findViewById(R.id.tvContent);
}
}
}
And the last one - item xml, that will be displayed by recycler
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="header! "
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:padding="8dp"
android:background="#cccccc"
/>
<TextView
android:id="#+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="content ! ! ! "
android:layout_gravity="top"
android:gravity="top"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:padding="8dp"
/>
</LinearLayout>
You can sort using Collections.sort method
My getView() method for customized ListViewAdapter is as follows :
public class ListViewAdapter extends BaseAdapter {
Context mContext;
LayoutInflater mInflater;
ArrayList mArray;
ArrayList<Item> mArray2;
DBHelper mydb;
String dbName;
public ListViewAdapter(Context context, LayoutInflater inflater) {
mContext = context;
mInflater = inflater;
mArray = new ArrayList();
mArray2 = new ArrayList<>();
mydb = new DBHelper(mContext);
}
#Override
public int getCount() {
return mArray.size();
}
#Override
public Object getItem(int position) {
return mArray.get(position);
}
public Item getItem2(int position) { return mArray2.get(position); }
#Override
public long getItemId(int position) {
// your particular data set uses String IDs
// but you have to put something in this method
return position;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
// check if the view already exists
// if so, no need to inflate and findViewById again!
if (convertView == null) {
// Inflate the custom row layout from your XML.
convertView = mInflater.inflate(R.layout.list_item, null);
// create a new "Holder" with subviews
holder = new ViewHolder();
holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name);
holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry);
// Taking care of the buttons
holder.editButton = (Button) convertView.findViewById(R.id.button_edit);
holder.deleteButton = (Button) convertView.findViewById(R.id.button_delete);
// hang onto this holder for future recycling
convertView.setTag(holder);
} else {
// skip all the expensive inflation/findViewById
// and just get the holder you already made
holder = (ViewHolder) convertView.getTag();
}
// Set listener on the buttons
holder.editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Edit Button CLicked", Toast.LENGTH_SHORT).show();
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = getItem(position).toString();
int id = mydb.getID(dbName, name);
mydb.deleteItem(dbName, id);
mArray2 = mydb.getAllItemsAsCollection(dbName);
notifyDataSetChanged();
Toast.makeText(mContext, "Item deleted", Toast.LENGTH_SHORT).show();
}
});
// Doing for 2nd case
Item _item = getItem2(position);
String name2 = _item.name;
System.out.println(name2);
String ex = _item.expiry;
System.out.println(ex);
// For the second case
holder.itemNameView.setText(name2);
holder.itemExpiryView.setText(ex);
return convertView;
}
// this is used so you only ever have to do
// inflation and finding by ID once ever per View
private static class ViewHolder {
public TextView itemNameView;
public TextView itemExpiryView;
public Button editButton;
public Button deleteButton;
}
public void updateData2(ArrayList<Item> arrayPassed) {
// update the adapter's data set
mArray2 = arrayPassed;
notifyDataSetChanged();
}
public void setDbName(String dbName){
this.dbName = dbName;
}
}
The DBHelper class function getAllItemsAsCollection() is defined as below :
public ArrayList<Item> getAllItemsAsCollection(String dbName)
{
ArrayList<Item> array_list = new ArrayList<Item>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from " + dbName, null );
res.moveToFirst();
while(res.isAfterLast() == false){
String n = res.getString(res.getColumnIndex(COLUMN_NAME));
String e = res.getString(res.getColumnIndex(COLUMN_EXPIRY));
String c = dbName;
Item _item = new Item(n, e, c);
array_list.add(_item);
res.moveToNext();
}
return array_list;
}
And also, the insertItem() function inside DBHelper is this :
public boolean insertItem (String dbName, String name, String expiry)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("expiry", expiry);
db.insert(dbName, null, contentValues);
return true;
}
I have added a separate class for customizable object handing :
public class Item {
String name;
String expiry;
String category;
Item(String n, String e, String c){
this.name = n;
this.expiry = e;
this.category = c;
}
}
And the addItem() method inside MainActivity.java works like :
public void addItem(final View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
LinearLayout lila1 = new LinearLayout(this);
lila1.setOrientation(LinearLayout.VERTICAL);
final EditText name = new EditText(this);
name.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
final EditText days = new EditText(this);
days.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
TextView text_ex = new TextView(this);
text_ex.setText("In how many days will it expire..");
alert.setTitle("Hello!");
alert.setMessage("What did you buy today..");
lila1.addView(name);
lila1.addView(text_ex);
lila1.addView(days);
alert.setView(lila1);
// Make an "OK" button to save the name
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Grab the EditText's input
String inputName = name.getText().toString();
String daysToExpiry = days.getText().toString();
System.out.println(daysToExpiry);
mydb.insertItem(currentDB, inputName, daysToExpiry);
System.out.println("Worked");
// For 2nd Case
currentList2 = mydb.getAllItemsAsCollection(currentDB);
System.out.println("Random Musings");
itemAdder2.updateData2(currentList2);
// addItemToList(inputName, v);
dialog.dismiss();
}
});
// Make a "Cancel" button
// that simply dismisses the alert
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alert.show();
}
I have initiaized every variable correctly. When I try to run my app, and try to add a new item, the Dialog box just vanishes and there is nothing shown in the layout. I tried with a simple ArrayList<Strings> before and it worked perfectly. That is why I believe there should not be any problem with the .xml Layout. And might be with the ListViewAdapter.updateData2() function. Please Help. Appreciate your patience going through these long pieces of code. If any further info is required, please let me know. Thanks a lot. :)
Forgot to attach the .xml for actual view. This worked perfectly with ArrayList<String>. I have already tested. When I tried to pass complex object, in this case, Item-class object, and correspondingly an ArrayList<Item>, my guess is, I could not write the adapter portion code correctly.
<?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">
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/item_expiry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_name" />
<Button
android:id="#+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/item_expiry"
android:layout_alignParentLeft="true"
android:text="Edit"
android:clickable="true" />
<Button
android:id="#+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/item_expiry"
android:layout_alignParentRight="true"
android:text="Delete"
android:clickable="true" />
</RelativeLayout>
firstly a big thanks to all the experts that answer questions and provide insight into challenges. Your work is appreciated.
Now, i'm a newbie and just started using java and Android....but i'm loving it.
secondly,
do forgive my code. its my very first Android app...moving from 13yrs of vb and vba :) and much of it is modified from user questions here on stackoverflow.
Background:
I have a gridview that i want to display contact data (name and number) from the Call Log.
In order to eliminate duplicate numbers, i loop through the cursor and compare phone numbers after of course, sorting the incoming cursor data by CallLog.Calls.NUMBER + " ASC";
i have also created my own class (ContactObj) that holds the name,number and ID of a contact and i pass this class to an ArrayList. eventually i pass this ArrayList to a custom adapter which uses layout inflater to populate the grid.
The issue:
For some reason, the program runs fine but the first ten contacts are repeated over and over. ie. the total contacts on my phone log are 113 unique. however the grid displays only the first 10 over and over for the total 113.
The question:
perhaps the "old hands" at this could point me on where i'm going wrong? i'm guessing is something to do with my implementation of the custom adapter that feeds the gridview.
as i debug, noticed that the value of mChildrenCount is fixed at 11 which is the count of the cells in the gridview in design mode. for some reason whenever this number is reached the gridview starts from 0 again and that repeats the data. it seems i'm missing some setting to allow the grid to go beyond the cells shown during design. ...any ideas anyone?
Thanks.
here's the code for the main activity
public class CallLogActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
final Context myContext = CallLogActivity.this;
final CustomAdapter mAdapter;
ArrayList<ContactObj> arrToPassToGrid = new ArrayList<ContactObj>();
String strNameHolder = "";
String strCurrentName;
String strNumber;
String strCallDate;
String ID;
int i = 0;
int ComparisonResult;
// first find the grid
GridView callLogGrid = (GridView) findViewById(R.id.callLogGrid);
// next get the contents to display
Long yourDateMillis = System.currentTimeMillis()- (30 * 24 * 60 * 60 * ' `1000);
Time yourDate = new Time();
yourDate.set(yourDate);
String[] YourDateMillistring = {String.valueOf(yourDateMillis)};
String formattedDate = yourDate.format("%Y-%m-%d %H:%M:%S");
Time tempDate;
Cursor Tempcursor;
Cursor cursor;
cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls._ID,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE},
null,
null,
CallLog.Calls.NUMBER + " ASC");
startManagingCursor(cursor);
// intialize nameholder ----will be used to remove duplicate names in
strNameHolder = "";
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
// place contents in variables for easier reading later on;
strCurrentName =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
strNumber = cursor.getString(
cursor.getColumnIndex(CallLog.Calls.NUMBER)).trim();
strCallDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
ID = cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID));
if (strCurrentName == null && strNumber == null) {
ComparisonResult = 0;
} else {
ComparisonResult = strNameHolder
.compareToIgnoreCase(strNumber);
}
if (ComparisonResult != 0) {
ContactObj contList = new ContactObj();
contList.setIndex(i);
contList.setContactName(strCurrentName);
contList.setContactDialledNumber(strNumber);
contList.setContact_ID(ID);
contList.setCallDate(strCallDate);
arrToPassToGrid.add(i, contList);
i++;
}
strNameHolder = cursor.getString(
cursor.getColumnIndex(CallLog.Calls.NUMBER)).trim();
};
};
try {
// Collections.sort(arrToPassToGrid)
mAdapter = new CustomAdapter(this, arrToPassToGrid);
callLogGrid.setAdapter(mAdapter);
} catch (Exception e)
{
Log.d("Kush", e.getMessage());
e.printStackTrace();
}
}
This code is my custom adapter
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ContactObj> mItems;
public CustomAdapter(Context c, ArrayList<ContactObj> items)
{
mContext = c;
mItems = items;
}
public int getCount()
{
return mItems.size();
}
public Object getItem(int position)
{
return mItems.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater li = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.calllog_layout, null);
Log.d("Kush",String.valueOf(getCount()));
TextView txtContactName = (TextView)v.findViewById(R.id.txtContactName);
txtContactName.setText(mItems.get(position).getContactName() );
TextView txtNumber = (TextView)v.findViewById(R.id.txtContactNumber);
txtNumber.setText(mItems.get(position).getContactDialledNumber());
TextView txtDate = (TextView)v.findViewById(R.id.txtCallDate);
txtNumber.setText(String.valueOf(position) );
}
return v;
}
public static String getDate(long milliSeconds, String dateFormat)
{
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
This is the object holding the contact details
public class ContactObj {
private String ContactName;
private String ContactDialledNumber;
private String Contact_ID;
private String CallDate;
public final String getCallDate()
{
return CallDate;
}
public final void setCallDate(String callDate)
{
CallDate = callDate;
}
private int index;
// #return the contactName
public final String getContactName()
{
return ContactName;
}
// #param contactName the contactName to set
public final void setContactName(String contactName)
{
ContactName = contactName;
}
//#return the contactDialledNumber
public final String getContactDialledNumber()
{
return ContactDialledNumber;
}
//#param contactDialledNumber the contactDialledNumber to set
public final void setContactDialledNumber(String contactDialledNumber)
{
ContactDialledNumber = contactDialledNumber;
}
//#return the contact_ID
public final String getContact_ID()
{
return Contact_ID;
}
// #param contact_ID the contact_ID to set
public final void setContact_ID(String contact_ID)
{
Contact_ID = contact_ID;
}
//#return the index
public final int getIndex()
{
return index;
}
//#param index the index to set
public final void setIndex(int index)
{
this.index = index;
}
}
Finally the gridview and layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
android:id="#+id/GridItem"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop" />
<TextView
android:gravity="center_horizontal"
android:id="#+id/txtContactName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/contactName"
android:textColor="#000000" />
<TextView
android:gravity="center_horizontal"
android:id="#+id/txtContactNumber"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/contactNumber"
android:textColor="#000000" />
<TextView
android:gravity="center_horizontal"
android:id="#+id/txtCallDate"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CallDate"
android:textColor="#000000" />
</LinearLayout>
and Gridview
txtContactName.setText(mItems.get(position).getContactName() );
These statements should be outside the if condition. Also check the viewholder usageonce.
Probably you will get the solution.
I have a peculiar problem. I am parsing a restaurant's menu card. They have it in english and in german. I have a class FoodItem as :
public class FoodItem {
private int foodClass;
private String foodType;
private String foodName;
private String foodCost;
private String hauptBeilage;
private String salat;
}
Now, I have an arraylist of fooditems downloaded using Jsoup. I separate the german and english menu using the String foodType.
I want to list german menu at the start. But, I get the english menu appended to the list as well. How should I tackle this?
My downloadThread (Jsoup) is :
public void run()
{
Log.i("downloadThread", "Inside run() - Starting getFoodItems");
getDailyGerman();
getDailyEnglish();
//Sending a message through handler here
}
In my activity, I have:
handler = new android.os.Handler() {
#Override
public void handleMessage(Message msg) {
foodItemAdapter.notifyDataSetChanged();
}
};
If I send a message through handler after getDailyGerman(); then i get a illegalstateexception saying the content of the adapter has changed, but the listview is not updated.
My Adapter code :
public FoodItemAdapter(Context context, int textViewResourceId, ArrayList<FoodItem> FoodItemArg) {
super(context, textViewResourceId, FoodItemArg);
FoodItemAdapter.foodItems = FoodItemArg;
this.setNotifyOnChange(false);
// if(FoodItemAdapter.foodItems == null)
// Log.i("Adapter", "Problem Inside Adapter Constructor");
}
//=========================public methods============================
public static ArrayList<FoodItem> getDailyEnglishFoodItems()
{
ArrayList<FoodItem> returnList = new ArrayList<FoodItem>();
for(FoodItem eachItem : FoodItemAdapter.foodItems)
{
if(eachItem.getFoodClass() == 1)
{
Log.i("Adapter" , "Adding English Daily Food : " + eachItem.getFoodName());
returnList.add(eachItem);
}
}
return returnList;
}
public static ArrayList<FoodItem> getDailyGermanFoodItems()
{
ArrayList<FoodItem> returnList = new ArrayList<FoodItem>();
for(FoodItem eachItem : FoodItemAdapter.foodItems)
{
if(eachItem.getFoodClass() == 2)
{
Log.i("Adapter" , "Adding German Daily Food : " + eachItem.getFoodName());
returnList.add(eachItem);
}
}
return returnList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* Describes each view in the list view.
* Get the question and find the question text, timestamp and the votes.
* Show them in the textview which is a part of the listview.
*/
View v = convertView;
FoodItem foodItem =(FoodItem) FoodItemAdapter.foodItems.get(position);
if(foodItem == null)
{
Log.i("Adapter", "Null Food Item");
}
int colorPos = 0;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.fooditem_row, null);
colorPos = position % colors.length;
}
Please help as I am stuck at this point for 3 days. Thanks.
I had the same issue once I added the items and called
notifyDataSetChanged() in the UI
thread issue solved
From What I understand of your question, you want to have the English items at the top of the list then the German Items. you can do that using Collection.sort method and Using a specific comparator for the task in hand.
For example:
final List<FoodItem> combinedList = getDailyGermanFoodItems();
combinedList.addAll(getDailyEnglishFoodItems());
Collections.sort(compinedList, new FoodItemComparator());
//then you call the handler to update the adapter and the listView
handler.post(new Runnable(){
public void run(){
FoodItemAdapter adapter = new FoodItemAdapter(activity.this, layout, combinedList);
listView.setAdapter(adapter);
}});
where FoodItemComparator:
public class FoodItemComparatorimplements Comparator<FoodItem>{
public int compare(FoodItem item1, item2) {
String foodType1 = item1.getFoodType();
String foodType2 = item2.getFoodType();
if (foodType1.equals(foodType2))
return 0;
if (foodType1.equals("English"))
return 1;
if (foodType2.equals("English))
return -1;
return foodType1.compareTo(foodType2);
}
}
Assuming foodType Value is guaranteed to be German/English only.
Also you will have to have a getter funcion inside your FoodItem Class so the comparator can access it:
Class FoodItem
.......
public String getFoodType(){
return foodType;
}
EDIT
If you want to display each one alone , then store the two lists inside your activity object, then when user select a language (english / german):
FoodItemAdapter adapter = new FoodItemAdapter(activity.this, layout, germanList);
listView.setAdapter(adapter);