0

Hey guys I have initialized the list view in my android quotation application. Now I want when the user tape on for example the life quotes then all the quotes of life should show on cardView separately. I don't know how to show each quote on each cardView please direct me to the right path I'll be very thankful.

this is the quotes activity from where i go to all other activities

public class QoutesActivity extends AppCompatActivity {

private ListView listView;

// Listview Adapter
ArrayAdapter<String> adapter;


String[] listItems = {

        "Happiness Quotes",
        "Attitude Quotes",
        "Anniversary Quotes",
        "Life Quotes",
        "Alone Quotes",
        "Strength Quotes",
        "Patriotism Quotes" ,
        "Hope Quotes" ,
        "Inspirational Quotes" ,
        "Independence Quotes" ,
        "Childhood Quotes" ,
        "Trust Quotes",
        "Selfie Quotes",
        "Birthday Quotes" ,
        "Knowledge Quotes" ,
        "Love Quotes" ,
        "Nature Quotes"
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qoutes);

    listView =  findViewById(R.id.list_view);


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(QoutesActivity.this,
            android.R.layout.simple_list_item_1,listItems) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the current item from ListView
            View view = super.getView(position, convertView, parent);

            // Get the Layout Parameters for ListView Current Item View
            ViewGroup.LayoutParams params = view.getLayoutParams();

            // Set the height of the Item View
            params.height = 180;
            view.setLayoutParams(params);

            return view;

        }

    };
    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String itemValue = (String) listView.getItemAtPosition(position);

            if (position == 0) {
                //code specific to first list item
                Intent myIntent = new Intent(view.getContext(), HappinessActivity.class);

                startActivityForResult(myIntent, 0);
            }else if (position == 1){

                Intent myIntent = new Intent(view.getContext(), AttitudeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 2){
                Intent myIntent = new Intent(view.getContext(), AnniversaryActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 3){
                Intent myIntent = new Intent(view.getContext(), LifeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 4){
                Intent myIntent = new Intent(view.getContext(), AloneActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 5){
                Intent myIntent = new Intent(view.getContext(), StrengthActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 6){
                Intent myIntent = new Intent(view.getContext(), PatriotismActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 7){
                Intent myIntent = new Intent(view.getContext(), HopeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 8){
                Intent myIntent = new Intent(view.getContext(), InspitationalActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 9){
                Intent myIntent = new Intent(view.getContext(), IndependenceActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 10){
                Intent myIntent = new Intent(view.getContext(), ChildhoodActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 11){
                Intent myIntent = new Intent(view.getContext(), TrustActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 12){
                Intent myIntent = new Intent(view.getContext(), SelfieActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 13){
                Intent myIntent = new Intent(view.getContext(), BirthdayActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 14){
                Intent myIntent = new Intent(view.getContext(), KnowledgeActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 15){
                Intent myIntent = new Intent(view.getContext(), LoveActivity.class);
                startActivityForResult(myIntent, 0);
            }else if (position == 16){
                Intent myIntent = new Intent(view.getContext(), NatureActivity.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });

}

}

and this this the xml file

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layercontainer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg3">

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Sufyan Hashmi
  • 21
  • 1
  • 10
  • Firstly, do you have the datasource for the life quotes or are you asking us for the source? Secondly, if you want to display a life quote on a cardView separately, that sounds like a RecyclerView to me with a custom adapter. Can you please share you code as to what progress you've made so far. – Nero Dec 31 '18 at 09:20
  • I've added the code – Sufyan Hashmi Dec 31 '18 at 09:34
  • Rather than creating a new Activity for each and every quote type, you should use single activity and display quotes according to user choice. The choice can be sent to new activity by bundling with Intent. – okcomputer_kid Dec 31 '18 at 09:39
  • Inside new Activity, to display the list, you just have to use another list view with custom list item layout to have cardview for each list item – okcomputer_kid Dec 31 '18 at 09:40
  • okcomputer_kid can you provide any useful link how to implement please.. – Sufyan Hashmi Dec 31 '18 at 09:43
  • In the activities you are starting, you need to have the same thing actually. The activity will contain a list as well. However, the list items are `CardView` here. Check the tutorials online for showing a list of cards in an Android activity. That will help. – Reaz Murshed Dec 31 '18 at 10:00

1 Answers1

0

In my honest opinion, having too many if statements that are basically validating position I would consider replacing. You see, I will not say it's wrong to do this way simply because it works and that's the important part of programming as you need to ensure your primary requirements work. However, if you can shorten the lines of codes you've used for it to work, even better! This is exactly what differentiates attributes between different programmers; people who look at the problem from different perspective. The main reason I would recommend you to replace the functionality which something more scale-able and future proof is because if you want to change something among that code, you'll only have a few lines to modify instead of 50+ lines (trust me, it gets more difficult as your application gets complex).

That being said, make a new activity(name it anything), I'll name it userChoiceActivity in this case.

In you current activity:

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String itemValue = (String) listView.getItemAtPosition(position);
            Intent intent = new Intent(view.getContext(), userChoiceActivity.class);
            intent.putExtra("userChoice", itemValue);
            startActivity(intent);
}

Next in your userChoiceActivity class, you need to receive these values within the onCreate method.

String userChoice;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        userChoice = null;
    } else {
        userChoice = extras.getString("userChoice");
    }
} else {
    userChoice = (String) savedInstanceState.getSerializable("userChoice");
}

Now you should have what the user picked from the listView in your userChoice variable in the second activity (userChoiceActivity). From here, you can use multiple if statements to determine which arraylist you will use to populate your recyclerview. For the recyclerview, there are endless tutorials on the Internet but you can try using the following: -

Android RecyclerView and CardView Tutorial

Nero
  • 1,058
  • 1
  • 9
  • 25