Is it possible to create something like a NumberPicker Widget in android but populate it with a series of strings pulled from an SQLite database?
If there's only one entry, then the spinner would have one string, if there is a hundred, then there would be a hundred entries. There cannot be less than one entry by design, as in, the activity can't be entered without at least one entry. There is no hardset upper limit, but the data will be populated by date and it is VERY unlikely that this data set would ever exceed a thousand entries due to the nature of how the data is created.
What I'm trying to do is show data saved in an SQLite database, the name of each dataset would be displayed in the NumberPicker, then the user could quickly scroll through the data. The data is then going to be be displayed in a Bar Chart (MPandroidcharts library), as they scroll past the dataset in the NumberPicker. At least, that's the goal.
I've managed the SQLite database, and I've got the charts working for single datasets, I just can't figure out how to get the data selection side working.
Try this code this is just a List Activity that is backed by a Recycler Adapter the XML for the List Activity is included and the Card Layout XML file
public class ListActivity extends AppCompatActivity {
DBHelper helper;
static List<DBModel>dbList;
RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
addListenerOnButtonAdd();
TextView tvNoData = (TextView)findViewById(R.id.tvNoData);
setTitle("");// This sets the title of the toolbar
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
//topToolBar.setLogo(R.drawable.keyss);// See Notes in MainActivity
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
helper = new DBHelper(this);
dbList = new ArrayList<>();
dbList = helper.getDataFromDB();
mRecyclerView = (RecyclerView)findViewById(R.id.recycleview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Code below defines the adapter
mAdapter = new RecyclerAdapter(this,dbList);
mRecyclerView.setAdapter(mAdapter);
int sz = dbList.size();
if(sz == 0){
tvNoData.setVisibility(View.VISIBLE);
tvNoData.setText("No Data");
}
}
// This method is called from DetailsActivity and notifies Recycler View
that the DB was changed
of DB and Recycler View
public static void removeListRow(int position) {
dbList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, dbList.size());
}
/* this BUTTON is on the ToolBar click to ADD new record */
private void addListenerOnButtonAdd() {
// Navigate to DetailsActivity to ADD new DATA
Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar( tb );
tb.findViewById( R.id.btnAdd ).setOnClickListener( new
View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentSP = new Intent(ListActivity.this,
DetailsActivity.class );
Bundle extras = new Bundle();
extras.putString("FROM_LIST_ACTIVITY","true" );
intentSP.putExtras(extras);
startActivity( intentSP );
}
} );
}
public void onBackPressed(){
Intent intent = new Intent( ListActivity.this, MainActivity.class );
startActivity( intent );
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_lightGray"
android:orientation="vertical"
tools:context="com.searchdb.ListActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#color/color_darkGray"
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="#+id/imageTB"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp"
android:src="#drawable/keyss" />
<TextView
android:text="#string/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/toolbar"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginBottom="20dp"
android:id="#+id/tvLA"
android:textStyle="bold"
android:textColor="#color/color_White"
android:textSize="22sp" />
<Button
android:text="#string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnAdd"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:textStyle="bold"
android:focusable="false"
android:textColor="#color/color_White"
android:background="#color/color_Transparent"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvNoData"
android:gravity="center"
android:layout_marginTop="240dp"
android:visibility="invisible"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="#color/color_Red" />
</LinearLayout>
</LinearLayout>
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
static List<DBModel> dbList;
static private Context context;
int sz;
RecyclerAdapter(Context context, List<DBModel> dbList) {
RecyclerAdapter.dbList = new ArrayList<>();
RecyclerAdapter.context = context;
RecyclerAdapter.dbList = dbList;
}
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemLayoutView =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int
position) {
holder.rowid.setText(String.valueOf(dbList.get(position).getRowid()));
holder.station.setText(dbList.get(position).getStation_Name());
System.out.println("RecyclerAdapter BindViewHolder FIRST position
"+position);
}
#Override
public int getItemCount() {
return dbList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView station, rowid;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
rowid = (TextView) itemLayoutView.findViewById(R.id.rvROWID);
station = (TextView) itemLayoutView.findViewById(R.id.rvSTATION);
// Attach a click listener to the entire row view
itemLayoutView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intentN = new Intent(context, DetailsActivity.class);
Bundle extras = new Bundle();
extras.putInt("POSITION", getAdapterPosition());
extras.putString("FROM_LIST_ACTIVITY", "false");
intentN.putExtras(extras);
context.startActivity(intentN);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_White">
<TextView
android:id="#+id/rvROWID"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="left|center_vertical"
android:padding="10dp"
android:textAlignment="center"
android:text="Position ID"
android:textColor="#color/color_Black"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"/>
<TextView
android:id="#+id/rvSTATION"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="10dp"
android:gravity="right|center_vertical"
android:text="Station"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#color/color_Black" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Related
I have an activity that is separated into two sections:
This first one is a Relative layout, in which I have a TextView that displays the total amount based on data stored on the database.
The second part is a RecyclerView, which displays all the records.
On the adapter I can update and delete the information from the database and I use notifyItemRemoved(position) and notifyItemChanged(position) to reflect the change on the reclyclerview.
My problem is that I couldn't find a way to update the TextView on the Relative Layout, I tried to use notifyDataSetChanged() on the Activity but it doesn't reload the list with the sum of values from the database. If I use it from the adapter I couldn't find a way to access the TextView on the activity. I also tried to set an observer on the adapter, but it doesn't reflect the changes.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
tripRecordList = getStandardAmountList(tripRecordList);
Collections.reverse(tripRecordList);
mAmount.setText(String.valueOf(nf.format(getAmountStd(tripRecordList))));
}
}
});
Following are the code fragments.
Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".Activities.ViewTripRecord">
<RelativeLayout
android:id="#+id/viewrecordLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/viewrecordSummaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="#dimen/cardview_compat_inset_shadow"
android:gravity="center"
android:text="#string/trip_summary"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp" />
<TextView
android:id="#+id/viewrecordTotalAmountText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordSummaryText"
android:layout_alignParentStart="true"
android:padding="3dp"
android:text="#string/amount"
android:textSize="15sp"
android:textStyle="italic|bold" />
<TextView
android:id="#+id/viewrecordTotalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordSummaryText"
android:layout_alignParentStart="true"
android:layout_marginStart="95dp"
android:padding="3dp"
android:textSize="15sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewrecordRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordTotalAmountText"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_trip_record);
setTitle(getResources().getString(R.string.view_expenses));
dbHandler = new DbHandler(this);
tripID = getIntent().getIntExtra("TripID",0);
tripRecordList = dbHandler.getTripRecords(tripID);
mAmount = findViewById(R.id.viewrecordTotalAmount);
recyclerView = findViewById(R.id.viewrecordRecyclerView);
setLayout();
}
private void setLayout(){
NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
if(tripRecordList.size()>0) {
tripRecordList = getStandardAmountList(tripRecordList);
Collections.reverse(tripRecordList);
mAmount.setText(String.valueOf(nf.format(getAmountStd(tripRecordList))));
}
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ViewTripRecordAdapter(tripRecordList, this);
recyclerView.setAdapter(adapter);
}
Adpater:
public class TripRecordView extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView stdAmount;
private ImageButton infoButton, deleteButton, updateButton;
public TripRecordView(#NonNull View itemView) {
super(itemView);
stdAmount = itemView.findViewById(R.id.viewtriptrecordStdAmount);
deleteButton = itemView.findViewById(R.id.viewtriptrecordDelete);
deleteButton.setOnClickListener(this);
updateButton = itemView.findViewById(R.id.viewtriptrecordUpdateButton);
updateButton.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final int position = getAdapterPosition();
switch (v.getId()){
case R.id.viewtriptrecordDelete:
deleteRecord(tripRecord2,position);
break;
case R.id.viewtriptrecordUpdateButton:
updateRecord(tripRecord2,position,recordView );
break;
}
}
}
public void deleteRecord(TripRecord deletedRecord, int position){
dbHandler = new DbHandler(context);
dbHandler.deleteTripRecord(deletedRecord.getTripID(),deletedRecord.getId());
tripRecordList.remove(position);
notifyItemRemoved(position);
}
public void updateRecord(final TripRecord updatedRecord, final int position, final View view){
EditText mAmount;
mAmount = view.findViewById(R.id.insertrecordAmount);
updatedRecord.setAmount(Double.valueOf(mAmount.getText().toString()));
dbHandler.updateTripRecord(updatedRecord);
notifyItemChanged(position);
}
Thanks,
Odair
I want to access from this fragment a view from another layout which is not part of the fragment. That is why i get NullPointerException.
How can i solve this?
Fragment code
public class Pacijenti_fragment extends Fragment {
//this is the JSON Data URL
public final static String MESSAGE_KEY= "com.example.android.app";
//a list to store all the products
List<Pacijent> pacijentList;
String id="";
String ID="";
//the recyclerview
RecyclerView recyclerView;
private SearchView searchView;
private PacijentiAdapter adapter;
Button btnPogled;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view=inflater.inflate(R.layout.pacijenti_fragment, container, false);
//getting the recyclerview from xml
recyclerView = (RecyclerView)view.findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, 36));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//initializing the productlist
pacijentList = new ArrayList<>();
btnPogled=(Button)findViewById(R.id.btnDodaj); //here i make my button
Bundle b=getArguments();
this.id=b.getString("id");
System.out.println(id);
ID = this.id;
btnPogled.setOnClickListener(new View.OnClickListener(){ //here i get null pointer
#Override
public void onClick(View v) {
OnPogled();
}
});
//this method will fetch and parse json
//to display it in recyclerview
loadPacijenti();
return view;
}
This is another xml where I located my button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/GornjiLayout"
>
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Jane"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="#+id/textViewSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/textViewName"
android:text="Doe"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/SredjiLayout"
android:layout_below="#id/GornjiLayout"
>
<TextView
android:id="#+id/textViewDateOfBirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textViewName"
android:layout_marginLeft="5dp"
android:layout_marginRight="150dp"
android:layout_marginTop="5dp"
android:text="21.05.1989"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold" />
<Button
android:id="#+id/buttonDodaj"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Otvori" />
</LinearLayout>
<TextView
android:id="#+id/textViewAdress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/SredjiLayout"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Some street 45, UnknownVille"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
I don't know how to access other xml from fragment. And this xml is separate file because it goes inside recyclerview with specific adapter that i wrote. and this button needs to be inside that recycler view because every button should pass id of every person from db.
Okay i got your point so basically what you're trying to do is access a button from another xml in your recycler view for that simply refer this code and do all changes in your adapter class here inside your oncreate view holder specify the xml file from which you'll be accessing the button:
public class ManagerTaskListAdapter extends RecyclerView.Adapter<ManagerTaskListAdapter.ProjectViewHolder> {
private ArrayList<Taskmsg> dataList;
private ArrayList<Project> projects;
Context mContext;
public ManagerTaskListAdapter(ArrayList<Taskmsg> dataList, Context mContext) {
this.dataList = dataList;
this.mContext=mContext;
}
#Override
public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.man_task_list_item, parent, false);
return new ProjectViewHolder(view);
}
#Override
public void onBindViewHolder(ProjectViewHolder holder, int position) {
String fname=dataList.get(position).getEmpFirstName();
String lname=dataList.get(position).getEmpLastName();
String prdesc=dataList.get(position).getTaskDescription();
String pid= dataList.get(position).getProjectId().toString();
String em=dataList.get(position).getTaskCreatedBy();
String taskid=dataList.get(position).getTaskId().toString();
prdesc = prdesc.replaceAll("\\<.*?\\>", "");
holder.txttname.setText(dataList.get(position).getTaskName());
holder.txttdesc.setText(prdesc);
holder.txttaskcrt.setText(dataList.get(position).getTaskCreatedBy());
holder.txtempname.setText(fname +" "+ lname);
holder.itemView.setOnClickListener(v -> {
Intent in=new Intent(mContext,TaskDetails.class);
in.putExtra("tid",taskid);
in.putExtra("pid",pid);
in.putExtra("email",em);
mContext.startActivity(in);
});
}
#Override
public int getItemCount() {
return dataList.size();
}
class ProjectViewHolder extends RecyclerView.ViewHolder {
TextView txttname,txttdesc,txtempname,txttaskcrt;
ProjectViewHolder(View itemView) {
super(itemView);
txttname = (TextView) itemView.findViewById(R.id.tv_taskname);
txttaskcrt=itemView.findViewById(R.id.tv_taskcreatedby);
txttdesc = (TextView) itemView.findViewById(R.id.tv_taskdescription);
txtempname = (TextView) itemView.findViewById(R.id.tv_empname);
}
}
}
Just change one line of findviewById.
btnPogled=(Button)findViewById(R.id.btnDodaj)
to
btnPogled=(Button)view.findViewById(R.id.btnDodaj)
I am making an android cv app but I want to implement the UI shown in the screenshot.
screenshot of ui I want
below current UI from real device
current ui
The XML layout where I have implemented my UI, which consists of an ImageView and some TextViews which shows subjects. I have implemented all the tasks but UI is not showing how I want it to show.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<ImageView
android:id="#+id/educationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/education_information"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/education_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="#string/education_information"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="#+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_duration"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/institution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_institution"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/degree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_degree"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<Space
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/university_subjects"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/university_subjects"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<include
layout="#layout/subject_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/subjects"
android:layout_marginTop="60dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have created another adapter and created dummy data in subjectivity
below adapter class
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below subject XML where I have hosted RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
below subject_list.xml where I have host items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlust"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:padding="5dp"
android:src="#drawable/computer_science"
android:layout_marginStart="10dp" />
<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:text="#string/computers_science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#color/colorWhite" />
</LinearLayout>
I have created fake data in order to host other images and texts
below fakeModel class
public class FakeData {
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSubjects() {
return subjects;
}
public void setSubjects(String subjects) {
this.subjects = subjects;
}
String image;
String subjects;
}
below adapter class where I have extended with RecyclerView
public class SubjectAdapter extends RecyclerView.Adapter {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below My Subject class where I have implemented fake images and data
public class SubjectActivity extends Activity {
List<FakeData> fakeData;
int [] subjectImage = {R.drawable.computer_science,
R.drawable.data_structure,
};
ListView list;
String[] subjectText = {
"Computer Science",
"Data Structure",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.list);
SubjectAdapter adapter = new SubjectAdapter(SubjectActivity.this, subjectImage,
subjectText, fakeData);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
}
}
This looks like you only have used Android Studio's drag and drop feature to position the UI elements. The problem is, Android Studio shows those UI elements in a generic device, which wont match all devices. When I was starting off with Android, this document helped me a lot to understand how elements in the UI must be placed.
https://developer.android.com/studio/write/layout-editor
You XML is malformed. Your LinearLayout orientation is wrong:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
It should be vertical.
Then validate its content. You have multiple RelativeLayout with orientation attribute.
It should be LinearLayout instead. RelativeLayouts don’t have orientation.
Besides those errors, your layout is very complex and have a deep hierarchy. This will lead to performance issues. My suggestion to you is to learn how to use ConstraintLayout.
The learning curve is a bit high, but it will be worth it!
I'm trying to adapt a recyclerview to display a list of objects. But actually I have some kind of wierd bug. My object have ID and Name data to display, but in the list, at first, I only can see the Id, and when I scroll down, only the rows going completely out of the screen are completely displayed...
This is my ListPresentActivity.
public class ListPresentActivity extends AppCompatActivity implements ViewInterface {
private static final String EXTRA_PRESENT_ID = "EXTRA_PRESENT_ID";
private static final String EXTRA_PRESENT_NAME = "EXTRA_PRESENT_NAME";
private List<PresentItem> listOfPresent;
private LayoutInflater layoutInflater;
private RecyclerView recyclerView;
private CustomAdapter adapter;
private PresentController presentController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_present);
recyclerView = (RecyclerView) findViewById(R.id.rec_list_activity);
recyclerView.setHasFixedSize(true);
layoutInflater = getLayoutInflater();
presentController = new PresentController(this, new FakePresentModel());
}
#Override
public void startDetailActivity(int id, String name, String info, String target, String advice, String price) {
//public void startDetailActivity(int id) {
Intent i = new Intent(this,PresentDetailActivity.class);
i.putExtra(EXTRA_PRESENT_ID, id);
i.putExtra(EXTRA_PRESENT_NAME, name);
startActivity(i);
}
#Override
public void setUpAdapterAndView(List<PresentItem> listOfPresent) {
this.listOfPresent = listOfPresent;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new CustomAdapter();
recyclerView.setAdapter(adapter);
}
private class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder>{
#Override
public CustomAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = layoutInflater.inflate(R.layout.item_present, parent, false);
return new CustomViewHolder(v);
}
#Override
public void onBindViewHolder(CustomAdapter.CustomViewHolder holder, int position) {
PresentItem currentPresent = listOfPresent.get(position);
holder.id.setText(
Integer.toString(currentPresent.getId())
);
holder.name.setText(
currentPresent.getName()
);
}
#Override
public int getItemCount() {
return listOfPresent.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView id;
private TextView name;
private ViewGroup container;
public CustomViewHolder(View itemView){
super(itemView);
this.id = (TextView) itemView.findViewById(R.id.texv_item_id);
this.name = (TextView) itemView.findViewById(R.id.texv_item_name);
this.container = (ViewGroup) itemView.findViewById(R.id.root_list_present);
this.container.setOnClickListener(this);
}
#Override
public void onClick(View v) {
PresentItem presentItem = listOfPresent.get(
this.getAdapterPosition()
);
presentController.onListItemClick(presentItem);
}
}
}
}
The layout item_present.xml
<android.support.constraint.ConstraintLayout 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="88dp"
android:id="#+id/root_list_present"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="#+id/texv_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textSize="48sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#id" />
<TextView
android:id="#+id/texv_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="84dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="collier" />
</android.support.constraint.ConstraintLayout>
And my list layout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.lubbee.bruh.view.ListPresentActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rec_list_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
I have search for a long time now, and the only concording result I have fount seems to be fixed with the recyclerView.setHasFixedSize(true); which is not my case...
Thanks for your time!
PS : If there is some code parts needed missing, just say the word! :]
Just after the first loading.
After one scroll to the bottom and back to the top of the screen
<LinearLayout android:layout_width="match_parent"
android:layout_height="88dp"
android:weightSum="1"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Try this simple LinearLayout it should work
You can also add other properties, but for now try it out
Friends i'm new to android and learning on my own. I was creating invoice manager app for learning. Here products are added by the admin and that is working fine. User has only permission to create invoice. When user arrives into create_invoice activity he has set of frame layouts in which one is for adding items. When user presses the frame layout he is made to see another activity where he can find all set of list items along with the product price in List view which admin has added. Now when user presses an item he is again brought back to create_invoice activity and a alert box appears which asks the qty required. When user enters the qty and clicks OK button, for the first the list item is displayed properly as i require. But when i add second item, 1st item gets replaced. So now my problem is how can i resolve this problem.. Guys please help me. Codes you people find may be very silly but i'm still learning. Thanks in advance.
Create_invoice activity
//data from activity invoice add_item
product_name = intent.getStringExtra("product_name");
product_price = intent.getDoubleExtra("product_price",0);
//product_qty = intent.getIntExtra("product_qty",0);
product_code = intent.getIntExtra("product_code",0);
if(product_name!= null && product_price!= 0 && product_code!= 0)
{
try {
builder = new AlertDialog.Builder(this);
builder.setTitle("Product Qty");
layoutInflater = LayoutInflater.from(Create_invoice.this);
view = layoutInflater.inflate(R.layout.dialoglayout_invoice,null);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
EditText etxt_dialog_qty=(EditText)view.findViewById(R.id.
etxt_dialog_qty);
int qty = Integer. parseInt (etxt_dialog_qty.getText().
toString().trim());
invoice_product_list products = new invoice_product_list
(product_name, product_price, qty, product_code);
//arraylist
ArrayList<invoice_product_list> productList = new ArrayList<>();
//customAdapter
customAdapterInvoice = new custom_adapter_invoice
(Create_invoice.this, productList);
customAdapterInvoice.add(products);
customAdapterInvoice.notifyDataSetChanged();
//listview in create_invoice activity
listView_additem = (ListView)
findViewById(R.id.listview_additem);
listView_additem.setAdapter(customAdapterInvoice);
alertDialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog = builder.create();
alertDialog.show();
}
catch (Exception e)
{
System.out.print(e);
}
}
customAdapter
public class custom_adapter_invoice extends ArrayAdapter
<invoice_product_list> {
public custom_adapter_invoice(Context context,
ArrayList<invoice_product_list> product_details) {
super(context, R.layout.custom_row_invoice_item, product_details);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view =
layoutInflater.inflate(R.layout.custom_row_invoice_item,parent,false);
invoice_product_list products = getItem(position);
TextView txt_product_name =
(TextView)view.findViewById(R.id.txt_product_name);
TextView txt_product_price =
(TextView)view.findViewById(R.id.txt_product_price);
TextView txt_product_qty =
(TextView)view.findViewById(R.id.txt_product_qty);
TextView txt_product_code =
(TextView)view.findViewById(R.id.txt_product_code);
txt_product_name.setText(products.getProduct_name());
txt_product_price.setText(String.valueOf(products.getProduct_price()));
txt_product_qty.setText(String.valueOf(products.getProduct_qty()));
txt_product_code.setText(String.valueOf(products.getProduct_code()));
return view;
}
create invoice activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.tournonstop.m.invoicemanager.Create_invoice"
tools:showIn="#layout/activity_create_invoice">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/darker_gray">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#android:color/white"
android:id="#+id/invoice_frame_company">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/company_name"
android:id="#+id/txt_company_name"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/invoice_date"
android:id="#+id/txt_invoice_date"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/invoice_no"
android:id="#+id/textView3"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#android:color/white"
android:id="#+id/invoice_frame_client"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/to"
android:id="#+id/txt_to"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txt_client_address"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:hint="#string/client_hint"/>
</FrameLayout>
----listview to add items-----
<FrameLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#android:color/white"
android:id="#+id/invoice_frame_add_item"
android:clickable="true">
<ListView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="#+id/listview_additem"
android:divider="#040404" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#android:color/white"
android:id="#+id/invoice_frame_sub_total">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/total_label"
android:id="#+id/txt_sub_total_label"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/total"
android:id="#+id/txt_sub_total"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:hint="#string/total_hint" />
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:text="#string/invoice_btn"
android:textStyle="bold"
android:background="#color/colorPrimaryDark"
android:textColor="#android:color/white"
android:clickable="true"
android:id="#+id/btn_invoice_save" />
</LinearLayout>
< /android.support.v4.widget.DrawerLayout>
invoice product list(getters and setters)
package com.tournonstop.m.invoicemanager;
public class invoice_product_list {
private String product_name;
private double product_price;
private int product_qty;
private int product_code;
public invoice_product_list(){
}
public invoice_product_list(String product_name,double
product_price,int product_qty,int product_code){
this.product_name = product_name;
this.product_price = product_price;
this.product_qty = product_qty;
this.product_code = product_code;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_code() {
return product_code;
}
public void setProduct_code(int product_code) {
this.product_code = product_code;
}
public double getProduct_price() {
return product_price;
}
public void setProduct_price(double product_price) {
this.product_price = product_price;
}
public int getProduct_qty() {
return product_qty;
}
public void setProduct_qty(int product_qty) {
this.product_qty = product_qty;
}
}
Add item to your list that you're using in adapter and (after verifying adapter is not null ) call method notifyDataSetChanged () on adapter object.
create_invoice activity
//arraylist
ArrayList<invoice_product_list> productList = new ArrayList<>();
//customAdapter
customAdapterInvoice = new custom_adapter_invoice
(Create_invoice.this, productList);
customAdapterInvoice.addProduct(products)
custom Adapter
public class custom_adapter_invoice extends ArrayAdapter <invoice_product_list>
{
ArrayList<invoice_product_list> productList = new ArrayList<>();
public custom_adapter_invoice(Context context,
ArrayList<invoice_product_list> product_details) {
super(context, R.layout.custom_row_invoice_item, product_details);
this.productList =product_details
}
public void addProduct(Product products
productList.add(products);
notifyDataSetChanged();
}
......
}
No,it does not change anything. If you want the latest method of working then I would prefer RecyclerView to custom list adapter. this link is very good for recyclerview
recyclerview demo series link