Android: Custom List View is not working properly - android

My apologies for not providing a proper source code.
basically what I have in my list view xml layout is:
listleave.xml
leaveSummary.xml
leavedB
Custom Adapter
ApplyLeave
I'm creating an app where I have a list view and when I'm running my app I'm getting an error in Locgat.
Caused by:java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null
object reference
at com.example.huzai.civilsoft1.ApplyLeave.loadDataInListView(ApplyLeave.java:258 )
at com.example.huzai.civilsoft1.ApplyLeave.onCreate(ApplyLeave.java:84)
In the line loadDataInListView when I'm not commenting l1.setAdapter(customAdapter); the app crashes but when I'm commenting it works properly.
Result of not commenting: l1.setAdapter(customAdapter);
ApplyLeave Activity
public class ApplyLeave extends AppCompatActivity {
LeaveService myDb;
//Declaration EditTexts
EditText editstartDate;
EditText editendDate;
DatePickerDialog datePickerDialog;
CheckBox checkgoingAbroad;
Spinner spinnertypess;
Spinner editleavess;
EditText editremarks;
EditText editnoOfDays1;
TextView noOfDays;
TextView endd;
Button btnApply;
Button addfield;
Button deletefield;
LinearLayout parentlinearlayout;
ListView l1;
ArrayList<Leave> arrayList;
CustomAdapter customAdapter;
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_leave);
myDb = new LeaveService (this);
checkgoingAbroad =(CheckBox) findViewById(R.id.GoingAbroad);
spinnertypess = (Spinner)findViewById(R.id.types);
editleavess = (Spinner)findViewById(R.id.Leaves);
editremarks = (EditText)findViewById(R.id.reason);
editnoOfDays1 = (EditText)findViewById(R.id.noOfDays);
parentlinearlayout =
(LinearLayout)findViewById(R.id.parent_linear_layout);
addfield = (Button)findViewById(R.id.addField);
deletefield = (Button)findViewById(R.id.removeField);
endd = (TextView)findViewById(R.id.date);
l1 = (ListView)findViewById(R.id.leaveApplicationSummary);
btnApply = (Button)findViewById(R.id.apply);
AddData();
arrayList = new ArrayList<>();
loadDataInListView();
//Created a method for loadDataInListView
private void loadDataInListView() {
arrayList = myDb.getAllData();
customAdapter = new CustomAdapter(this,arrayList);
l1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
private void AddData() {
btnApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData("", checkgoingAbroad.toString(), spinnertypess.toString(), editleavess.toString(), editnoOfDays1.getText().toString(), editstartDate.getText().toString(), editendDate.getText().toString(), editremarks.getText().toString());
if (isInserted == true){
Toast.makeText(ApplyLeave.this, "Data Inserted Successfully",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ApplyLeave.this, "Data Not Inserted",Toast.LENGTH_LONG).show();
}
}
});
}
Result of commenting: //l1.setAdapter(customAdapter);
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<Leave> arrayList;
public CustomAdapter(Context context, ArrayList<Leave>arraylist) {
this.context = context;
this.arrayList = arraylist;
}
#Override
public int getCount() {
return this.arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.leavesummary, null);
TextView types = (TextView)convertView.findViewById(R.id.textViewtypes);
TextView startDate = (TextView)convertView.findViewById(R.id.textViewstartDate);
TextView endDate = (TextView)convertView.findViewById(R.id.textViewendDate);
TextView leavetype = (TextView)convertView.findViewById(R.id.textViewleaveType);
TextView noOfDays = (TextView)convertView.findViewById(R.id.textViewnoOfDays);
TextView reason = (TextView)convertView.findViewById(R.id.textViewreason);
Leave leave = arrayList.get(position);
types.setText(leave.getTypes());
startDate.setText(leave.getStartDate());
endDate.setText(leave.getEndDate());
leavetype.setText(leave.getLeaves());
noOfDays.setText(leave.getNoOfDays());
reason.setText(leave.getReason());
return convertView;
}
}
listleave.xml Over here I'm using ConstraintLayout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveApplicationSummary">
</ListView>
</android.support.constraint.ConstraintLayout>
leaveSummary.xml
<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"
android:background="#drawable/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Application Summary"
android:textColor="#080808"
android:textSize="20dp"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="72dp"
android:text="Applicant Name :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewApplicantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="72dp"
android:text="Huzaifa [001]"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="116dp"
android:text="Type of Leave :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewtypes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="116dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="164dp"
android:text="Leave Start Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewstartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="164dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="208dp"
android:text="Rejoining Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewendDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="208dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="248dp"
android:text="Leave Type :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewleaveType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="248dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="288dp"
android:text="Total Days Applied :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewnoOfDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="216dp"
android:layout_marginLeft="216dp"
android:layout_marginTop="288dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="332dp"
android:text="Remarks :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewreason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginLeft="144dp"
android:layout_marginTop="332dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Back" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
apply_leave.xml
<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="match_parent"
android:background="#drawable/background"
tools:context=".ApplyLeave">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_linear_layout"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/GoingAbroad"
android:hint="Going Abroad"
android:inputType="text"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Need Advance Payment?"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/types"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/types"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Leave Type"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/Leaves"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/leaves"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/noOfDays"
android:hint="No. of Days"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/addField"
android:text="+"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/removeField"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Leave starts on" />
<EditText
android:id="#+id/startDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rejoining will be on" />
<EditText
android:id="#+id/endDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<EditText
android:id="#+id/reason"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Remarks" />
<TextView
android:id="#+id/days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total leave days = 0"/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Leave ends on = "/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:gravity="bottom">
<Button
android:id="#+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Apply" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please tell me where I'm going wrong.

You are getting null l1.
Confirm your id of ListView in your activity_apply_leave.xml. It should be leaveApplicationSummary.
EDIT:
Your ListView is in listleave.xml but you are using activity_apply_leave.xml in your Activity.
Move your ListView from listleave.xml to activity_apply_leave.xml.

Related

How to click button inside listview

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.xml
<?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="match_parent"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<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:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?

making a customized list view in android showing the track of a bus

how can I make a layout that looks like this in android using list view.
I am making a tracking system for many buses. If the bus crossed the location the circle will turn green else it is red.
I just want a vertical line which has length depending on the number of stops for the bus.
the line should be passing through the center of the circles.
Here is the listBusStops.xml file
`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginBottom="30dp">
<ImageView
android:id="#+id/ivstatus"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:layout_marginStart="5dp"
android:gravity="center_vertical"
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="30dp"
/>
</TableRow>
</RelativeLayout>
`
here the custom_list class which I am using
`
class Custom_list extends ArrayAdapter {
private final Activity context;
private final Integer imageid;
ArrayList<String> list;
ArrayList<String> status;
public Custom_list(Activity context, ArrayList<String> list, ArrayList<String> status, Integer imageid) {
super(context, R.layout.list_bus_stops,Track_any_bus.stop_name);
this.context=context;
this.list = list;
this.imageid=imageid;
this.status = status;
}
public View getView (int position, View view, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowview = inflater.inflate(R.layout.list_bus_stops,null,true);// ALL LAYOUT RESOURSE OF list_bus_stops SAVED TO THIS VIEW(rowview)
TextView textView = rowview.findViewById(R.id.tvname);//
ImageView imageView = rowview.findViewById(R.id.ivstatus);
textView.setText(Track_any_bus.stop_name.get(position));
if(Track_any_bus.status.get(position).equalsIgnoreCase("yes")) {
imageView.setImageResource(R.drawable.circle_green);
}
else {
imageView.setImageResource(R.drawable.circle_red);
}
return rowview;
}
`
thank you for your time.
Here is the demo working code plz have a look i hope it will help you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#D3D3D3" />
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="44dp"
android:tint="#000000"
app:srcCompat="#drawable/shape_round_solid" />
</RelativeLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="1dp"
app:cardCornerRadius="5dp"
app:cardElevation="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:background="#800000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_height="1dp"></LinearLayout>
<TextView
android:id="#+id/noteTitle"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:background="#000000 "
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:background="#800000"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_height="1dp"></LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:textSize="14dp"
android:textColor="#000080"
android:text="Text-Preview"/>
<TextView
android:id="#+id/textPreview"
android:textSize="14dp"
android:textColor="#000080"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:text="Sample Text"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#000080"
android:layout_marginLeft="18dp"
android:layout_width="120dp"
android:text="Last Modified"/>
<TextView
android:id="#+id/lastModified"
android:textColor="#000080"
android:layout_width="wrap_content"
android:textSize="14dp"
android:layout_marginLeft="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:text="12/4/2018"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>

NullPointerException in dialog - Strange case

I have a stupid problem but i couldnt solve it yet. I have an activity with a button in toolbar, this button open one dialog with some filter params.
Here is what a i did..
Spinner spinner_categoria;
Spinner spinner_vendedor;
SeekBar seek_preco;
TextView tv_filtro;
onCreate method {
...
}
onClickEvent{
dialogFiltro();
}
public void dialogFiltro() {
Context context = SalesActivity.this;
final Dialog dialog;
dialog = new Dialog(context);
//dialog.setContentView(R.layout.dialog_filtro);
// dialog.setTitle("Filtro");
//dialog.show();
VendedorDAO auxVendedor = new VendedorDAO();
final List<String> listVendedor = auxVendedor.getVendedorList();
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null, false);
CardView bt_cancelar = (CardView) dialogView.findViewById(R.id.cb_filtro_cancelar);
CardView bt_aceitar = (CardView) dialogView.findViewById(R.id.cb_filtro_aceitar);
seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);
tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
spinner_categoria = (Spinner) dialogView.findViewById(R.id.spinner_categoria);
spinner_vendedor = (Spinner) dialogView.findViewById(R.id.spinner_vendedor);
spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
this, R.array.categoria_array, android.R.layout.simple_spinner_item));
spinner_vendedor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listVendedor));
String oi = "oi";
tv_filtro.setText(oi);
dialog.setContentView(dialogView);
dialog.show();
seek_preco.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// tv_filtro.setText("R$ " + progress);
// Log.i("RS " , String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
bt_cancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
bt_aceitar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
categoria = (String) spinner_categoria.getSelectedItem();
vendedor = (Integer) spinner_vendedor.getFirstVisiblePosition();
Log.e("id vendedor ", String.valueOf(vendedor));
if (vendedor == 0) {
vendedor = -1;
}
int precoAux = seek_preco.getProgress();
precoMax = (float) precoAux;
}
});
}
I have this in dialog_filtro.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="8dp"
android:foreground="#drawable/border_card"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="8dp">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cb_filtro_cancelar"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="X"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cb_filtro_aceitar"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="Aceitar"
android:textColor="#android:color/white" />
</FrameLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Filtrar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/tv_label_categoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_categoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/tv_label_vendedor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Supermercado"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_vendedor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preço máximo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="#+id/sb_preco_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminate="false"
android:max="100"
android:progress="0" />
<TextView
android:id="#+id/tv_filtro_preco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:id="#+id/rv_shoppingcart_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/tv_label_oerdenar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Ordenar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Preço:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_preco"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_preco_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_preco_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_data_Validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Data de validade:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_validade_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_validade_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<LinearLayout
android:id="#+id/linear_isoffer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_label_ofertas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Apenas ofertas:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cb_offer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:checked="false"
android:text="Selecionar apenas ofertas" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
</LinearLayout>
Everything works fine (spinner, cardview etc) but i always get NullPointerException because the TextView and i don't know why. Here goes my log:
FATAL EXCEPTION: main
Process: com.catafeira.catafeira, PID: 2419
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.catafeira.catafeira.SalesActivity.dialogFiltro(SalesActivity.java:281)
at com.catafeira.catafeira.SalesActivity.onOptionsItemSelected(SalesActivity.java:432)
at android.app.Activity.onMenuItemSelected(Activity.java:2914)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
EDIT: I know what is a NullPointerException, I already checked the previous posts on the subject, but no problem is the same as mine
I think the problem is with findViewByID. The documentation says
findViewById
Added in API level 1
View findViewById (int id)
Finds a child view with the given identifier. Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()).
It is just possible that since you are calling show() later the textView might not be created. I would do this
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_filtro, null);
Button bt_cancelar = (Button) dialogView.findViewById(R.id.cb_filtro_cancelar);
Button bt_aceitar = (Button) dialogView.findViewById(R.id.cb_filtro_aceitar);
seek_preco = (SeekBar) dialogView.findViewById(R.id.sb_preco_max);
tv_filtro = (TextView) dialogView.findViewById(R.id.tv_filtro_preco);
spinner_categoria = (Spinner) dialogView.findViewById(R.id.spinner_categoria);
spinner_vendedor = (Spinner) dialogView.findViewById(R.id.spinner_vendedor);
dialog.setContentView(dialogView)
dialog.show();
spinner_categoria.setAdapter(ArrayAdapter.createFromResource(
this, R.array.categoria_array, android.R.layout.simple_spinner_item));
spinner_vendedor.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listVendedor));
tv_filtro.setText("oi");
Change the XML to the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:foreground="#drawable/border_card"
android:layout_margin="8dp"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="8dp">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cb_filtro_cancelar"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="X"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cb_filtro_aceitar"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:background="#color/colorPrimary"
android:elevation="2dp"
android:text="Aceitar"
android:textColor="#android:color/white" />
</FrameLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Filtrar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/tv_label_categoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_categoria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/tv_label_vendedor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="Supermercado"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner_vendedor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preço máximo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="#+id/sb_preco_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:indeterminate="false"
android:max="100"
android:progress="5" />
<TextView
android:id="#+id/tv_filtro_preco"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="5"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<View
android:id="#+id/rv_shoppingcart_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/tv_label_oerdenar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:text="Ordenar por"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/colorPrimary" />
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Preço:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_preco"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_preco_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_preco_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_data_Validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="Data de validade:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/group_validade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_validade_maior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Maior" />
<RadioButton
android:id="#+id/rb_validade_menor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Menor" />
</RadioGroup>
<TextView
android:id="#+id/tv_label_ofertas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="Apenas ofertas:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cb_offer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:checked="false"
android:text="Selecionar apenas ofertas" />
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
Hope this helps!
SOLVED
The problem was something related android material design, I didn't note before that some layouts has 2 files, so some files were outdated -.-
Thanks for everyone that tried to help.

Object reference not set to an instance of an object when trying to populate Xamarin Android TextView on fragmenrt from arrary

I'm getting the error System.NullReferenceException: Object reference not set to an instance of an object when I try to reference a TextView from my fragment. It happens at runtime.
Fragment
public class Fragment1 : Fragment
{
private int checkNumber = 0;
private string[] updateTitleArray = new string[10];
private string[] updateBodyArray = new string[10];
private TextView updatesTitle0;
private TextView updatesTitle1;
private TextView updatesTitle2;
private TextView updatesTitle3;
private TextView updatesTitle4;
private TextView updatesTitle5;
private TextView updatesTitle6;
private TextView updatesTitle7;
private TextView updatesTitle8;
private TextView updatesTitle9;
private TextView updateBody0;
private TextView updateBody1;
private TextView updateBody2;
private TextView updateBody3;
private TextView updateBody4;
private TextView updateBody5;
private TextView updateBody6;
private TextView updateBody7;
private TextView updateBody8;
private TextView updateBody9;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
fillUpdates(updateTitleArray, updateBodyArray);
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.fragment1, null);
}
public void fillUpdates(string[] updatetitlearray, string[] updatebodyarray)
{
//increment1 so if block skips here
checkNumber = 1;
//fill updatesArray
updateTitleArray = updatetitlearray;
updateBodyArray = updatebodyarray;
updatesTitle0 = View.FindViewById<TextView>(Resource.Id.update_title0);
updatesTitle1 = View.FindViewById<TextView>(Resource.Id.update_title1);
updatesTitle2 = View.FindViewById<TextView>(Resource.Id.update_title2);
updatesTitle3 = View.FindViewById<TextView>(Resource.Id.update_title3);
updatesTitle4 = View.FindViewById<TextView>(Resource.Id.update_title4);
updatesTitle5 = View.FindViewById<TextView>(Resource.Id.update_title5);
updatesTitle6 = View.FindViewById<TextView>(Resource.Id.update_title6);
updatesTitle7 = View.FindViewById<TextView>(Resource.Id.update_title7);
updatesTitle8 = View.FindViewById<TextView>(Resource.Id.update_title8);
updatesTitle9 = View.FindViewById<TextView>(Resource.Id.update_title9);
updateBody0 = View.FindViewById<TextView>(Resource.Id.update_body0);
updateBody1 = View.FindViewById<TextView>(Resource.Id.update_body1);
updateBody2 = View.FindViewById<TextView>(Resource.Id.update_body2);
updateBody3 = View.FindViewById<TextView>(Resource.Id.update_body3);
updateBody4 = View.FindViewById<TextView>(Resource.Id.update_body4);
updateBody5 = View.FindViewById<TextView>(Resource.Id.update_body5);
updateBody6 = View.FindViewById<TextView>(Resource.Id.update_body6);
updateBody7 = View.FindViewById<TextView>(Resource.Id.update_body7);
updateBody8 = View.FindViewById<TextView>(Resource.Id.update_body8);
updateBody9 = View.FindViewById<TextView>(Resource.Id.update_body9);
updatesTitle0.Text = updateTitleArray[0].ToString();
updatesTitle1.Text = updateTitleArray[1].ToString();
updatesTitle2.Text = updateTitleArray[2].ToString();
updatesTitle3.Text = updateTitleArray[3].ToString();
updatesTitle4.Text = updateTitleArray[4].ToString();
updatesTitle5.Text = updateTitleArray[5].ToString();
updatesTitle6.Text = updateTitleArray[6].ToString();
updatesTitle7.Text = updateTitleArray[7].ToString();
updatesTitle8.Text = updateTitleArray[8].ToString();
updatesTitle9.Text = updateTitleArray[9].ToString();
updateBody0.Text = updateBodyArray[0].ToString();
updateBody1.Text = updateBodyArray[1].ToString();
updateBody2.Text = updateBodyArray[2].ToString();
updateBody3.Text = updateBodyArray[3].ToString();
updateBody4.Text = updateBodyArray[4].ToString();
updateBody5.Text = updateBodyArray[5].ToString();
updateBody6.Text = updateBodyArray[6].ToString();
updateBody7.Text = updateBodyArray[7].ToString();
updateBody8.Text = updateBodyArray[8].ToString();
updateBody9.Text = updateBodyArray[9].ToString();
}
}
View
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--Cardview 1-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shownbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view2-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--cardview 3-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 4-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 5-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 6-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 7-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 8-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 9-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
<!--card view 10-->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp">
<GridLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="1">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_column="0"
android:padding="16sp">
<TextView
android:id="#+id/update_title9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update 1"
android:padding="4sp"
android:textSize="24sp"
android:textColor="#color/primaryText" />
<TextView
android:id="#+id/update_body9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Details Is Shown Here"
android:padding="4sp"
android:textSize="16sp" />
</LinearLayout>
</GridLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
I understand that what I am trying to achieve can be implemented with recycler views but I haven't fully understood that, because I am a beginner programmer.
You are calling you function too early. OnCreate gets called before OnCreateView. That means, that your views haven't been created, when you try to access the layout components. You have to move your call of fillUpdates to OnStart (or OnCreateView or another lifecycle method that gets called after OnCreateView).
Have a look at the Frament Lifecycle
public override void OnStart(Bundle bundle)
{
base.OnStart(bundle);
fillUpdates(updateTitleArray, updateBodyArray);
}

How to get Item Clicked in Fragment's Recycler View to activity?

i have an activity which is having about 4 fragment and fragment have a recyclerview and in each recyclerview item there is a button.. i have a textfield in activity and i want to show the no. of button clicked.
thats my activity.
<?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="match_parent"
android:orientation="vertical"
android:weightSum="3"
tools:context="com.example.khaalijeb.newlistview_module.PromoCodeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.4"
android:background="#f9f9f9"
android:elevation="5dp">
<ImageView
android:id="#+id/back"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="#drawable/greyback" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back"
android:text="Fancy Some Deals ?"
android:textSize="16dp"
android:textStyle="bold" />
<Button
android:id="#+id/skip"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/roundedcorneredittext"
android:text="SKIP"
android:textColor="#0277bd"
android:textSize="12dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.55"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="#E12728"
app:tabMode="scrollable"
app:tabSelectedTextColor="#E12728"
app:tabTextAppearance="#style/TextAppearance.Design.Tab" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.05"
android:background="#f9f9f9"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel">
<EditText
android:id="#+id/spass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:hint="Promo Code"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textColor="#color/material_blue_grey_800"
android:textSize="14dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#727272" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLY"
android:textColor="#0277bd"
android:textSize="12dp" />
<Button
android:id="#+id/applied"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/roundedcorneredittext"
android:text="APPLIED"
android:textColor="#0277bd"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8">
<TextView
android:id="#+id/credittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="12dp"
android:text="Credited To Jeb No." />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/credittext"
android:layout_toRightOf="#+id/credittext"
android:text="C100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/credittext"
android:layout_marginRight="16dp"
android:text="Rs.100"
android:textSize="12dp"
/>
<TextView
android:id="#+id/pickdeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/credittext"
android:layout_margin="5dp"
android:text="Haven't Picked Any Deals Yet"
android:textColor="#78909C"
android:textSize="12dp" />
<TextView
android:id="#+id/dealamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/amount"
android:text="Rs.50"
android:layout_marginRight="16dp"
android:layout_alignTop="#+id/pickdeal"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/dealamount"
android:text="+"
android:layout_alignTop="#+id/dealamount"
android:layout_marginRight="2dp"
android:textSize="12dp"
/>
<TextView
android:id="#+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pickdeal"
android:text="Discount"
android:layout_alignLeft="#+id/pickdeal"
android:textColor="#F44336"
android:textSize="12dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/discountedamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Rs.50"
android:textColor="#F44336"
android:layout_alignTop="#+id/discount"
android:layout_marginRight="16dp"
android:textSize="12dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/discountedamount"
android:layout_alignTop="#+id/discountedamount"
android:text="-"
android:textColor="#F44336"
android:layout_marginRight="2dp"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0277bd"
android:text="PROCEED TO PAY" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
recyelrview item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/llContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
card_view:cardBackgroundColor="#f9f9f9"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.7">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/nike" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/brandname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="hello"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="14dp"
android:typeface="sans" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/brandname"
android:layout_below="#+id/brandname"
android:layout_marginTop="10dp"
android:text="Get 50% off on Apparels"
android:textSize="12dp" />
<TextView
android:id="#+id/seemore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/brandname"
android:layout_below="#+id/title"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="See More"
android:textSize="12dp" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignTop="#+id/seemore"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/seemore"
android:src="#drawable/sendgrey" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<Button
android:id="#+id/free"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorneredittext"
android:text="Free"
android:textColor="#0277bd"
android:textSize="12dp"
/>
<Button
android:id="#+id/taken"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:background="#drawable/roundedcorner1"
android:text="Great!"
android:textColor="#ffffff"
android:textSize="12dp"
android:visibility="invisible"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Adapter.java
public class PromoCodeRecyclerViewAdapter extends RecyclerView.Adapter<PromoCodeRecyclerViewAdapter.MyViewholder> {
private ClickListener clickListener;
private LayoutInflater inflator;
private Context mcontext;
Typeface font;
public Button b1;
public ImageButton b2;
List<promocodedata> data = Collections.emptyList();
public PromoCodeRecyclerViewAdapter(Context context, List<promocodedata> y, Typeface font) {
inflator = LayoutInflater.from(context);
this.data = y;
this.mcontext = context;
this.font = font;
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflator.inflate(R.layout.promocode_item_layout, parent, false);
MyViewholder holder = new MyViewholder(view);
return holder;
}
#Override
public void onBindViewHolder(final MyViewholder holder, int position) {
promocodedata current = data.get(position);
holder.brandicon.setImageResource(current.brandicon);
holder.brandname.setText(current.brandname);
holder.title.setText(current.title);
holder.free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.VISIBLE);
holder.free.setVisibility(View.INVISIBLE);
}
});
holder.taken.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.taken.setVisibility(View.INVISIBLE);
holder.free.setVisibility(View.VISIBLE);
}
});
}
#Override
public int getItemCount() {
return data.size();
}
public interface ClickListener {
void itemClicked(View v, int position);
}
class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView brandicon;
TextView brandname;
TextView title;
Button free;
Button taken;
public MyViewholder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
brandicon = (ImageView) itemView.findViewById(R.id.icon);
brandname = (TextView) itemView.findViewById(R.id.brandname);
title = (TextView) itemView.findViewById(R.id.title);
free = (Button)itemView.findViewById(R.id.free);
taken = (Button)itemView.findViewById(R.id.taken);
}
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.itemClicked(v, getPosition());
}
}
}
}
You need to create your onitemclick because RecyclerView does not have a OnItemClick like Listview, i have an example in Github check it.

Categories

Resources