I have a custom view adapter with four buttons and a title that populate a ListView.
When a user clicks one of those buttons, I want to retrieve the title associated with that specific adapter.
So far I have tried retrieving the parent and getting the textView but that does not return the correct title.
So...how would I get the specific title affiliated to the adapter that has the button the user clicked on?
I'd be happy to clarify if you need more information.
Here is the layout of the adapter.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/fragment_student_home_classTitle_textView"
android:paddingTop="20dp"
android:layout_toLeftOf="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/linearLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/fragment_student_home_grades"
android:background="#ffff130c" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fragment_student_home_grades_imageButton"
android:src="#drawable/ic_grades_512"
android:background="#00000000" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/fragment_student_home_notification_textEdit"
android:background="#ffff130c" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fragment_student_home_notification_imageButton"
android:src="#drawable/ic_bell_512"
android:background="#00000000" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/fragment_student_home_homework"
android:background="#ffff130c" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fragment_student_home_homework_imageButton"
android:src="#drawable/ic_foundation_book_bookmark_simple_black_512x512"
android:contentDescription="#string/homework_notification_icon"
android:background="#00000000" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/fragment_student_home_attendance"
android:background="#ffff130c" />
<ImageButton
android:layout_width="42dp"
android:layout_height="40dp"
android:id="#+id/fragment_student_home_attendance_imageButton"
android:src="#drawable/ic_attendance"
android:contentDescription="#string/homework_notification_icon"
android:background="#00000000" />
</LinearLayout>
</LinearLayout>
And here is the adapter where I implemented the onClick
public class ClassAdapter extends ArrayAdapter<SchoolClass> implements View.OnClickListener{
private SchoolClass aClass;
public ClassAdapter(Context context, int resource, ArrayList<SchoolClass> objects) {
super(context, resource, objects);
}
public SchoolClass getSchoolClass(){
return this.aClass;
}
public void setSchoolClass(SchoolClass schoolClass){
this.aClass = schoolClass;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.classes_adapter, null);
}
aClass = getItem(position);
if(aClass != null){
TextView title = (TextView) v.findViewById(R.id.fragment_student_home_classTitle_textView);
if(title != null){
title.setText(aClass.getTitle());
}
setSchoolClass(aClass);
}
//set buttons
ImageButton notificationsButton = (ImageButton) v.findViewById(R.id.fragment_student_home_notification_imageButton);
ImageButton gradesButton = (ImageButton) v.findViewById(R.id.fragment_student_home_grades_imageButton);
ImageButton attendanceButton = (ImageButton) v.findViewById(R.id.fragment_student_home_attendance_imageButton);
ImageButton homeworkButton = (ImageButton) v.findViewById(R.id.fragment_student_home_homework_imageButton);
notificationsButton.setOnClickListener(this);
gradesButton.setOnClickListener(this);
attendanceButton.setOnClickListener(this);
homeworkButton.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ClassActivity.class);
View parent = (View) v.getRootView();
//v.getParent() returns null when I look for the textView
TextView title = (TextView) parent.findViewById(R.id.fragment_student_home_classTitle_textView);
System.out.println("\n\n title: " + title.getText().toString() + " \n\n");
intent.putExtra("__CLASS_NAME__", title.getText().toString());
It populates a list view and if I were to click on fragment_student_home_attendance_imageButton I would want to get the fragment_student_home_classTitle_textView associated with that ClassAdapter.
Just give a idea to you, hope this can help to fix the problem :)
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
// ....
ImageButton notificationsButton = (ImageButton) v.findViewById(R.id.fragment_student_home_notification_imageButton);
notificationsButton.setTag(R.id.fragment_student_home_notification_imageButton);
// ....
return v;
}
#Override
public void onClick(View v) {
if(v!=null && v.getTag()!=null && v.getTag()==R.id.fragment_student_home_notification_imageButton){
// do something you like
}
}
Ok nevermind, I got it.
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ClassActivity.class);
//It's the parent of the parent OF THE parent where
//the textView lies in my layout.
View parent = (View) v.getParent().getParent().getParent();
TextView title = (TextView) parent.findViewById(R.id.fragment_student_home_classTitle_textView);
System.out.println("\n\n title: " + title.getText().toString() + " \n\n");
intent.putExtra("__CLASS_NAME__", title.getText().toString());
If there is a better way to get this solution, as it seems like a head on approach to the problem, I welcome any comments!
Related
I am trying to add delete button next to my item details basing on this answer.
I tried it on a new project, it works perefect:
and when I click delete, I delete the item.
sadly, I tried to use it in my own project when my listView is in calendar_tab.xml. calendar_tab uses CompactCalendarTab.java - fragment class.
so Android Studio errored:
E:\Downloads\MyCustomAdapter.java
Error:(49, 63) error: cannot find symbol method getSystemService(String)
I tried to change
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
to
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
but with no luck.
custom_listview.xml:
<?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/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
calendar_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/calendar_tab"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbar_calendar"
android:background="#color/teal_300"
android:layout_alignParentTop="true"
android:padding="10sp"
android:layout_alignParentStart="true">
<ImageButton
android:id="#+id/back_button"
android:src="#mipmap/ic_arrow_back_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="goBackmain"
/>
<ImageButton
android:id="#+id/next_button"
android:src="#mipmap/ic_keyboard_arrow_left_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/showdate"
android:layout_toStartOf="#+id/showdate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#color/black"
android:id="#+id/showdate"
android:layout_alignBaseline="#+id/prev_button"
android:layout_alignBottom="#+id/prev_button"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="#+id/prev_button"
android:src="#mipmap/ic_keyboard_arrow_right_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/showdate"
android:layout_toEndOf="#+id/showdate" />
</RelativeLayout>
<com.github.sundeepk.compactcalendarview.CompactCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/compactcalendar_view"
android:layout_width="fill_parent"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="#null"
app:compactCalendarTextColor="#color/blue_grey_700"
app:compactCalendarCurrentSelectedDayBackgroundColor="#color/teal_300"
app:compactCalendarCurrentDayBackgroundColor="#color/teal_600"
app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator"
app:compactCalendarEventIndicatorStyle="small_indicator"
app:compactCalendarOtherMonthDaysTextColor="#534c4c"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
android:layout_below="#+id/toolbar_calendar"
/>
<ListView
android:id="#+id/bookings_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/compactcalendar_view"
>
</ListView>
my fragment:
public class CompactCalendarTab extends Fragment {
final ListView bookingsListView = (ListView) v.findViewById(R.id.bookings_listview);
adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mutableBookings);
final ArrayList<String> list = new ArrayList<>();
final MyCustomAdapter adapter = new MyCustomAdapter(list, this);
bookingsListView.setAdapter(adapter);
compactCalendarView = (CompactCalendarView)
v.findViewById(R.id.compactcalendar_view);
}
my custom adapter:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private CompactCalendarTab context;
public MyCustomAdapter(ArrayList<String> list, CompactCalendarTab context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_listview, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
}
Change this line
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
To this
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Here is my code so far based on various answers to related questions on SO.
In my Activity
GridView gv = (GridView) findViewById(R.id.gridView1);
MyCustomAdapter mAdapter = new MyCustomAdapter();
mAdapter.addItem("Action1");
mAdapter.addItem("Action2");
mAdapter.addItem("Action3");
gv.setAdapter(mAdapter);
My Adapter
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
NumericViewHolder holder = new NumericViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.horizontalnumberpicker, null);
holder.textView = (TextView)convertView.findViewById(R.id.txtNPTitle);
holder.minus = (Button)convertView.findViewById(R.id.btnMinus);
holder.plus = (Button)convertView.findViewById(R.id.btnPlus);
holder.value = (TextView)convertView.findViewById(R.id.txtNPValue);
holder.minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
int value = Integer.parseInt(tv.getText().toString());
Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
if (value > 0) {
value = value - 1;
tv.setText(Integer.toString(value));
}
}
});
holder.plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
int value = Integer.parseInt(tv.getText().toString()); <--Error is here
Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
value = value + 1;
tv.setText(Integer.toString(value));
}
});
convertView.setTag(holder);
} else {
holder = (NumericViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
XML - Main_Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlAddProduct"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnSaveProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="#string/Save" />
<TextView
android:id="#+id/lblSelectCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnSaveProduct"
android:layout_alignBottom="#+id/btnSaveProduct"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="#string/selectCategory" />
<Spinner
android:id="#+id/spCategory"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/lblSelectCategory"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_toEndOf="#+id/lblSelectCategory"
android:layout_toRightOf="#+id/lblSelectCategory" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblSelectCategory"
android:layout_below="#+id/btnSaveProduct"
android:layout_marginTop="14dp"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
XML - Horizontalnumberpicker
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtNPTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="EnterTextHere"
android:layout_marginTop="5dp" />
<Button
android:id="#+id/btnPlus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/txtNPValue"
android:layout_alignBottom="#+id/txtNPValue"
android:layout_toRightOf="#+id/txtNPValue"
android:text="+" />
<TextView
android:id="#+id/txtNPValue"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/btnMinus"
android:layout_toRightOf="#+id/btnMinus"
android:gravity="center"
android:text="0"
android:textAppearance="#style/AppBaseTheme"
android:textSize="20dp" />
<Button
android:id="#+id/btnMinus"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtNPTitle"
android:layout_marginTop="2dp"
android:text="-" />
</RelativeLayout>
The requirement is that when I press the plus button, I need to increment the value in the corresponding txtNPValue. Similarly with the minus button, I need to decrement the value in the txtNPValue.
The error is
01-31 22:40:10.854: E/AndroidRuntime(32198): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
Also I do not know if this is the right way to program such a requirement and would like some pointers.
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
this will return null pointer exception, as the view that u r getting is of the button or the clicked item..
Use this :-
RelativeLayout rlLayout = (RelativeLayout) v.getParent();
TextView tv = (TextView) rlLayout.findViewById(R.id.txtNPValue);
please remove:
TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
from onClick to resolve nullpointerException
I have created a list view with plus and minus buttons on each row but the value only changes on the top row also i need a way so that when i click submit order it will reference the type of coffee and say what the quantity is that that was ordered.
activity_main.xml
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
customListAdapter.java
public class CustomListAdapter extends ArrayAdapter<String> {
String[] description = {
"Price: £1.75",
"Price: £2.15",
"Price: £2.80",
"Price: £2.00",
"Price: £2.80",
"Price: £3.35",
"Price: £3.15",
"Price: £2.00",
"Price: £2.45",
"Price: £3.00",
"Price: £2.25",
};
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText(description[position]);
return rowView;
};
myList.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Top text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#906B41"
android:text="Bottom Text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</View>
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:onClick="decrement"
android:textAlignment="center"
android:background="#layout/roundshapebtn"
android:text="-" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="0"
android:textColor="#android:color/white"
android:textAlignment="center"
android:textSize="18sp" />
<Button
android:id="#+id/increment_button_view"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:onClick="increment"
android:textAlignment="center"
android:text="+"
android:background="#layout/roundshapebtn"/>
</LinearLayout>
Image showing the quantity only changes on the top value
i dont know how is your on click listener code but i am providing some example for that-
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_custom_list_layout, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
}
});
return view;
}
}
Here is my custom adapter code. I get an error that my textView.setText() is null
"Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
I usually retrieve data from my database, but right now I used hardcoded string just to check if the problem is with the database, but the problem persist with the hardcoded string.
public class CardViewAdapter extends BaseAdapter {
Context context;
List<Note> notes;
TextView cardTitleText;
TextView cardDescriptionText;
TextView cardDateText;
ImageView cardImage;
public CardViewAdapter(Context context, ArrayList<Note> notes) {
this.context = context;
this.notes = notes;
}
#Override
public int getCount() {
return notes.size();
}
#Override
public Object getItem(int position) {
return notes.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(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.card_view, parent, false);
cardTitleText = (TextView) view.findViewById(R.id.titleTextCardView);
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);
cardDateText = (TextView) view.findViewById(R.id.dateTextCardView);
cardImage = (ImageView) view.findViewById(R.id.cardImage);
Note note = notes.get(position);
Log.v("NOTE", note.getTitle() + " " + note.getDescription() + " " + note.getDate());
cardTitleText.setText("Title");
cardDescriptionText.setText("Description");
cardDateText.setText("Date");
return view;
}
}
Here is my card_view layout (item layout for my listView):
<RelativeLayout
android:id="#+id/relativeLayoutCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/relative_layout"
android:elevation="6dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/titleTextCardView"
android:layout_below="#+id/frameLayoutCardView"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:text="Example Title"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionTextCardView"
android:layout_alignParentLeft="true"
android:layout_below="#+id/titleTextCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="Example description"
android:textSize="13sp"
android:textStyle="bold" />
<FrameLayout
android:id="#+id/frameLayoutCardView"
android:layout_height="130dp"
android:layout_width="fill_parent">
<ImageView
android:id="#+id/cardImage"
android:layout_height="130dp"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/placeholder" />
<TextView
android:id="#+id/dateTextCardView"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Example date" />
</FrameLayout> </RelativeLayout>
I know that this is probably a stupid and easy to fix problem, but I just keep banging my head because I don't know how to fix it because I can't see how can my TextView be null.
You have a typo in your XML... this line references an element that does not exist in your XML:
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextView);
I think it should be:
cardDescriptionText = (TextView) view.findViewById(R.id.descriptionTextCardView);
based on the XML element:
<TextView
android:id="#+id/descriptionTextCardView"
android:layout_alignParentLeft="true"
android:layout_below="#+id/titleTextCardView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="Example description"
android:textSize="13sp"
android:textStyle="bold" />
I need to add a code responsible for action when one row from my list is clicked. I don't know if it should be OnItemClickListener or OnClickListener and how&where to write it. My app is with view holder. Here is my code:
public class JobListAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
public static class WorkViewHolder {
public TextView mJob;
public ImageView mImageAndroKorpo;
}
public JobListAdapter(Context mContext, List<String> mDane) {
super(mContext, R.layout.list_element_job, mDane);
this.mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
WorkViewHolder mHolder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_element_job, parent, false);
mHolder = new WorkViewHolder();
TextView mJobsName = (TextView) convertView.findViewById(R.id.nazwa_oferty);
ImageView mImageAndroKorpo = (ImageView)convertView.findViewById(R.id.list_image);
mHolder.mJob = mJobsName;
mHolder.mImageAndroKorpo = mImageAndroKorpo;
convertView.setTag(mHolder);
} else {
mHolder = (WorkViewHolder)convertView.getTag();
}
final String mWorkPosition = getItem(position);
mHolder.mJob.setText(mWorkPosition);
mHolder.mJob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here?
}
});
return convertView;
}
}
I added code where I think it should be placed. Is it ok? OnItem or OnClick? And how to use item position?
My list_element_job.xml:
<?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="wrap_content"
android:background="#android:color/white"
android:padding="10dp"
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/list_image"
android:src="#drawable/android_white_piece"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="1dp"
android:layout_height="1dp"/>
<TextView
android:text=""
android:background="#drawable/android_korpo_transparent3"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="left"
android:layout_toLeftOf="#id/list_image"
android:layout_width="0dp"
android:textSize="7pt"
android:layout_height="wrap_content"
android:id="#+id/nazwa_oferty"/>
<TextView
android:text="Details..."
android:background="#android:color/white"
android:clickable="true"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/nazwa_oferty"
android:textSize="6pt"
android:textColor="#android:color/darker_gray"/>
</RelativeLayout>
Please help me somehow :)
Just remove the focus for the checkbox in xml like below:
<CheckBox
android:id="#+id/list_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
because of check box focus list item click listeners wont work.
Just remove onClick from your adapter class. And add OnItemClickListener to your ListView
example:
JobListAdapter jobListAdapter = new JobListAdapter (...);
listView.setAdapter(jobListAdapter);
listView..setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});