Sathya Baman Sathya Baman - 4 months ago 19
Java Question

getActivity() inside viewholder

I have created a Recyclerview, with itemobjects, Adapter, viewholder and activity.

The Recyclerview works fine.

My problem is when an item is clicked from Recyclerview, i want to direct the user to TicketDetails activity.

i have the code to get the clicked item and its position in the view holder, but when i try to create a new intent to new activity like this inside view holder

startActivity(new Intent(getActivity(), TicketDetails.class));


I am unable to resolve getActivity()

ViewHolder

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;

import com.example.baman.zupportdesk.Login;
import com.example.baman.zupportdesk.R;
import com.example.baman.zupportdesk.TicketDetails;

import static android.support.v4.app.ActivityCompat.startActivity;

public class TicketsRecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

public ImageView priority;
public TextView sts_open;
public TextView sts_overdue;
public TextView tkt_from;
public TextView tkt_subject;
public TextView tkt_assignedto;
public TextView tkt_created_date;
public TextView txt_ticket_id;

private SparseBooleanArray selectedItems = new SparseBooleanArray();

public TicketsRecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);

priority = (ImageView) itemView.findViewById(R.id.priority_status_icon);
sts_open= (TextView) itemView.findViewById(R.id.tv_Tk_opn_status);
sts_overdue = (TextView) itemView.findViewById(R.id.tv_Tk_overdue);
tkt_from = (TextView) itemView.findViewById(R.id.tv_Tk_from);
tkt_subject = (TextView) itemView.findViewById(R.id.tv_Tk_subject);
tkt_assignedto = (TextView) itemView.findViewById(R.id.tv_Tk_Assignedto);
tkt_created_date = (TextView) itemView.findViewById(R.id.tv_Tk_Created_date);
txt_ticket_id = (TextView) itemView.findViewById(R.id.tv_Tk_TicketID);

}

@Override
public void onClick(View view) {
int position = getAdapterPosition();
String ticket_id = txt_ticket_id.getText().toString();
Log.d("ZD-clicked : ", "Position => "+String.valueOf(position)+", Ticket Id => "+ticket_id);

startActivity(new Intent(getActivity(), TicketDetails.class));

// if (selectedItems.get(getAdapterPosition(), false)) {
// selectedItems.delete(getAdapterPosition());
// view.setSelected(false);
// }
// else {
// selectedItems.put(getAdapterPosition(), true);
// view.setSelected(true);
// }
}
}


Adapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.example.baman.zupportdesk.R;
import java.util.List;

public class TicketsRecyclerViewAdapter extends RecyclerView.Adapter<TicketsRecyclerViewHolders>{

public List<TicketsItemObject> TicketsItemList;
private Context context;


public TicketsRecyclerViewAdapter(Context context, List<TicketsItemObject> itemList) {
this.TicketsItemList = itemList;
this.context = context;
}

@Override
public TicketsRecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_tickets_row, null);
TicketsRecyclerViewHolders rcv = new TicketsRecyclerViewHolders(layoutView);
return rcv;
}

@Override
public void onBindViewHolder(TicketsRecyclerViewHolders holder, int position) {

holder.priority.setImageResource(TicketsItemList.get(position).getStatus_priority());
holder.sts_open.setText(TicketsItemList.get(position).getStatus_open());
holder.sts_overdue.setText(TicketsItemList.get(position).getStatus_overdue());
holder.tkt_from.setText(TicketsItemList.get(position).getTicket_from());
holder.tkt_subject.setText(TicketsItemList.get(position).getTicket_subject());
holder.tkt_assignedto.setText(TicketsItemList.get(position).getTicket_assignedto());
holder.tkt_created_date.setText(TicketsItemList.get(position).getTicket_created_date());
holder.txt_ticket_id.setText(TicketsItemList.get(position).getTicket_id());
}

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


Item Object

public class TicketsItemObject {
private int status_priority;
private String status_open;
private String status_overdue;
private String ticket_from;
private String ticket_subject;
private String ticket_assignedto;
private String ticket_created_date;
private String ticket_id;

public TicketsItemObject(int status_priority, String status_open, String status_overdue, String ticket_from, String ticket_subject, String ticket_assignedto, String ticket_created_date, String ticket_id) {
this.status_priority = status_priority;
this.status_open = status_open;
this.status_overdue = status_overdue;
this.ticket_from = ticket_from;
this.ticket_subject = ticket_subject;
this.ticket_assignedto = ticket_assignedto;
this.ticket_created_date = ticket_created_date;
this.ticket_id =ticket_id;
}

public int getStatus_priority() {
return status_priority;
}

public String getStatus_open() {
return status_open;
}

public String getStatus_overdue() {
return status_overdue;
}

public String getTicket_from() {
return ticket_from;
}

public String getTicket_subject() {
return ticket_subject;
}

public String getTicket_assignedto() {
return ticket_assignedto;
}

public String getTicket_created_date(){return ticket_created_date;}

public String getTicket_id(){return ticket_id;}

}


Activity

public class Dashboard extends AppCompatActivity {
private Toolbar toolbar;
public DrawerLayout drawerLayout;
public ListView drawerList;
private navigationDrawerFragment drawerFragment;

private CompanyProfileAdapter mAdapter;
ApplicationEnvironmentURL applicationEnvironment;
ProgressDialog pDialog;
Context context;
String BASEURL;
String FilteredData;
String allAgents;
List<TicketsItemObject> items = new ArrayList<TicketsItemObject>();

private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private TicketsRecyclerViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Dashboard");
setContentView(R.layout.activity_dashboard);

applicationEnvironment = new ApplicationEnvironmentURL(this.context);
context = this.getApplicationContext();

toolbar = (Toolbar) findViewById(R.id.app_bar_dashboard);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

drawerFragment = (navigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setup(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);



SharedPreferences prefs = getSharedPreferences("zupportdesk", MODE_PRIVATE);
String islogged = prefs.getString("islogged", "Not defined");
String userid = prefs.getString("userid", "Not defined");
String profileToken = prefs.getString("profileToken", "Not defined");
String companyId = prefs.getString("companyId", "Not defined");
String companyName = prefs.getString("companyName", "Not defined");
String ProfileId = prefs.getString("ProfileId", "Not defined");

Log.d("islogged : ", islogged);
Log.d("userid : ", userid);
Log.d("profileToken : ", profileToken);
Log.d("companyId : ", companyId);
Log.d("companyName : ", companyName);
Log.d("ProfileId : ", ProfileId);

recyclerView = (RecyclerView)findViewById(R.id.recycler_view_tickets);
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
layoutManager = new LinearLayoutManager(Dashboard.this);
recyclerView.setLayoutManager(layoutManager);

getTickets(ProfileId, companyId, profileToken);

View newTicket = findViewById(R.id.newtic);
newTicket.setOnClickListener(onClickListener);




}

/* Multiple Button on click event handle */
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.newtic:
// Create a login URl, before starting anything

if(isNetworkAvailable()){
Intent intentTicket = new Intent(Dashboard.this, NewTicket.class);
startActivity(intentTicket);
} else {showErrorMessage("Please check your internet connection.", "No Connectivity!"); }
break;
}
}
};


private void getTickets(String profileId, String companyId, String profileToken){
if(isNetworkAvailable()) {
try {
setFilteredDataURL(companyId);
FilteredData = new getFilteredData().execute(profileToken).get();


Log.d("Full Filtered Data", FilteredData);
setTicketsURL(profileId, companyId);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
new getNewTickets().execute(profileToken);
}else{ showErrorMessage("Please check your internet connection.", "No Connectivity!"); }
}
................
.............
..More...........

Answer

getActivity() only works in fragments. A view will have activity Context. So get Context from the view.

Change

startActivity(new Intent(getActivity(), TicketDetails.class));

to

Context context = view.getContext();
context.startActivity(new Intent(context, TicketDetails.class));