I design one app where I am displaying image using recyclerView.
I am not clicking image from camera, its downloded from json api.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="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:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
tools:context="com.bjp.app.bjpapp.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</RelativeLayout>
layout_design.xml
this layout file designed for the component views and this file attaching with recyclerView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/image"
android:src="#drawable/logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/image"
android:layout_weight=".2"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:id="#+id/share"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="#drawable/share"
android:layout_marginRight="5dp"
android:theme="#style/MyButton1"/>
<ImageButton
android:id="#+id/download"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="#drawable/downward"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:theme="#style/MyButton1"/>
</LinearLayout>
</RelativeLayout>
Data.java (Modal class.)
this is my getter and setter class file code.
public class Data implements Serializable {
private int id;
private String imgUrl;
public Data(int id, String imgUrl) {
this.id = id;
this.imgUrl = imgUrl;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}
Adapter class file code is here
Image_Adaper.java
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private List<Data> data;
private Context context;
private Bitmap bitmap;
String url1;
String filepath;
public ImageAdapter(List<Data> data, Context context) {
this.data = data;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_design,parent,false);
return new ImageAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Data data1=data.get(position);
url1=data1.getImgUrl();
ImageView imageView=holder.imageView;
Glide.with(context)
.load(data1.getImgUrl())
.placeholder(R.drawable.logo)
.into(imageView);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public ImageButton share,download;
public ViewHolder(final View itemView) {
super(itemView);
imageView=(ImageView)itemView.findViewById(R.id.image);
share=(ImageButton)itemView.findViewById(R.id.share);
download=(ImageButton)itemView.findViewById(R.id.download);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position=getAdapterPosition();
Data data2=data.get(position);
// i want to share a image with social sites
Toast.makeText(context,"Product id "+data2+" btn working",Toast.LENGTH_LONG).show();
}
});
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"download btn working"+url1,Toast.LENGTH_LONG).show();
}
});
}
}
}
I am attaching the mail layout screenshot for more understanding.
The image should be downloaded on click of the download button.
and Image should be shareable to social apps.
I tried some code but it is not working.
Please, guys, help me to find solutions.
Thank you in advance.
please find the attachment of this image
Please refer below link for download or save image from image view
Save image from ImageView to device gallery
For Sharing image foloww below link
https://developer.android.com/training/sharing/send.html
Related
I am using the SwipeCard library and the shapeableimageview is stored in a separate xml file that is connected to the main layout activity xml. there is also an adapter for this SwipeCard. data is not received although there are no errors in the code
activity with receive method
private void getUsermenwomInfo()
{
DatabaseReference reference= FirebaseDatabase.getInstance().getReference()
.child("Userwoman_men");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()&&snapshot.getChildrenCount()>0)
{
String name=snapshot.child("name").getValue().toString();
nameusercard.setText(name);
if (snapshot.hasChild("image")) {
String image = snapshot.child("image").getValue().toString();
Picasso.get().load(image).into(imageosnovnoe);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
xml with shapeableimageview
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_margin="15dp"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<LinearLayout
android:id="#+id/linearmain"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/imageosnovnoe"
android:layout_width="match_parent"
android:layout_height="400dp"
android:backgroundTint="#color/white"
android:scaleType="fitXY"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearhori"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:weightSum="3">
<ImageView
android:id="#+id/like"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/sas"
/>
<ImageView
android:id="#+id/disslike"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/aan"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
her adapter java class
public class SwipeAdapter extends BaseAdapter {
private Context context;
private List<Integer> list;
public SwipeAdapter(Context context,List<Integer>list) {
this.context=context;
this.list=list;
}
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View view;
if(convertView==null)
{
view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_koloda,parent,false);
}else {
view=convertView;
}
return view;
}
}
how to change my code or write a new one so that image and textview data is taken from firebase and inserted into shapeableimageview and textview from this separate xml? and is it correct that I wrote the method inside the main activity and not in the adapter? Thanks in advance for your help
I am making an app that let users create meetings and I would like the details of the meeting (eg name, type, location) in a recycler view.I ran the php code by itself and was able to get the values from the database.
Currently, I am getting an error of
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at in recyclerviewAdapter
holder.Name.setText(users.getName());
holder.Type.setText(users.getType());
holder.Location.setText(users.getLocation());
which is linked to
public class Users {
private String name,type,location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
recyclerviewAdapter
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserHolder>{
Context context;
List<Users> usersList;
public UserAdapter(Context context, List<Users> usersList) {
this.context = context;
this.usersList = usersList;
}
#NonNull
#Override
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
return new UserHolder(userLayout);
}
#Override
public void onBindViewHolder(#NonNull UserHolder holder, int position) {
Users users=usersList.get(position);
holder.Name.setText(users.getName());
holder.Type.setText(users.getType());
holder.Location.setText(users.getLocation());
}
#Override
public int getItemCount() {
return usersList.size();
}
public class UserHolder extends RecyclerView.ViewHolder{
TextView Name,Type,Location;
public UserHolder(#NonNull View itemView){
super(itemView);
Name=itemView.findViewById(R.id.textTitle);
Type=itemView.findViewById(R.id.textType);
Location=itemView.findViewById(R.id.textLocation);
}
}
}
main activity code
public class MainActivity2 extends AppCompatActivity {
private static final String URL = "http://10.0.2.2/mad/activitydisplay.php";
RecyclerView recyclerView;
UserAdapter userAdapter;
List<Users> usersList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView = findViewById(R.id.recylcerList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
usersList=new ArrayList<>();
LoadAllUser();
}
private void LoadAllUser() {
JsonArrayRequest request =new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray array) {
for (int i=0;i<array.length();i++){
try {
JSONObject object=array.getJSONObject(i);
String name=object.getString("eventname").trim();
String type=object.getString("type").trim();
String location=object.getString("location").trim();
Users user= new Users();
user.setName(name);
user.setType(type);
user.setLocation(location);
usersList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
userAdapter=new UserAdapter(MainActivity2.this,usersList);
recyclerView.setAdapter(userAdapter);
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MainActivity2.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue=Volley.newRequestQueue(MainActivity2.this);
requestQueue.add((request));
}
}
php code to get data from table
$sql="SELECT * FROM `activitytable` ";
$result=mysqli_query($conn,$sql);
$user=array();
while($row = mysqli_fetch_assoc($result)){
$index['eventname']=$row['eventname'];
$index['type']=$row['type'];
$index['location']=$row['location'];
array_push($user, $index);
}
echo json_encode($user);
activity_main2.xml
<?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="match_parent"
tools:context=".MainActivity2">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recylcerList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_list resource file
<?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="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textTitle"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textType"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:"
android:textColor="#color/black"
android:textSize="24dp" />
<TextView
android:id="#+id/textLocation"
style="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
instead of
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main2,parent,false);
return new UserHolder(userLayout);
}
use this
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View userLayout= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list ,parent,false);
return new UserHolder(userLayout);
}
because activity_main2 doesn't contain R.id.textTitle,R.id.textType,R.id.textLocation
check your xml(R.layout.activity_main2) looks like id of your view are from another layout
Hello guys I want to display one single item between two grid row items.
I want this type of out.
I'm try this way but it didn't work.
My Activity layout XML activity_grid_with_single_rv.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"
tools:context=".Activitry.GridWithSingleRVActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvGridWithSingle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"/>
</LinearLayout>
My Activity class GridWithSingleRVActivity.java
public class GridWithSingleRVActivity extends AppCompatActivity {
private RecyclerView rvGridWithSingle;
private GridWIthSingleRVAdapter gridWIthSingleRVAdapter;
private ArrayList<GridWithSingle> gridWithSingles=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_with_single_rv);
rvGridWithSingle=findViewById(R.id.rvGridWithSingle);
getGridWithSingles();
rvGridWithSingle.setLayoutManager(new GridLayoutManager(this,2));
gridWIthSingleRVAdapter=new GridWIthSingleRVAdapter(this,gridWithSingles);
rvGridWithSingle.setAdapter(gridWIthSingleRVAdapter);
}
private ArrayList<GridWithSingle> getGridWithSingles(){
gridWithSingles.clear();
gridWithSingles=new ArrayList<>();
int addSingleCnt=0;
for (int i=0;i<20;i++){
gridWithSingles.add(new GridWithSingle(i+1,"GRID : "+(i+1)));
if (addSingleCnt==1){
addSingleCnt=0;
gridWithSingles.add(new GridWithSingle(-1,"Single : "));
}else {
addSingleCnt++;
}
}
return gridWithSingles;
}
}
My Adapter class GridWIthSingleRVAdapter.java
public class GridWIthSingleRVAdapter extends RecyclerView.Adapter<GridWIthSingleRVAdapter.GridWIthSingleRVVH> {
private Context context;
private ArrayList<GridWithSingle> gridWithSingles=new ArrayList<>();
private final int GRID_LAYOUT=1;
private final int SINGLE_LAYOUT=0;
public GridWIthSingleRVAdapter(Context context, ArrayList<GridWithSingle> gridWithSingles) {
this.context = context;
this.gridWithSingles = gridWithSingles;
}
#NonNull
#Override
public GridWIthSingleRVVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType==GRID_LAYOUT)
return new GridWIthSingleRVVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_grid_item,parent,false));
else
return new GridWIthSingleRVVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_single_item,parent,false));
}
#Override
public void onBindViewHolder(#NonNull GridWIthSingleRVVH holder, int position) {
if (holder.getItemViewType()==GRID_LAYOUT){
holder.txtGrid.setText(gridWithSingles.get(position).getName());
}else {
holder.txtSingle.setText(gridWithSingles.get(position).getName());
}
}
#Override
public int getItemCount() {
return gridWithSingles.size();
}
#Override
public int getItemViewType(int position) {
if (gridWithSingles.get(position).getId()==-1){
return SINGLE_LAYOUT;
}else {
return GRID_LAYOUT;
}
}
public class GridWIthSingleRVVH extends RecyclerView.ViewHolder{
private TextView txtGrid,txtSingle;
public GridWIthSingleRVVH(#NonNull View itemView) {
super(itemView);
txtGrid=itemView.findViewById(R.id.txtGrid);
txtSingle=itemView.findViewById(R.id.txtSingle);
}
}
}
Two layout item xml
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cvGrid"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardBackgroundColor="#EF9A9A">
<TextView
android:id="#+id/txtGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GRID"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/black"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
single_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cvSingle"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardBackgroundColor="#CE93D8">
<TextView
android:id="#+id/txtSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SINGLE"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/black"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
Output
You need to specify a spanSizeLookup for the affected columns to the layoutManager.
A couple of years ago I wrote a similar proof of concepts where the idea was to insert "ads" every 4 rows.
The project is located on GitHub.
The sample does this the trick directly in the activity by using recyclerView types and modifying the span size.
layoutManager = GridLayoutManager(this, 2)
mainRecyclerView.layoutManager = layoutManager
mainRecyclerView.adapter = adapter
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (adapter.getItemViewType(position)) {
adapter.viewTypeGreen -> 1
adapter.viewTypePurple -> 1
adapter.viewTypeAd -> 2
else -> -1
}
}
}
The repository that provides this data simulates inserting a different type every 4 items
I'm developing an android app using java and Firebase. In that I'm using a recycler view to display the images and other contents which are stored in Firebase database. When I run the program the other contents are displaying correctly but the image is not showing. This is my first time developing an android application so I'm confused.
Any help you can provide me to get this to work would be great.
Here is my code,
Adapter class
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyMessageHolder> {
private StorageReference storage;
private Context context;
private ArrayList<profile> Messages;
public MessageAdapter(Context c , ArrayList<profile> p)
{
context = c;
Messages =p;
}
#NonNull
#Override
public MyMessageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyMessageHolder(LayoutInflater.from(context).inflate(R.layout.activity_card_view,parent,false));
}
#Override
public void onBindViewHolder(#NonNull MyMessageHolder holder, int position) {
profile prof = Messages.get(position);
holder.name.setText(Messages.get(position).getTitle());
holder.description.setText(Messages.get(position).getDescription());
holder.date.setText(Messages.get(position).getDate());
//Picasso.get().load(Messages.get(position).getImage()).into(holder.image);
Picasso.get()
.load(prof.getImage())
.fit()
.centerCrop()
.into(holder.image);
}
#Override
public int getItemCount() {
return Messages.size();
}
class MyMessageHolder extends RecyclerView.ViewHolder
{
TextView name,description,date,time;
ImageView image;
public MyMessageHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.hTitle);
description = itemView.findViewById(R.id.hDescription);
date = itemView.findViewById(R.id.hDate);
time = itemView.findViewById(R.id.hTime);
image = itemView.findViewById(R.id.hImage);
}
}
}
Main activity class
class MainActivityHomePage extends AppCompatActivity {
private DatabaseReference reference;
RecyclerView recyclerView;
ArrayList<profile> list;
MessageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
reference = FirebaseDatabase.getInstance().getReference().child("Messages");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
profile p = dataSnapshot1.getValue(profile.class);
list.add(p);
}
adapter = new MessageAdapter( MainActivityHomePage.this,list);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(MainActivityHomePage.this, "something wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
Model class
package com.eNotification.getnotify;
public class profile {
private String title;
private String description;
private String date;
private String image;
public profile() {
}
public profile(String title, String description, String date, String image) {
this.title = title;
this.description = description;
this.date = date;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
card view layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="170dp"
app:cardBackgroundColor="#android:color/white"
android:layout_marginTop="10dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/silver"
android:padding="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="260dp"
android:layout_height="match_parent"
android:padding="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/hTitle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:background="#color/lightSilver"
android:hint="Title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/hTime" />
<TextView
android:id="#+id/hDescription"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:background="#color/lightSilver"
android:text="Description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/hTitle" />
<TextView
android:id="#+id/hDate"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginEnd="4dp"
android:background="#color/lightSilver"
android:text="Date"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/hTime"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/hTime"
android:layout_width="116dp"
android:layout_height="30dp"
android:layout_marginStart="4dp"
android:background="#color/lightSilver"
android:text="Time"
app:layout_constraintStart_toEndOf="#id/hDate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="#+id/hImage"
android:layout_width="160dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.834" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Recycler view xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:padding="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to show an image and text each list view item. I read a few tutorial but I still could not.
I'm getting this error when I try to open TagsActivity:
Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
In this line:
this.tagList.setAdapter(adapter);
TagsActivity.java
public class TagsActivity extends AppCompatActivity {
public ListView tagList;
final List<Tag> tags = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
tags.add(new Tag("Black & White"));
tags.add(new Tag("Geometric"));
this.tagList = (ListView) this.findViewById(R.id.list_tags);
LazyAdapter adapter = new LazyAdapter(this, this.tags);
this.tagList.setAdapter(adapter);
}
}
Tag.java
public class Tag {
private String title;
public Tag(String title) {
super();
this.title = title;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}
JazyAdapter.java
public class LazyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Tag> tags;
public LazyAdapter(Activity activity, List<Tag> tags) {
this.inflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.tags = tags;
}
#Override
public int getCount() {
return this.tags.size();
}
#Override
public Tag getItem(int position) {
return this.tags.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView (int position, View convertView, ViewGroup parent) {
View row;
row = this.inflater.inflate(R.layout.list_item, null);
TextView textView = (TextView) row.findViewById(R.id.title);
ImageView imageView = (ImageView) row.findViewById(R.id.icon);
Tag tag = this.tags.get(position);
textView.setText(tag.getTitle());
imageView.setImageResource(R.drawable.icon);
return row;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="#drawable/border_bottom">
<LinearLayout
android:id="#+id/thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true">
<ImageView
android:id="#+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/icon" />
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumb"
android:layout_toRightOf="#+id/thumb"
android:layout_toEndOf="#+id/thumb"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textSize="24sp"
android:text="Black & White"
android:paddingTop="5dp"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/arrow_right"/>
</RelativeLayout>
activity_tags.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}" >
<ListView
android:id="#+id/list_tags"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
How can I solve this?
You're getting this error because of "View row" is null. Just replace that in your LazyLoader.java
View row;
row = this.inflater.inflate(R.layout.list_item, null);
With:
View row = convertView;
if (row == null) {
row = this.inflater.inflate(R.layout.list_item, null);
}
this.setContentView(R.layout.activity_main); should be this.setContentView(R.layout.activity_tags);