gpt4 book ai didi

java - 取景器内的 getActivity()

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:44 28 4
gpt4 key购买 nike

我创建了一个 Recyclerview,其中包含项目对象、适配器、查看器和 Activity 。

Recyclerview 工作正常。

我的问题是当从 Recyclerview 单击一个项目时,我想将用户定向到 TicketDetails Activity 。

我有代码来获取点击的项目及其在 View 持有者中的位置,但是当我尝试为像这样的内部 View 持有者这样的新 Activity 创建新 Intent 时

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

我无法解析 getActivity()

ViewHolder

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);
// }
}
}

适配器

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();
}
}

项目对象

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

最佳答案

getActivity() 仅适用于 fragment 。 View 将具有 Activity 上下文。所以从 View 中获取上下文。

改变

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

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

关于java - 取景器内的 getActivity(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com