RecyclerView click on item, take some data then go to another activity - android

I want to take the reference when i click on an item in this recyclerview then go to another activity with this reference there is override functions in listview but recylclerview didn't have the same function
card.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="119dp"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="1dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="112dp">
<TextView
android:text="ID Article : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle1"
android:textStyle="normal|bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#android:color/black" />
<TextView
android:text="Reference : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_reference1"
android:textStyle="normal|bold"
android:layout_above="#+id/txt_des"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:textColor="#android:color/background_dark" />
<TextView
android:text="Prix : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des1"
android:textStyle="normal|bold"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="-51dp"
android:layout_below="#+id/txt_des"
android:textColor="#android:color/background_dark" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_des"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/txt_prix1"
android:layout_toEndOf="#+id/txt_prix1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_prix"
android:layout_alignBaseline="#+id/txt_des1"
android:layout_alignBottom="#+id/txt_des1"
android:layout_toRightOf="#+id/txt_des1"
android:layout_toEndOf="#+id/txt_des1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_idArticle"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txt_idArticle1"
android:layout_toEndOf="#+id/txt_idArticle1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_reference"
android:layout_alignBaseline="#+id/txt_reference1"
android:layout_alignBottom="#+id/txt_reference1"
android:layout_toRightOf="#+id/txt_reference1"
android:layout_toEndOf="#+id/txt_reference1" />
<TextView
android:text="Designation : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_prix1"
android:textStyle="normal|bold"
android:textColor="#android:color/black"
android:layout_below="#+id/txt_reference1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView> </LinearLayout>
avticity_recherche_art.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"
xmlns:article_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_recherche_art"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.bacha.pfe.activity.recherche_art">
<TextView
android:text="Recherche Article :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="rechercher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnrechArt"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/eTextRechArt" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/eTextRechArt"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/btnrechArt"
android:layout_toStartOf="#+id/btnrechArt" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
article_view:cardUseCompatPadding="true"
article_view:cardElevation="5dp"
article_view:cardCornerRadius="5dp"
android:scrollbars="vertical"
android:layout_marginTop="18dp"
android:layout_below="#+id/btnrechArt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
recherche_art.java:
package com.example.bacha.pfe.activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.bacha.pfe.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private Button btnrechArt ;
private EditText arech;
int d=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (Button) findViewById(R.id.btnrechArt);
btnrechArt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(0);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
Log.d("PFE", String.valueOf(d));
load_article_from_server(d);
}
}
});
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
#Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}

You have to implement a listener in the holder that you are using in the ArticleAdapter.
Look this: RecyclerView onClick

Related

In Android, Is it possible to make Text Tag separation in editext Like Stack overflow (see the image)?

I want to create this kind of separation between commas(,)? like StackOverflow Tags
Try this:
In MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import me.originqiu.library.EditTag;
public class MainActivity extends AppCompatActivity
implements SwitchCompat.OnCheckedChangeListener {
private EditTag editTagView;
private SwitchCompat statusSwitchView;
private List<String> tagStrings = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTagView = (EditTag) findViewById(R.id.edit_tag_view);
statusSwitchView = (SwitchCompat) findViewById(R.id.status_switch);
statusSwitchView.setOnCheckedChangeListener(this);
for (int i = 0; i < 10; i++) {
tagStrings.add("test" + i);
}
//Set tag add callback before set tag list
editTagView.setTagAddCallBack(new EditTag.TagAddCallback() {
#Override
public boolean onTagAdd(String tagValue) {
if ("test1".equals(tagValue)) {
return false;
} else {
return true;
}
}
});
editTagView.setTagDeletedCallback(new EditTag.TagDeletedCallback() {
#Override
public void onTagDelete(String deletedTagValue) {
Toast.makeText(MainActivity.this, deletedTagValue, Toast.LENGTH_SHORT).show();
}
});
editTagView.setTagList(tagStrings);
editTagView.addTag("hello world!");
editTagView.removeTag("test3");
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editTagView.setEditable(isChecked);
}
}
In activity_main.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="me.originqiu.edittag.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<me.originqiu.library.EditTag
android:id="#+id/edit_tag_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:input_layout="#layout/view_sample_input_tag"
app:tag_layout="#layout/view_sample_tag" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="4dp"
android:background="#color/colorAccent" />
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="#+id/status_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:padding="16dp"
android:text="open editable status" />
</LinearLayout>
In view_sample_input_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<me.originqiu.library.MEditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:minWidth="56dp"
android:drawablePadding="2dp"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:singleLine="true"
android:maxEms="30"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:hint="Add Tag "
android:imeOptions="actionDone"
android:textColor="#color/dark_clr"
android:textSize="14sp"/>
In view_sample_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:background="#drawable/bg_sample_tag"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:textColor="#fff"
android:textSize="14sp"
android:textStyle="bold" />
See here for more details: https://github.com/qiugang/EditTag/blob/master/sample/src/main/res/layout/view_sample_tag.xml

Generating QR Code on different activity in android

i have been trying to create a QR Code generator that generates the QR Code on a separate activity on button click. Below is my code but it crushes on button click. Where did i go wrong?
First class is MainActivity.class:
package com.example.profmox.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public class MainActivity extends AppCompatActivity {
TextView tx1;
Button btn1, btn2;
EditText edt1,edt2;
CheckBox ch;
String text2qr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = (EditText)findViewById(R.id.edit1);
edt2 = (EditText)findViewById(R.id.edit2);
btn1 = (Button)findViewById(R.id.login);
btn2 = (Button)findViewById(R.id.signup);
ch = (CheckBox)findViewById(R.id.check);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("EditText", edt1.getText().toString());
Log.v("EditText", edt2.getText().toString());
// text2qr = edt1.getText().toString().trim();
// MultiFormatWriter mfw = new MultiFormatWriter();
// try{
// BitMatrix btm = mfw.encode(text2qr, BarcodeFormat.QR_CODE,250,250);
// BarcodeEncoder ben = new BarcodeEncoder();
// Bitmap bmap = ben.createBitmap(btm);
// UserScreen.qr.setImageBitmap(bmap);
// }catch (WriterException e){
// e.printStackTrace();
// }
startActivity(new Intent(MainActivity.this, UserScreen.class));
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SignUp.class));
}
});
}
}
The layout file for the MainActivity class:
<?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:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/art_background"
tools:context="com.example.profmox.myapplication.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:padding="16dp"
android:layout_marginTop="20dp"
android:src="#mipmap/logo"/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Username"
android:textColorHint="#fff"
android:maxLines="1"
android:drawableLeft="#drawable/girl"
android:layout_marginTop="70dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:background="#android:color/transparent"
android:drawablePadding="12dp"
android:padding="8dp"
android:hint="Password"
android:textColorHint="#fff"
android:maxLines="1"
android:layout_marginTop="4dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F2CC8F"/>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#fff"
android:text="Remember Me"
android:padding="9dp"
/>
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/login_art"
android:text="Login"
android:textColor="#3D405B"
android:textAllCaps="false"
android:padding="16dp"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_marginTop="23dp"
android:textSize="18sp"/>
<Button
android:id="#+id/signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/signuo_art"
android:text="Sign up"
android:textColor="#fff"
style="#style/Base.TextAppearance.AppCompat.Body1"
android:textAllCaps="false"
android:textSize="18sp"
android:layout_marginTop="16dp"
android:clickable="true"
android:padding="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot your password?"
android:clickable="true"
style="#style/Base.TextAppearance.AppCompat.Body2"
android:padding="16dp"
android:layout_marginBottom="12dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
The UserScreen.class is where i want to generate the QR Code in:
package com.example.profmox.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class UserScreen extends AppCompatActivity {
static ImageView qr;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_screen);
qr = (ImageView)findViewById(R.id.qrCode);
}
}
The layout file for the UserScreen.class:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/art_background"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.profmox.myapplication.UserScreen">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/qrCode"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Implementing formulas into each value in the spinner which will change the result everytime the user change selected value

I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.
To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.
This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class LooseWeight extends AppCompatActivity {
TextView TotalCal;
EditText numb2;
Spinner ActLvl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loose_weight);
numb2 = (EditText) findViewById(R.id.value1);
ActLvl = (Spinner) findViewById(R.id.value2);
TotalCal = (TextView)findViewById(R.id.resultDisplay);
final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ActLvl.setAdapter(myAdapter);
Button calcBtn = (Button)findViewById(R.id.result);
calcBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
float numb3 = Float.parseFloat(numb2.getText().toString());
float a = numb3 * 24;
float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
float c = b - 500;
TotalCal.setText(Float.toString(c));
}
});
Button extra = (Button)findViewById(R.id.advance);
extra.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
startActivity(intent);
}
});
}
}
Here is the MoreLooseWeightDetails.class
package com.fitup.fit_up;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MoreLooseWeightDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_loose_weight_details);
}
}
Here is the MoreLooseWeightDetails.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:id="#+id/activity_more_loose_weight_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.fitup.fit_up.MoreLooseWeightDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Title3"
tools:text="More"
android:textSize="36sp" />
<TextView
android:text="Select Type of Diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Title3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/Info3"
android:textSize="30sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Info3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/spinner" />
<TextView
android:text="You Should Eat :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/Info4"
android:textSize="30sp"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Protein (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit1"
android:textSize="24sp"
android:layout_below="#+id/Info4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:text="Fat (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit3"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/result1"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignLeft="#+id/result2"
android:layout_alignStart="#+id/result2" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/unit2"
android:id="#+id/result2"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/result3"
android:layout_alignStart="#+id/result3" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit2"
android:id="#+id/result3"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/unit2"
android:layout_toEndOf="#+id/unit2" />
<TextView
android:text="Carbohydrates (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/unit2"
android:textSize="24sp"
android:layout_below="#+id/unit1"
android:layout_alignRight="#+id/unit1"
android:layout_alignEnd="#+id/unit1" />
</RelativeLayout>

OnItemClickListener doesn't work , OnItemClick is not called

I'm trying to make a Toast message whenever I click an item from the list, For some reason when I click the item nothing happens,
I hope you can help me, thanks.
ProductList.java
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProductList extends AppCompatActivity {
private List<myProductsView> myProducts_types = new ArrayList<myProductsView>();
ArrayAdapter<myProductsView> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
//Button btnDel = (Button) findViewById(R.id.btnDel);
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,list);
populateProductsList();
populateListView();
}
private void populateProductsList() {
myProducts_types.add(new myProductsView("aaa", "aaa", 1111, 12.90, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaaaaa ", 1112, 10.90, R.drawable.cereal, 2));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 30.00, R.drawable.cereal, 1));
myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 20.00, R.drawable.cereal, 3));
}
private void populateListView() {
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.product_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("BLALBALBLALBLABLAL");
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
}
});
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
public class MyListAdapter extends ArrayAdapter<myProductsView>{
public MyListAdapter(){
super(ProductList.this, R.layout.pop_productlist, myProducts_types);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure we have a view to work with(may have been given null
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false);
}
//we need to populate the list
//find the product to work with
myProductsView currentProduct = myProducts_types.get(position);
//fill the view
CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ProductList.this,"BLA",Toast.LENGTH_SHORT);
}
});
TextView productname = (TextView) itemView.findViewById(R.id.product_name);
productname.setText(currentProduct.getProductName());
EditText quantity = (EditText)itemView.findViewById(R.id.edit_text);
quantity.setText(String.valueOf(currentProduct.getQuantity()));
return itemView;
}
}
}
product_item_view.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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="7dp"
android:layout_weight="1">
<Button
android:id="#+id/btn_minus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="-" />
<EditText
android:id="#+id/edit_text"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:gravity="center"
android:focusable="false"
android:text="0" />
<Button
android:id="#+id/btn_plus"
android:layout_width="35dp"
android:layout_height="40dp"
android:text="+" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Text"
android:textSize="15dp"
android:textIsSelectable="true"
android:gravity="center"
android:id="#+id/product_name"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/product_checkBox"
android:layout_toStartOf="#+id/product_checkBox" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/product_checkBox" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
pop_productlist.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="match_parent"
android:background="#drawable/ightwall"
android:id="#+id/drawerlayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="הרשימה שלי"
android:textAlignment="center"
android:textSize="30dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore"
>
</ListView>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/CalckButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calckButton"
android:onClick="StartCalck"
android:layout_gravity="center" />
<Button
android:id="#+id/btnDel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/lblBtnDel"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#D5D4D4"
android:layout_gravity="bottom">
<ImageView
android:id="#+id/mylist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_mylist"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/freeadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_freetext"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/favproductadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/createlist_favproduct"
android:adjustViewBounds="true"
android:maxHeight="90dp"
android:maxWidth="90dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</LinearLayout>
</LinearLayout>
You have to call .show() on your toast.
change
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT);
to
Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT).show();

How to include GridView and ListView together in one layout

I am building an app where i have to use both gridview and listview in same activity but the problem is gridview items are clickable but not the listview this is my xml of activity...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow2"
android:layout_alignRight="#+id/tableRow1"
android:layout_below="#+id/tableRow2"
android:layout_marginTop="39dp"
android:background="#drawable/border"
android:numColumns="3" >
</GridView>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="180dp"
android:text="Run Background" />
<Button
android:id="#+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Add To Cart" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableRow1"
android:layout_below="#+id/tableRow1"
android:layout_marginTop="25dp" >
<Button
android:id="#+id/button3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Edit Cart" />
<EditText
android:id="#+id/editText1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tableRow2"
android:layout_marginLeft="200dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="#+id/tableRow2"
android:background="#layout/editext_background"
android:ems="10"
android:hint="Quantity" />
</TableRow>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:layout_marginLeft="22dp" >
<TextView
android:id="#+id/tv_slno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Item Name"
android:textColor="#000" />
<TextView
android:id="#+id/tv_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Quantity"
android:textColor="#000" />
<TextView
android:id="#+id/tv_phone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Price"
android:textColor="#000" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/linearLayout1"
android:layout_alignTop="#+id/tableRow1"
android:layout_marginRight="105dp"
android:text="Bill"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/tableRow2"
android:layout_toLeftOf="#+id/gridView1" >
</ListView>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:layout_alignBottom="#+id/button4"
android:layout_alignRight="#+id/linearLayout1"
android:text="Tender" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:text="Delete" />
please help me how to make listview item and row clickable ......
Have you tried android:clickable="true"?
like:
<TableRow
android:id="#+id/tableRow1"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp" >
You need to determine the height of the grid view first. Use this tutorial.
http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html
Firstly, you only need to put 2 RecyclerViews object to your activity/fragment layout. For better scroll later, please wrap theme in a NestedScrollView like this:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#ffffe0"
tools:context="info.devexchanges.gridlistrecyclerview.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="#string/os"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/corporation"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Of course, you always need to creating layout for each RecyclerView item. In this every item is a CardView:
item_list.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/image"
android:gravity="center" />
</RelativeLayout>
</android.support.v7.widget.CardView>
item_grid.xml
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="#+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:contentDescription="#null" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_marginTop="5dp"
android:background="#color/colorPrimaryDark"
android:gravity="center"
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Now, we must create 2 adapter classes for 2 RecyclerViews based on RecyclerView.Adapter:
ListViewAdapter.java
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Activity activity;
private List<RecyclerViewItem> items;
public ListViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
#Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.image);
textView = (TextView)view.findViewById(R.id.text);
}
}
}
GridViewAdapter.java
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder> {
private List<RecyclerViewItem> items;
private Activity activity;
public GridViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_grid, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(GridViewAdapter.ViewHolder viewHolder, int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
#Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
textView = (TextView)view.findViewById(R.id.text);
imageView = (ImageView) view.findViewById(R.id.image);
}
}
}
There is no special point in your activity or fragment programmatically code, locating all xml elements from layout file, create LayoutManager for RecyclerViews, initializing adapters and attaching them,...This is full code for my main activity:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import info.devexchanges.gridlistrecyclerview.adapter.GridViewAdapter;
import info.devexchanges.gridlistrecyclerview.adapter.ListViewAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView listView;
private RecyclerView gridView;
private ListViewAdapter listViewAdapter;
private GridViewAdapter gridViewAdapter;
private ArrayList<RecyclerViewItem> corporations;
private ArrayList<RecyclerViewItem> operatingSystems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (RecyclerView) findViewById(R.id.list);
gridView = (RecyclerView) findViewById(R.id.grid);
setDummyData();
listView.setHasFixedSize(true);
gridView.setHasFixedSize(true);
//set layout manager and adapter for "ListView"
LinearLayoutManager horizontalManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
listView.setLayoutManager(horizontalManager);
listViewAdapter = new ListViewAdapter(this, corporations);
listView.setAdapter(listViewAdapter);
//set layout manager and adapter for "GridView"
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
gridView.setLayoutManager(layoutManager);
gridViewAdapter = new GridViewAdapter(this, operatingSystems);
gridView.setAdapter(gridViewAdapter);
}
private void setDummyData() {
corporations = new ArrayList<>();
corporations.add(new RecyclerViewItem(R.drawable.microsoft, "Microsoft"));
corporations.add(new RecyclerViewItem(R.drawable.apple, "Apple"));
corporations.add(new RecyclerViewItem(R.drawable.google, "Google"));
corporations.add(new RecyclerViewItem(R.drawable.oracle, "Oracle"));
corporations.add(new RecyclerViewItem(R.drawable.yahoo, "Yahoo"));
corporations.add(new RecyclerViewItem(R.drawable.mozilla, "Mozilla"));
operatingSystems = new ArrayList<>();
operatingSystems.add(new RecyclerViewItem(R.drawable.bbos, "BlackBerry OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.ios, "iOS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.tizen, "Tizen"));
operatingSystems.add(new RecyclerViewItem(R.drawable.android, "Android"));
operatingSystems.add(new RecyclerViewItem(R.drawable.symbian, "Symbian"));
operatingSystems.add(new RecyclerViewItem(R.drawable.firefox_os, "Firefox OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.wp_os, "Windows Phone OS"));
}
}
And this is the POJO class of this project:
RecyclerViewItem.java
public class RecyclerViewItem {
private int drawableId;
private String name;
public RecyclerViewItem(int drawableId, String name) {
this.drawableId = drawableId;
this.name = name;
}
public int getDrawableId() {
return drawableId;
}
public String getName() {
return name;
}
}
NOTE: Never forget to put RecyclerView and CardView dependencies to your app-level build.gradle file:
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'

Categories

Resources