-1

Hey folks!

Fair Warning: I am EXTREMELY new to programming in general, not to mention java and android, so to be brutally honest I probably don't even know how to ask the right questions in the right way. Therefore I appreciate any of you who are willing to take the time to look this over and help if you can! Also if you can find a way to dumb down any solutions you have and explain it like I am a three year old I'd also be very thankful. I've looked over on developer.android.com and all the technical terms are making it worse when it comes to my confusion.

So what I've done so far is;

I have the following codes below (both java and xml) for an app that once it opens it displays the three 'categories' of my app (that are activities.java files) on the activity_main.xml file. Since all three are the same for now I've posted the code for MidlandsActivity.java below.

OnClick it opens whichever activity category you clicked and displays an arraylist (as seen in the MidlandsActivity.java file) of information on the list_items.xml file.

So now... What I want to be able to do is:

I want you to be able to click on any item on the list (which you currently can but it does nothing at the moment) then display whichever list item you clicked in a new activity (I guess right? Well call that one ListDetails.java for the sake of converstation but which i havent made yet because I'm lost. Or should it be coded into the MidlandsActivity.java and each additional category activity for example?). Then have that activity display to the list_details.xml file for full viewing of the data in the selected category arraylist.

Currently I do have the xml file (list_details.xml) set up for this but I just dont know what i need to do next or where to put it.

Any help or suggestions?


Java Files


MidlandsActivity.java

package com.example.android.hauntedsc;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MidlandsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);

    //Create an array of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));
    words.add(new Word(
            "title",
            "address\n" +
            "citystatezip\n\n",
            "The Facts:\n\n" +
            "factstext\n\n" +
            "The Legend:\n\n" +
            "legendtext",
            R.drawable.haunted));


    // Create an {@link ArrayAdapter}, whose data source is a list of Strings. The
    // adapter knows how to create layouts for each item in the list, using the
    // simple_list_item_1.xml layout resource defined in the Android framework.
    // This list item layout contains a single {@link TextView}, which the adapter will set to
    // display a single word.
    WordAdapter adapter =
            new WordAdapter(this, words, R.color.category_midlands);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml file.
    ListView listView = (ListView) findViewById(R.id.list);

    // Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
    // {@link ListView} will display list items for each word in the list of words.
    // Do this by calling the setAdapter method on the {@link ListView} object and pass in
    // 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);
        }
    });
  }
}

MainActivity.java

package com.example.android.hauntedsc;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);

    //Find the View that shows the lowcountry catagory
    TextView lowcountry = (TextView)findViewById(R.id.lowcountry);
    //set a clicklistener on that View
    lowcountry.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers category is clicked on.
        @Override
        public void onClick(View view) {
            //Create a new intent to open the (@Link LowcountryActivity)
            Intent lowcountryIntent = new Intent(MainActivity.this,LowcountryActivity.class);
            //Start New Activity
            startActivity(lowcountryIntent);
        }
    });

    //Find the View that shows the upstate catagory
    TextView upstate = (TextView)findViewById(R.id.upstate);
    //set a clicklistener on that View
    upstate.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers category is clicked on.
        @Override
        public void onClick(View view) {
            //Create a new intent to open the (@Link UpstateActivity)
            Intent upstateIntent = new Intent(MainActivity.this,UpstateActivity.class);
            //Start New Activity
            startActivity(upstateIntent);
        }
    });

    //Find the View that shows the midlands catagory
    TextView midlands = (TextView)findViewById(R.id.midlands);
    //set a clicklistener on that View
    midlands.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers category is clicked on.
        @Override
        public void onClick(View view) {
            //Create a new intent to open the (@Link midlandsActivity)
            Intent midlandsIntent = new Intent(MainActivity.this,MidlandsActivity.class);
            //Start New Activity
            startActivity(midlandsIntent);
        }
    });

  }
}

Word.java

package com.example.android.hauntedsc;

public class Word {

//
private String mDescription;

//
private String mAddress;

//
private String mTitle;

/** Image resource ID for the word */
private int mImageResourceId = NO_IMAGE_PROVIDED;

/** Constant value that represents no image was provided for this word */
private static final int NO_IMAGE_PROVIDED = -1;

public Word(String description, String address, String title) {
    mDescription = description;
    mAddress = address;
    mTitle = title;
}

public Word(String description, String address, String title,  int imageResourceId) {
    mDescription = description;
    mAddress = address;
    mTitle = title;
    mImageResourceId = imageResourceId;
}

//Get the description of the list item.
public String getdescription() { return mDescription;}
//Get the address of the list item.
public String getaddress() { return mAddress; }
//Get the title of the list item.
public String gettitle() { return mTitle; }
//Return the image resource ID of the list item.
public int getImageResourceId() { return mImageResourceId; }
//Returns whether or not there is an image for this list item.
public boolean hasImage() { return mImageResourceId != NO_IMAGE_PROVIDED; }
}

WordAdapter.java

package com.example.android.hauntedsc;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class WordAdapter extends ArrayAdapter<Word> {

//Resource ID for background color of this list of words
private int mColorResourceId;

// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
    super(context, 0, words);
    mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    // Get the {@link AndroidFlavor} object located at this position in the list
    Word currentWord = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID version_name
    TextView titleTextView = (TextView) listItemView.findViewById(R.id.description_text_view);
    // Get the version name from the current AndroidFlavor object and
    // set this text on the name TextView
    titleTextView.setText(currentWord.gettitle());

    // Find the TextView in the list_item.xml layout with the ID version_number
    TextView addressTextView = (TextView) listItemView.findViewById(R.id.address_text_view);
    // Get the version number from the current AndroidFlavor object and
    // set this text on the number TextView
    addressTextView.setText(currentWord.getaddress());

    // Find the TextView in the list_item.xml layout with the ID version_number
    TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.title_text_view);
    // Get the version number from the current AndroidFlavor object and
    // set this text on the number TextView
    descriptionTextView.setText(currentWord.getdescription());

    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView imageView = (ImageView) listItemView.findViewById(R.id.image_view);
    // Check if an image is provided for this word or not
    if (currentWord.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
        imageView.setImageResource(currentWord.getImageResourceId());
        // Make sure the view is visible
        imageView.setVisibility(View.VISIBLE);
    } else {
        // Otherwise hide the ImageView (set visibility to GONE)
        imageView.setVisibility(View.GONE);
    }

    //Set the theme color for the list item
    View textContainer = listItemView.findViewById(R.id.text_container);
    //find the color that the resource ID maps too
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    //Set the background color of the text container View
    textContainer.setBackgroundColor(color);

    //Set the theme color for the list item
    View imageContainer = listItemView.findViewById(R.id.list_item_layout);
    //find the color that the resource ID maps too
    int icolor = ContextCompat.getColor(getContext(), mColorResourceId);
    //Set the background color of the text container View
    imageContainer.setBackgroundColor(icolor);


    // Return the whole list item layout (containing 2 TextViews and an ImageView)
    // so that it can be shown in the ListView
    return listItemView;
}
}

XML Files

activity_main.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"
android:background="@color/background"
android:orientation="vertical"
tools:context="com.example.android.hauntedsc.MainActivity">

<!-- Lowcountry category ((formerly numbers))-->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/category_lowcountry">
    <TextView
        android:id="@+id/lowcountry"
        style="@style/CategoryStyle"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/category_lowcountry" />
</FrameLayout>

<!-- Midlands category ((formerly family))-->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/category_midlands">
    <TextView
        android:id="@+id/midlands"
        style="@style/CategoryStyle"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/category_midlands" />
</FrameLayout>

<!-- Upstate category ((formerly colors))-->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/category_upstate">
    <TextView
        android:id="@+id/upstate"
        style="@style/CategoryStyle"
        android:background="?android:attr/selectableItemBackground"
        android:text="@string/category_upstate" />
</FrameLayout>
</LinearLayout>

list_items.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/list_item_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:background="@color/background"
android:orientation="horizontal">

<ImageView
    android:id="@+id/image_view"
    android:layout_width="@dimen/list_item_height"
    android:layout_height="@dimen/list_item_height"/>

<LinearLayout
    android:id="@+id/text_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/image_view"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"
        tools:text=""/>

    <TextView
        android:id="@+id/address_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center_vertical"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"
        tools:text="" />

    <TextView
        android:id="@+id/description_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0"
        android:gravity="top"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"
        tools:text="" />
</LinearLayout>
</RelativeLayout>

list_details.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <TextView
        android:id="@+id/detail_title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"/>

    <TextView
        android:id="@+id/detail_address_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"/>

    <TextView
        android:id="@+id/detail_description_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:paddingTop="4dp"
        android:paddingRight="8dp"
        android:paddingBottom="4dp"/>

    </LinearLayout>

</RelativeLayout>

</ScrollView>

3 Answers3

0

Create a new activity and name it ListDetails.java and then add the following to your existing Midlands.java

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

    // Get the {@link Word} object at the given position the user clicked on 

    //Word word = words.get(position);

Intent intent = new Intent(Midlands.this, ListDetails.class);
startActivity(intent);

 }
Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
0

So you just want your ListView items to take you to a new Activity?

In your MidlandsActivity where you set your ListView onClick do this:

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

            Word word = words.get(position);
            Intent intent = new Intent(MidlandsActivity.this, ListDetails.class);
            intent.putExtra("myWord", word);
            startActivity(intent);

        }
    });

What this does is start a new Activity and pass the value of the Word object. However, your Word class will have implement the Parcelable interface in order to be passed around through intents. You can find a great answer here.

Once you have that ready your object will be passed to your ListDetails activity where you can retrieve it in the onCreate(..) by using

Bundle data = getIntent().getExtras();
Word word = (Word) data.getParcelable("myWord");
Community
  • 1
  • 1
Pztar
  • 4,274
  • 6
  • 33
  • 39
0

In addition to what @Suhayl SH has written - You've said you want the item you clicked on to be visible in the activity that you start. In the given code this activity would be ListDetails. To do that use the code given by Suhayl and add the following:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

    // Get the {@link Word} object at the given position the user clicked on 

    //Word word = words.get(position);

   Intent intent = new Intent(Midlands.this, ListDetails.class);
   Word word = word.get(position);
   intent.putExtra("wordToBeDisplayed",word);
   startActivity(intent);

 }

Then in the ListDetails activity you can display the word by using:

Bundle b = getIntent().getExtras();
Word word = (Word) b.get("wordToBeDisplayed");
// The rest depends on your preferences about what to display
cru3lgenius
  • 106
  • 1
  • 7
  • Hey cru3lgenius, thanks for the info. So I added Suhayls code to my MidlandsActivity.java file. Then I dropped in your code snippet on the ListDetails.java file I created. It worked fine in the ListDetails java file however in the MidlandsActivity.java file I keep getting an error on the line that reads: intent.putExtra("wordToBeDisplayed",word). The error reads: Cannot resolve method putExtra(String,Word). Am I missing something? – Joshua Cox Jan 17 '17 at 22:22
  • Yeah, your POJO needs to implement the `Parcelable` interface. I noted that in my answer below. – Pztar Jan 18 '17 at 03:24