0

When I click on SOUTHPARK it opens KYLE and I want to make KYLE clickable and write something in KYLE how can I do that?

If it's related I can copy paste my manifest

That's pretty much it I have to write something here so I can post

public class MainActivity extends AppCompatActivity {

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

    String[] tvShows = {"southPark","naruto","simpsons","americandad"};

    String[] southPark = {"Kyle"};

    String[] naruto = {"naruto"};

    String[] simpsons = {"bart"};

    String[] americandad = {"Stan"};

    ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tvShows);

    final ListView theListView = (ListView) findViewById(R.id.theListView1);
    theListView.setAdapter(adapter);

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

            int itemPosition = position;
            String value = (String) theListView.getItemAtPosition(position);

            switch (value){
                case "southPark":
                    Intent southparkScreen = new Intent(MainActivity.this, SouthPark.class);
                    MainActivity.this.startActivity(southparkScreen);
                    break;
                case "naruto":
                    Intent narutoscreen = new Intent(MainActivity.this, Naruto.class);
                    MainActivity.this.startActivity(narutoscreen);
                    break;
                case "simpsons":
                    Intent simpsonsScreen = new Intent(MainActivity.this, Simpsons.class);
                    MainActivity.this.startActivity(simpsonsScreen);
                    break;

                case "americandad":
                    Intent americandadscreen = new Intent(MainActivity.this, AmericanDad.class);
                    MainActivity.this.startActivity(americandadscreen);
                    break;


            }

        }


    });

}

SouthPark.java

public class SouthPark extends AppCompatActivity {

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


    String[] southPark = {"Kyle"};


    ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, southPark);

    final ListView theListView = (ListView) findViewById(R.id.theListView2);
    theListView.setAdapter(adapter);
}

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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</RelativeLayout>

second_layout.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">

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



</LinearLayout>
Zain
  • 37,492
  • 7
  • 60
  • 84
fffff
  • 1

2 Answers2

0

I would suggest you, try the RecyclerView than ListView. Here's the explanation https://stackoverflow.com/a/28886129/11445765

If you find this answer a bit too complicated you can check tutorials here, here and here.

This answer might be the longest one so sit back and relax.

Here's my answer and I'll give it a try.

First, you need to add this recyclerView dependency implementation "androidx.recyclerview:recyclerview:1.1.0" you can get the updated one here https://developer.android.com/jetpack/androidx/releases/recyclerview

and in your activity_main.xml add this widget.

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Here's the code snippet below.

(I'll explain this furthermore down below).

public class MainActivity extends AppCompatActivity implements TvShowListener{

    RecyclerView recyclerView;
    List<String> tvShows;

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

        tvShows = new ArrayList<>();

        tvShows.add("southPark");
        tvShows.add("naruto");
        tvShows.add("simpsonPark");
        tvShows.add("americandad");

        TvShowsAdapter adapter = new TvShowsAdapter(tvShows, this);

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void itemListener(int position){
        Intent tvShowActivity = new Intent(this, TvShowActivity.class);
        tvShowActivity.putExtra("tvShowTitle", tvShows.get(position));
        startActivity(tvShowActivity);
    }
}

In your MainActivity instead of using an array of strings, use a List<String> and add those tvShowTitle to the List

tvShows.add("southPark");
tvShows.add("naruto");
tvShows.add("simpsonPark");
tvShows.add("americandad");

and pass the List to the custom adapter TvShowsAdapter constructor

TvShowsAdapter adapter = new TvShowsAdapter(tvShows, this);

together with the implemented custom listener interface.

interface TvShowListener{
    void itemListener(int position);
}

(which later on we pass this reference to our view holder class)

So that, whenever you click a text title or row item it'll trigger a callback containing a reference of item position to our list. We pass this position value using custom listener to the MainActivity who implements the interface and dereference the List index by item position to get the value and to pass this String value to TvShowActivity using Intent extras.

@Override
public void itemListener(int position){
    Intent tvShowActivity = new Intent(this, TvShowActivity.class);
    tvShowActivity.putExtra("tvShowTitle", tvShows.get(position));
    startActivity(tvShowActivity);
}


To initialize our RecyclerView Adapter an improvement version of ListView.

TvShowsAdapter.class
In our TvShowsAdapter, we extend RecyclerViewAdapter<TvShowsViewHolder> with a viewHolder helper class to hold each View of rows.

public class TvShowsAdapter extends RecyclerView.Adapter<TvShowsViewHolder>{
    private List<String> list;
    private TvShowListener listener;

    // We pass a reference of List<String> and interface listener from our main activity.
    TvShowsAdapter(List<String> tvShows, TvShowListener listener){
        this.list = tvShows;
        this.listener = listener;
    }

    // this initialized and inflates the item layout
    // we passed the referenced of a listener from our activity to the viewHolder.
    @NonNull
    @Override
    public TvShowsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.tvshows_items, parent, false);
        return new TvShowsViewHolder(view, listener);
    }

    // This replaces the content of views.
    @Override
    public void onBindViewHolder(@NonNull TvShowsViewHolder holder, int position){
        holder.showTitle.setText(list.get(position));
    }

    @Override
    public int getItemCount(){
        return list.size();
    }
}

And in the TvShowsViewHolder.class we extend RecyclerView.Holder as well as OnClickListener which later we will use the implemented method to pass the listener callback to our custom interface listener.

This helps our onBindViewHolder method to update the contents of the itemView to reflect the item at the given position.

// This provides a reference to the views for each data item
public class TvShowsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView showTitle;
    TvShowListener listener;

    /* setting onClick listener to each itemView and implement the interface
    listeners to the onClick method to getAdapterPositions of each views. */
    public TvShowsViewHolder(@NonNull View itemView, TvShowListener listener){
        super(itemView);
        this.itemView.setOnClickListener(this);
        this.listener = listener;
        showTitle = itemView.findViewById(R.id.text);
    }

    @Override
    public void onClick(View v){
        // this gets the position of list you clicked
        // and triggerrs item position callback to our MainActivity
        listener.itemListener(getAdapterPosition());
    }
}

And in our Main Activity, we have this block of code in which the activity implements the custom TvShowListener with a given int position parameter. This int position referenced the List<String> on which position we clicked and then it dereferences the List<String> value in our MainActivity to get and pass the string value using Intent extra, putting string key so we can get this string later on in our TvShowsActivity

@Override
public void itemListener(int position){
    Intent tvShowActivity = new Intent(this, TvShowActivity.class);
    tvShowActivity.putExtra("tvShowTitle", tvShows.get(position));
    startActivity(tvShowActivity);
}

And lastly, in our TvShowActivity.java this gets the string using Intent extras and display views.

public class TvShowActivity extends AppCompatActivity implements View.OnClickListener{

    Button button;
    EditText editText;
    TextView showTitle;
    TextView getEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tv_show);
        button = findViewById(R.id.button);
        editText = findViewById(R.id.edit_Text);
        showTitle = findViewById(R.id.tvShow);
        getEditText = findViewById(R.id.getText);

        Intent intent = getIntent();
        String title = intent.getStringExtra("tvShowTitle");

        showTitle.setText(title);

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        getEditText.setText(editText.getText());
    }
}

ListView/RecyclerView output of MainActivity: Output of MainActivity
Second Activity you want to click and write text: Output of 2nd Activity

I've attached the GIF output check here: https://i.stack.imgur.com/6UYnI.jpg

I've also uploaded this to my GitHub repo. Github link

0

you could set an OnItemClickListeneron the listview:

listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object o = prestListView.getItemAtPosition(position);
    //...
}

});

julianpjp
  • 112
  • 1
  • 8