-1

EDIT: In an effort to make this question not as unpleasant, I have continued working on this and have something to actually show for now. I completely removed all previous content and am completely rewriting my question.

My end goal is to have a MainActivity screen with multiple buttons that each perform different actions. Two example buttons are 1) to view device build information, and 2) to view installed applications. When a button is clicked, I want the Activity to switch to the RecyclerView and present that information. The user should be able to press the back button and click on another button.

I believe I have the structure ready but cannot figure out how to integrate the function to read packages. For that fact, where would I create the functions themselves and how can I pass the results to be viewed within RecyclerView?

Here is "MainActivity.java":

package com.emil.proofofconcepts.anothergo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    //Variables
    private TextView mTv;

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

        initRecyclerView();
    }

    private void initRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mTv);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

}

This is my "RecyclerViewAdapter.java":

package com.emil.proofofconcepts.anothergo;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

    private static final String TAG = "RecyclerViewAdapter";

    private TextView mTv;
    private Context mContext;

    public RecyclerViewAdapter(Context context, TextView mTv) {
        mTv = mTv;
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: called.");

        //holder.tv.setText("what");
        holder.tv.setText(readPackages());

    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        RelativeLayout parentLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_display);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }

    private String readPackages() {
        PackageManager pm = getPackageManager();
        String ret = "";
        for (ApplicationInfo app : pm.getInstalledApplications(0)){
            if(app.className != null && !app.className.equals("Null")){
                if(!ret.equals("")) ret = ret + "\n";
                ret = ret + app.className;
            }
        }
        return ret;
    }

}

Here is my "layout_list.xml":

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

    <TextView
        android:id="@+id/tv_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample Text"/>

</RelativeLayout>

And last but not least, this is my "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">

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

</RelativeLayout>
emilhozan
  • 1
  • 3
  • Okay, so I found this link: https://www.vogella.com/tutorials/AndroidRecyclerView/article.html#exercise-using-recyclerview-in-a-new-android-application but the sample code isn't working as is. – emilhozan Dec 10 '19 at 21:20
  • I solved my own question. There is no need to use RecyclerView. TextView can actually scroll - please disregard this post. https://stackoverflow.com/questions/1748977/making-textview-scrollable-on-android – emilhozan Dec 11 '19 at 01:05

1 Answers1

0

So it seems TextView can actually scroll. Tried Googling a bit more and found this: Making TextView scrollable on Android

I wasted quite some time here and lost some SO red :(

emilhozan
  • 1
  • 3