0

This is my first question so I'll try to do it as best as possible.

The Goal is to have a list that will allow me to click on each item so that it would open a new activity where I'd be able to edit or remove this item.

At the moment I have a tabbed app where I have different fragments representing each tab, and inside these fragments I have a recycle view of all the items to be displayed. I also have a floating button that allows me to add new clients to the first tab (clients). It looks like this:

App default home page

I've tried several videos on YouTube and blogs to sort this out, but I can't get it to work. My wish would be if I could get that click to work and when it opens the UpdateClient activity it takes the necessary details from that item with it. See here the structure and UpdateClient activity:

App File Structure UpdateClient Activity

So far I've tried creating a button in the fragment itself, but this returns me a null object when trying to reference the id of the edit button (called "update"). I've also tried implementing the OnClickListener in the RecyclerViewAdapter, but no luck either, it just randomly allowed me to click sometimes and still didn't solve my need.

Here is the code so far:

MainActivity

public class MainActivity extends AppCompatActivity {

private AdminDB_Manager db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(sectionsPagerAdapter);
    TabLayout tabs = findViewById(R.id.tabs);
    tabs.setupWithViewPager(viewPager);
    FloatingActionButton fab = findViewById(R.id.fab);

    // Crear base de datos
    db = new AdminDB_Manager(this);
    db.open();

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Adding Client", Toast.LENGTH_LONG).show();
            addClient(view);
        }
    });
}

public void addClient(android.view.View v) {
    Intent intent = new Intent(this, AddClient.class);
    startActivity(intent);
}
}

ClientFragment

public class ClientsFragment extends Fragment {

// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ClientsFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ClientsFragment newInstance(int columnCount) {
    ClientsFragment fragment = new ClientsFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_client_list, container, false);
    Button update = view.findViewById(R.id.update);

    if(update != null){
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Adding Client", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
    }

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView.setAdapter(new MyClientsRecyclerViewAdapter(DummyContent.ITEMS));
    }
    return view;
}
}

MyClientsRecyclerViewAdapter

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

private final List<DummyItem> mValues;

public MyClientsRecyclerViewAdapter(List<DummyItem> items) {
    mValues = items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_clients, parent, false);


    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.mItem = mValues.get(position);
    holder.mIdView.setText(mValues.get(position).id);
    holder.mContentView.setText(mValues.get(position).content);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public final View mView;
    public final TextView mIdView;
    public final TextView mContentView;
    public DummyItem mItem;

    public ViewHolder(View view) {
        super(view);
        mView = view;
        mIdView = (TextView) view.findViewById(R.id.item_number);
        mContentView = (TextView) view.findViewById(R.id.content);
    }

    @Override
    public String toString() {
        return super.toString() + " '" + mContentView.getText() + "'";
    }
}
}

Here are the layouts:

fragment_clients.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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/item_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem" />

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem" />

    </LinearLayout>

fragment_clients_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView     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:id="@+id/list"
android:name="com.example.app.fragments.ClientsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ClientsFragment"
tools:listitem="@layout/fragment_clients">

</androidx.recyclerview.widget.RecyclerView>

1 Answers1

-1

I've fount the solution, with a small adaptation. Instead of having a button for each item, what happens now that each viewHolder has an onClickListener, so that when I click on the item (anywhere in its grid) it would open the activity. The changes made where:

ClientsFragment

public class ClientsFragment extends Fragment implements MyClientsRecyclerViewAdapter.OnClientListenser {

// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;

// Vars
private List<DummyContent.DummyItem> mValues = new ArrayList<>();

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ClientsFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ClientsFragment newInstance(int columnCount) {
    ClientsFragment fragment = new ClientsFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_client_list, container, false);

    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView.setAdapter(new MyClientsRecyclerViewAdapter(DummyContent.ITEMS, this));
    }
    return view;
}

@Override
public void onClientClick(int position) {
    Toast.makeText(getActivity(), "Editing Client", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getActivity(), UpdateClient.class);
    // intent.putExtra("Some info", "something else");
    startActivity(intent);
}
}

MyClientsRecyclerViewAdapter

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

private final List<DummyItem> mValues;
private OnClientListenser mOnClientListener;

public MyClientsRecyclerViewAdapter(List<DummyItem> items, OnClientListenser onClientListenser) {
    mValues = items;
    this.mOnClientListener = onClientListenser;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_clients, parent, false);


    return new ViewHolder(view, mOnClientListener);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.mItem = mValues.get(position);
    holder.mIdView.setText(mValues.get(position).id);
    holder.mContentView.setText(mValues.get(position).content);
}

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

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public final View mView;
    public final TextView mIdView;
    public final TextView mContentView;
    public DummyItem mItem;

    OnClientListenser onClientListener;

    public ViewHolder(View view, OnClientListenser onClientListener) {
        super(view);
        mView = view;
        mIdView = (TextView) view.findViewById(R.id.item_number);
        mContentView = (TextView) view.findViewById(R.id.content);

        this.onClientListener = onClientListener;
        itemView.setOnClickListener(this);
    }

    @Override
    public String toString() {
        return super.toString() + " '" + mContentView.getText() + "'";
    }

    // Implementation of interface
    @Override
    public void onClick(View v) {
        onClientListener.onClientClick(getAdapterPosition());
    }
}

public interface OnClientListenser{
    public void onClientClick(int position);
}
}

Thanks to everyone who helped on Facebook, I managed to find this video (https://www.youtube.com/watch?v=69C1ljfDvl0&feature=emb_logo) of CodingWithMitch who helped me with the progress!