Custom ListView doesn't display entries - android

I've created a custom ListView with a BaseAdapter. When I start the activity, I see an empty ListView with no entries.
Here's the Adapter class:
public class SpeiseplanListAdapter extends BaseAdapter {
private ArrayList<SpeiseplanEntry> data;
private Context context;
public SpeiseplanListAdapter(ArrayList<SpeiseplanEntry> data, Context context) {
this.data = data;
this.context = context;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
TextView day = (TextView) entry.findViewById(R.id.text_day);
TextView date = (TextView) entry.findViewById(R.id.text_date);
TextView firstDish = (TextView) entry.findViewById(R.id.text_first_dish);
TextView secondDish = (TextView) entry.findViewById(R.id.text_second_dish);
SpeiseplanEntry current = data.get(position);
day.setText(current.getDay());
date.setText(current.getDate());
firstDish.setText(current.getFirstDish());
secondDish.setText(current.getSecondDish());
return entry;
}
}
The OnCreate() method :
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, getApplicationContext());
listView.setAdapter(adapter);
...
}
The Layout of a single row:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp">
<TextView
android:text="MO"
android:layout_width="wrap_content"
android:layout_height="35dp" android:id="#+id/text_day" android:fontFamily="monospace"
android:typeface="monospace" android:textSize="30sp"
android:textColor="#color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp" android:layout_marginLeft="28dp"
app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="28dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="#+id/text_first_dish"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp"
app:layout_constraintLeft_toRightOf="#+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:textAlignment="textStart"
android:text="firstDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:layout_width="250dp"
android:layout_height="38dp"
android:id="#+id/text_second_dish"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
app:layout_constraintLeft_toRightOf="#+id/text_day" android:layout_marginLeft="40dp"
android:textSize="16sp" android:text="secondDish"
android:gravity="center_vertical"
android:layout_marginStart="40dp"/>
<TextView
android:text="21.09."
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/text_date"
android:layout_marginLeft="16dp" app:layout_constraintLeft_toLeftOf="parent" android:textSize="18sp"
android:fontFamily="monospace" android:typeface="monospace" android:textColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="32dp"
android:layout_marginStart="16dp"/>
The layout of the Activity:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.gymoth.goapp.SpeiseplanActivity" tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/list_speiseplan"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" android:divider="#color/divider" android:dividerHeight="1dp"/>
Is there a problem with the ConstraintLayout I used for the single row? Or is the Adapter the problem? Any help is appreciated.

first put a condition at inflate time...
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View entry = inflater.inflate(R.layout.layout_list_entry, parent, false);
}
then use of holder in adapter..

Use like this
protected void onCreate(Bundle savedInstanceState) {
...
ListView listView = (ListView) findViewById(R.id.list_speiseplan);
SpeiseplanListAdapter adapter = new SpeiseplanListAdapter(entries, YourActivityName.this);
listView.setAdapter(adapter);
...
}

Related

trying to iterate spinners RecycleView by clicking a button miss few items

PLEASE READ THE EDIT SECTION BELLOW
I have a Spinners Recyclerview (list) I want to iterate after clicking a button(finishBtn).
in order to get the selected results entered by the user, I try to iterate the list using list.findViewHolderForAdapterPosition.
The size of the list is supposed to be 6 (by viewing Recyclerview I can see all 6) but when I try to iterate, the size is smaller than what actually there is. (instead of 6 the list.getChildCount() is only 3).
Fragment:
public class FragmentUserDetails extends Fragment {
Button finishBtn;
LinkedHashMap<String, ArrayList<String>> detailsQuestions;
DetailsViewModel detailsViewModel;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
detailsViewModel = new ViewModelProvider(this).get(DetailsViewModel.class);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_details, container, false);
detailsQuestions =getQuestions();
Log.d("TAG", "size: "+detailsQuestions.size());
RecyclerView list = view.findViewById(R.id.details_list_rv);
list.setHasFixedSize(true);
list.setLayoutManager(new LinearLayoutManager(getContext()));
DetailsAdapter detailsAdapter = new DetailsAdapter(this);
list.setAdapter(detailsAdapter);
finishBtn = view.findViewById(R.id.userDetails_next_btn);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
finishBtn.setOnClickListener(v -> {
Log.d("TAG", ""+list.getChildCount());
for (int i = 0; i < list.getChildCount(); i++) {
DetailsHolder holder = (DetailsHolder) list.findViewHolderForAdapterPosition(i);
if (holder!=null) {
String question = (String) detailsQuestions.keySet().toArray()[i];
Log.d("TAG", "The answer for " +question+"is :"+ holder.questionSp.getSelectedItem().toString());
}
}
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
getActivity().finish();
});
return view;
}
interface OnItemClickListener {
void onItemClick(int position);
}
public LinkedHashMap<String, ArrayList<String>> getQuestions() {
ArrayList<Detail> arrayList = detailsViewModel.getDetails();
detailsQuestions = new LinkedHashMap<>();
for (Detail detail : arrayList) {
detailsQuestions.put(detail.getQuestion(), detail.getAnswers());
}
return detailsQuestions;
}
}
Adapter:
class DetailsAdapter extends RecyclerView.Adapter<DetailsHolder> {
private final FragmentUserDetails fragmentUserDetails;
FragmentUserDetails.OnItemClickListener listener;
public DetailsAdapter(FragmentUserDetails fragmentUserDetails) {
this.fragmentUserDetails = fragmentUserDetails;
}
public void setOnItemClickListener(FragmentUserDetails.OnItemClickListener listener) {
this.listener = listener;
}
#NonNull
#Override
public DetailsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = fragmentUserDetails.getLayoutInflater().inflate(R.layout.details_list_row, parent, false);
DetailsHolder detailsHolder = new DetailsHolder(view, listener);
return detailsHolder;
}
#Override
public void onBindViewHolder(#NonNull DetailsHolder detailsHolder, int position) {
if(fragmentUserDetails.detailsQuestions.size()!=0) {
String detail = (String) fragmentUserDetails.detailsQuestions.keySet().toArray()[position];
fragmentUserDetails.detailsQuestions.get(detail).add(0,detail);
String[] array = fragmentUserDetails.detailsQuestions.get(detail).toArray(new String[0]);
ArrayAdapter adapter = new ArrayAdapter<String>(fragmentUserDetails.getContext(), R.layout.spinner_item, array){
#Override
public boolean isEnabled(int position) {
if (position == 0) {
return false;
} else {
return true;
}
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.WHITE);
}
return view;
}};
detailsHolder.questionSp.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_item);
}
}
#Override
public int getItemCount() {
return fragmentUserDetails.detailsQuestions.size();
}
}
Holder:
class DetailsHolder extends RecyclerView.ViewHolder {
Spinner questionSp;
public DetailsHolder(#NonNull View itemView, FragmentUserDetails.OnItemClickListener listener) {
super(itemView);
questionSp = itemView.findViewById(R.id.listrow_question_sp);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition();
listener.onItemClick(pos);
}
});
}
}
details_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true">
<Spinner
android:id="#+id/listrow_question_sp"
android:layout_width="409dp"
android:layout_height="90dp"
android:layout_weight="1"
android:clickable="true"
android:drawablePadding="14dp"
android:focusable="true"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:spinnerMode="dialog"
android:textSize="18sp"
android:theme="#style/mySpinnerItemStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_user_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".details.FragmentUserDetails">
<ImageView
android:id="#+id/userDetails_img_main"
android:layout_width="306dp"
android:layout_height="160dp"
android:layout_marginTop="30dp"
android:src="#drawable/logo__nobg_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/homeScr_text_title"
android:layout_width="350dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:text="Please enter the following details:"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/userDetails_img_main" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/details_list_rv"
android:layout_width="402dp"
android:layout_height="211dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/homeScr_text_title" />
<com.google.android.material.button.MaterialButton
android:id="#+id/userDetails_next_btn"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:fontFamily="sans-serif-black"
android:text="NEXT"
android:textSize="20sp"
app:cornerRadius="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/details_ti" />
<com.google.android.material.button.MaterialButton
android:id="#+id/userDetails_btn_back"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:backgroundTint="#color/black"
android:fontFamily="sans-serif"
android:text="BACK"
android:textSize="20sp"
app:cornerRadius="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/details_ti"
app:strokeColor="#color/primeOrng"
app:strokeWidth="1dp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/details_ti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:hint="map.."
android:textColorHint="#color/white"
app:layout_constraintTop_toBottomOf="#+id/details_list_rv"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/listrow_question_et"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_input"
android:drawablePadding="14dp"
android:fontFamily="sans-serif-light"
android:inputType="text"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/details_ti"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_editor_absoluteX="0dp" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How can I solve this?
EDIT:
By replacing :
list.getChildCount();
with:
list.getAdapter().getItemCount()
I get the exact size (6) and now I understand it is related to the fact RecycleView recycles the views and not keeping them all, so it will always be a maximum of 3 items(in my example).
now, my question is still how could I achieve my target of saving the selected answers in the spinners by clicking finishBtn(I understand that iterating them is not an option because of the same thing, or maybe there is a way?)

RecyclerView that using GridLayoutManager with background makes different heights visible

I've made an adapter with a layout like the one I share below. it went well but, the appearance of each item became untidy.
Here is my code :
public class TableRecyclerAdapter extends RecyclerView.Adapter<TableRecyclerAdapter.ViewHolder> {
private ArrayList<TableModel> data;
private Context context;
private Fragment fragment;
private Dialog dialog;
public TableRecyclerAdapter(Context context, ArrayList<TableModel> data, Fragment fragment, Dialog dialog) {
this.context = context;
this.data = data;
this.fragment = fragment;
this.dialog = dialog;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_table, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.tvTable.setText(data.get(position).getName());
holder.tvState.setText(data.get(position).getStateString());
holder.bg.setBackgroundColor(Color.parseColor(data.get(position).getWarna()));
holder.tvHtml.setText(Html.fromHtml(data.get(position).getShortInfoHtml()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView paid;
private TextView tvTable, tvHtml, tvState;
private ConstraintLayout bg;
public ViewHolder(View itemView) {
super(itemView);
tvTable = itemView.findViewById(R.id.tvTable);
tvHtml = itemView.findViewById(R.id.tvHtml);
bg = itemView.findViewById(R.id.bg);
tvState = itemView.findViewById(R.id.tvState);
paid = itemView.findViewById(R.id.ivPaid);
}
}
}
This is for layout item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bg"
android:layout_width="match_parent"
android:background="#drawable/border_button_green"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:background="#drawable/border_transparent_table"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tvState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="This text"
android:textColor="#color/color_primary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/tvTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="25dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:text="7"
android:textColor="#color/color_primary"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvHtml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:paddingBottom="12dp"
android:text="Example Text"
android:textColor="#color/color_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvTable" />
<ImageView
android:id="#+id/ivPaid"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:src="#drawable/paid"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
It makes my recyclerview have different heights depending on the text.
I want the items to be the same height per row (maximum height in the row)
Sorry for my english. I hope someone can help me. Thank you

Andriod Studio ListView doesn't update itself

I have ListView and my own adapter. ListView item consists of TextView, icon image and image "trash can"(simply "trash"). The problem is when i click on any item it should be deleted but in fact the last item of ListView disappears. I checked ArrayList and the item I clicked on is deleted from there.
Also when i try to add new item the program just restore previous items and if all initial items are displayed and i try to add new the program crashes. Although in ArrayList items are added correctly.
Activity code
public class MyItemsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_items);
myItemsList = findViewById(R.id.ListOfMyItems);
myItemsAdapter.arrayList.add(new MyItems("Keys", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Wallet", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Physics", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("umovnyy vasyl", R.drawable.plus));
myItemsAdapter.arrayList.add(new MyItems("Smartphone)", R.drawable.plus));
for (int i = 0; i<100; i++){
arrayList.add(new MyItems("Max)", R.drawable.plus));
}
myItemsList.addFooterView(new ImageView(this));
myItemsList.addHeaderView(new ImageView(this));
myItemsList.setAdapter(myItemsAdapter);
for (int i = 0; i<100; i++){
myItemsAdapter.arrayList.remove((5));
}
arrayList.add(new MyItems("Maxx)", R.drawable.plus));
myItemsAdapter.notifyDataSetChanged();
myItemsAdapter.notifyDataSetInvalidated();
myItemsList.setOnItemClickListener((parent, view, position, id) -> {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_SHORT)
.show();
myItemsAdapter.arrayList.remove((position-1));
myItemsAdapter.notifyDataSetChanged();
myItemsList.invalidateViews();
myItemsAdapter.notifyDataSetInvalidated();
});
}
ListView myItemsList;
ArrayList<MyItems> arrayList = new ArrayList<>();
MyItemsAdapter myItemsAdapter = new MyItemsAdapter(MyItemsActivity.this, arrayList);
public void addNewItem(View view) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MyItemsActivity.this);
dialog.setView(R.layout.add_new_item);
AlertDialog alertDialog = dialog.create();
ListView a = alertDialog.getListView();
alertDialog.show();
myItemsList.invalidateViews();
}
public void addNewItemButtonClicked(View view){
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.add_new_item, null);
EditText nameOfItem = v.findViewById(R.id.nameOfItem);
arrayList.add(new MyItems(nameOfItem.getText().toString(), R.drawable.forwardbutton));
myItemsAdapter.notifyDataSetChanged();
myItemsAdapter.notifyDataSetInvalidated();
}
public void notificationClicked(View view){
Toast.makeText(getApplicationContext(),
"Notification clicked", Toast.LENGTH_SHORT)
.show();
}
public void openMenu(View view) {
Intent intent = new Intent(getBaseContext(), MenuActivity.class);
startActivity(intent);
}
}
Adapter
class MyItemsAdapter extends BaseAdapter {
ArrayList<MyItems> arrayList;
Context context;
public MyItemsAdapter(Context context, ArrayList<MyItems> arrayList) {
this.arrayList=arrayList;
this.context=context;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
observer.onChanged();
super.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
super.unregisterDataSetObserver(observer);
observer.onInvalidated();
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public MyItems getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyItems myItems = arrayList.get(position);
if(convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.example_of_list_of_items, parent, false);
TextView nameOfItem = convertView.findViewById(R.id.exampleTextView);
ImageView itemCircle = convertView.findViewById(R.id.exampleImageViewIcon);
ImageView trash = convertView.findViewById(R.id.exampleImageViewTrash);
nameOfItem.setText(myItems.itemName);
itemCircle.setImageResource(myItems.itemImage);
trash.setImageResource(R.drawable.trash);
trash.setOnClickListener(v -> {
Toast.makeText(context, "lol "+position, Toast.LENGTH_SHORT).show();
arrayList.remove(position);
notifyDataSetChanged();
notifyDataSetInvalidated();
});
}
return convertView;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return arrayList.size();
}
#Override
public boolean isEmpty() {
return false;
}
}
MyItems code
class MyItems {
String itemName;
int itemImage;
public MyItems(String itemName, int image) {
this.itemName = itemName;
this.itemImage = image;
}
}
Example of item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/exampleTextView"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="85dp"
android:fadingEdge="horizontal|vertical"
android:layoutDirection="inherit"
android:lineSpacingExtra="14sp"
android:text="TextView"
android:textAlignment="textStart"
android:textColor="#000000"
android:textSize="20sp"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="8sp"
app:layout_constraintEnd_toStartOf="#+id/exampleImageViewTrash"
app:layout_constraintStart_toEndOf="#+id/exampleImageViewLeft"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/exampleImageViewLeft"
android:layout_width="25dp"
android:layout_height="23dp"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher_round" />
<ImageView
android:id="#+id/exampleImageViewTrash"
android:layout_width="49dp"
android:layout_height="47dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:cropToPadding="true"
android:fitsSystemWindows="true"
android:focusableInTouchMode="false"
android:focusable="false"
android:clickable="false"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/trash" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="70dp"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code of main screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="99dp">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_begin="127dp"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/myItemsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_items"
android:textColor="#000000"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/menuImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:clickable="true"
android:focusable="true"
android:onClick="openMenu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu" />
<ImageView
android:id="#+id/notificationImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="true"
android:focusable="true"
android:onClick="notificationClicked"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/notification" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListOfMyItems"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="100dp"
android:alwaysDrawnWithCache="true"
android:animateLayoutChanges="true"
android:choiceMode="singleChoice"
android:clickable="false"
android:clipToPadding="false"
android:descendantFocusability="beforeDescendants"
android:dividerHeight="1dp"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:isScrollContainer="true"
android:layoutDirection="ltr"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:listfooter="#android:layout/simple_list_item_1">
</ListView>
<LinearLayout
android:layout_width="409dp"
android:layout_height="70dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:focusable="auto"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ListOfMyItems">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<TextView
android:id="#+id/addNewItemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-1dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="70dp"
android:clickable="true"
android:focusable="true"
android:onClick="addNewItem"
android:text="#string/add_new_item"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/plusImageView"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/plusImageView"
android:layout_width="67dp"
android:layout_height="74dp"
android:layout_marginStart="56dp"
android:clickable="false"
android:focusable="true"
android:onClick="addNewItem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/plus" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Code for AlertDialog adding new item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#FFFFFF">
<EditText
android:id="#+id/nameOfItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:ems="10"
android:hint="Enter name"
android:inputType="textPersonName"
android:singleLine="true"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="9dp"
android:background="#android:drawable/btn_default_small"
android:onClick="addNewItemButtonClicked"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/nameOfItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Looks like your data is not properly updated inside the Adapter's getView method.
You should try to update the item inside the Adapter when contentView is not null.
For performance reason, there is a pattern called ViewHolder used inside the adapter. Refer to Android listview using ViewHolder for more info. Just create Holder when contentView is null and set the data when it isn't. Hope it helps.
P.S. ListView is considered deprecated. Use RecyclerView instead. It has already built-in ViewHolder pattern and is more flexible

OnItemClick never being called for button in ListView

I'm trying to implement Onclicklistener for button inside Listview item but unable to get OnitemClick method called in listview.
I got the following code snippet from stack overflow
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
( break point)
click++;
}
});
The break point never goes off when i click the button.
Activity Code
public class MainActivity extends AppCompatActivity {
long click=0;
String[] aOrderID = {"1-AA-355433", "2-AA-456474", "3-AA-43546464", "4-A-446464E6","5-A-43555" };
String[] aOrderType = {
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY"
};
double[] aPrice={3353.33, 4567.77, 335.66, 535.77, 678.67};
double[] aVOL={93353.33, 94567.77, 9335.66, 9535.77, 9678.67};
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter whatever = new CustomListAdapter(this,
aOrderID, aOrderType,aPrice, aVOL );
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(whatever);
////////////////////////
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
// CODE NEVER GETS HER AFTER BUTTON IS CLICKED ON
click++;
}
});
}
}
CUSTOM ADAPTER
public class CustomListAdapter extends ArrayAdapter {
int ted;
//to reference the Activity
private final Activity context;
//to store the animal images
private Integer[] imageIDarray = null;
//List of order ids
private String[] aOrderID = null;
// Type of order id limit buy
private String[] aOrderType = null;
// Price, price to sell of buy at
private double[] aPrice = null;
// Amount of bitcoins to buy or sell
private double[] aVol = null;
public CustomListAdapter(Activity context,
String[] aOrderID, String[] aOrderType,
double[] aPrice, double[] aVol) {
super(context, R.layout.row2, aOrderID);
this.context = context;
this.aOrderID = aOrderID;
this.aOrderType = aOrderType;
this.aPrice = aPrice;
this.aVol = aVol;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
return rowView;
}
}
XML FOR LISTVIEW
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView33" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Oder Type "
app:layout_constraintStart_toEndOf="#+id/textView33"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/IDtYPE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limit Order"
app:layout_constraintStart_toEndOf="#+id/textView35"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Volume "
app:layout_constraintStart_toEndOf="#+id/textView34"
app:layout_constraintTop_toBottomOf="#+id/textView35" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type::"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/idOrderID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limmit"
app:layout_constraintStart_toEndOf="#+id/textView40"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView40" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_marginTop="4dp"
android:text="Delete Order"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView34" />
<TextView
android:id="#+id/textView45"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="-------------------------------------------------"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<TextView
android:id="#+id/idPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000.00"
app:layout_constraintStart_toEndOf="#+id/textView43"
app:layout_constraintTop_toBottomOf="#+id/idOrderID" />
<TextView
android:id="#+id/idVol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.9999"
app:layout_constraintStart_toEndOf="#+id/textView37"
app:layout_constraintTop_toBottomOf="#+id/textView35" />
You should use button click event from this method. Try this.
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
Button btn = (Button) rowView.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff.
}
});
return rowView;
}

Personalized listview android studio

(First of all sorry my bad english)
I am doing an app about routes in my town, is like a project of my grade, and I have it done it all but I don't know what is the problem..
My problem is that the ListView that I made from a ViewHolder it doesn't show up when I start the app.
My code is this:
MainActivity:
public class RutasActivity extends AppCompatActivity {
private static final String LOGO = "";
ImageButton btnImgAtras;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rutas);
lv = (ListView)findViewById(R.id.lvRutas);
RutasParser parser = new RutasParser(this);
if(parser.parse()) {
Ruta[] rutas = parser.getRutas();
RutaAdapter adapter = new RutaAdapter(this, rutas);
lv.setAdapter(adapter);
Toast.makeText(this, "Enseñando", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No se pudieron obtener los datos de los países", Toast.LENGTH_SHORT).show();
}
btnImgAtras = (ImageButton)findViewById(R.id.imgBtnAtras);
btnImgAtras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RutasActivity.this, MainActivity.class);
startActivity(i);
}
});
}
}
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RutasActivity"
>
<ImageView
android:layout_width="45dp"
android:layout_height="44dp"
app:srcCompat="#drawable/escudoazulsinfondo"
android:id="#+id/imageView5"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:contentDescription="" />
<TextView
android:text="#string/Rutas_Javea"
android:textSize="20sp"
android:textColor="#color/Blacky"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_marginTop="32dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
app:srcCompat="#drawable/atras"
android:id="#+id/imgBtnAtras"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:background="#android:color/transparent"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" />
<ListView
android:layout_width="312dp"
android:layout_height="358dp"
android:id="#+id/lvRutas"
android:layout_marginStart="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="24dp"
android:layout_marginEnd="24dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="24dp"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="24dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" >
</ListView>
<ImageView
android:layout_width="105dp"
android:layout_height="36dp"
app:srcCompat="#drawable/depturismenofondo"
android:id="#+id/imageView6"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
tools:ignore="ContentDescription" />
</android.support.constraint.ConstraintLayout>
Then, my AdapterView:
public class RutaAdapter extends ArrayAdapter<Ruta> {
Ruta[] rutas;
public RutaAdapter(Context context, Ruta[]rutas) {
super(context, R.layout.listitem_ruta, rutas);
this.rutas = rutas;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder viewHolder;
if(item == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
item = inflater.inflate(R.layout.listitem_ruta, null);
viewHolder = new ViewHolder();
viewHolder.vhKm = (TextView) item.findViewById(R.id.listKm);
viewHolder.vhNombreRuta = (TextView) item.findViewById(R.id.listNombre);
viewHolder.vhDesnivel = (TextView) item.findViewById(R.id.listDesnivel);
viewHolder.vhDificultad = (TextView) item.findViewById(R.id.listDificultad);
viewHolder.vhTiempoTotal = (TextView) item.findViewById(R.id.listTiempo);
item.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)item.getTag();
}
viewHolder.vhKm.setText(String.valueOf(rutas[position].getDistancia()));
viewHolder.vhNombreRuta.setText(rutas[position].getNombre());
viewHolder.vhDificultad.setText(rutas[position].getNivel_dificultad());
viewHolder.vhTiempoTotal.setText(rutas[position].getTiempo_efectivo());
viewHolder.vhDesnivel.setText(String.valueOf(rutas[position].getDesnivel_acumulado_ascenso()));
return item;
}
static class ViewHolder {
TextView vhNombreRuta;
TextView vhKm;
TextView vhDificultad;
TextView vhTiempoTotal;
TextView vhDesnivel;
}
}
My parser:
public class RutasParser {
private Ruta[] rutas;
private InputStream rutasFile;
public RutasParser(Context c) {
this.rutasFile = c.getResources().openRawResource(R.raw.rutas);
}
public boolean parse() {
boolean parsed = false;
rutas = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(rutasFile);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("country");
rutas = new Ruta[items.getLength()];
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
int rutaId = Integer.parseInt(item.getAttributes().getNamedItem("code").getNodeValue());
String rutaNombre = item.getAttributes().getNamedItem("name").getNodeValue();
String rutaDescrip = item.getAttributes().getNamedItem("description").getNodeValue();
double rutaDistance = Double.valueOf(item.getAttributes().getNamedItem("distance").getNodeValue());
String rutaHora = item.getAttributes().getNamedItem("horas").getNodeValue();
String rutaDif = item.getAttributes().getNamedItem("dificultad").getNodeValue();
int rutaElev = Integer.valueOf(item.getAttributes().getNamedItem("elevacion").getNodeValue());
rutas[i] = new Ruta(rutaId, rutaNombre, rutaDescrip, rutaDistance, rutaDif, rutaHora, rutaElev);
}
parsed = true;
} catch (ParserConfigurationException pce) {
Log.e("CountryParser", "ParserConfigurationException: "+pce.getLocalizedMessage());
} catch (Exception e) {
Log.e("CountryParser", "Unknown Exception: "+e.getLocalizedMessage());
}
return parsed;
}
public Ruta[] getRutas() {
return this.rutas;
}
}
And the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00.00km"
android:textSize="30sp"
android:id="#+id/listKm"
android:layout_gravity="center_horizontal|center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingStart="3dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="nombre de ruta"
android:id="#+id/listNombre"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listDificultad"
android:text="dificultad"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listTiempo"
android:text="000000"
android:paddingRight="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listDesnivel"
android:text="000000"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
At first place I don´t see any error but it doesn´t show up in my app..
If you can help me, I would be grateful..
Thanks
Photo of what I tell you

Categories

Resources