0
이 안녕,

오류리스트 뷰에서 버튼을

여기

내가 이미지와 button.I로 목록보기를 만든 다른 fragment.For에 점프하여 해당 버튼을 처리하는

클릭 할 때 단추의 onclick listner에서이 코드를 사용했지만 잘못되었습니다.

"getFragmentManager() 메서드를 해결할 수 없습니다"라는 메시지가 표시됩니다.. 버튼의 onclicklistner 코드에서 수정해야 할 부분 및 수정점은 무엇입니까? 도와주세요.

목록보기 용 어댑터 파일은 다음과 같습니다.

Cart_ContactAdapter.java : 당신이 활동 안에 있지 않는

public class Cart_ContactAdapter extends ArrayAdapter{ 

    List list = new ArrayList(); 
    public Cart_ContactAdapter(@NonNull Context context, @LayoutRes int resource) { 
     super(context, resource); 
    } 

    public void add(Contacts object) { 
     super.add(object); 
     list.add(object); 
    } 

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

    @Nullable 
    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

     View row; 
     ContactHolder contactHolder; 
     row = convertView; 
     if(row==null){ 
      LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.cart_list_pattern,parent,false); 
      contactHolder=new ContactHolder(); 
      contactHolder.t_product_name= (TextView) row.findViewById(R.id.productname); 
      contactHolder.t_product_cn= (TextView) row.findViewById(R.id.productcn); 
      contactHolder.t_quantity= (TextView) row.findViewById(R.id.quantity); 
      contactHolder.t_price= (TextView) row.findViewById(R.id.price); 
      contactHolder.buynow= (Button) row.findViewById(R.id.buynow); 

      contactHolder.buynow.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        TextView product_name= (TextView) view.findViewById(R.id.productname); 
        TextView product_cn= (TextView) view.findViewById(R.id.productcn); 

        PurchaseFragment p = new PurchaseFragment(); 
        Bundle args = new Bundle(); 
        args.putString("product_name",product_name.getText().toString()); 
        args.putString("product_cn", product_cn.getText().toString()); 
        p.setArguments(args); 
        /*it shows error on this line*/getFragmentManager().beginTransaction().add(R.id.content_frame, p).addToBackStack("organic acid").commit(); 

       } 
      }); 
      row.setTag(contactHolder); 

     } 
     else 
     { 
      contactHolder= (ContactHolder) row.getTag(); 
     } 
     Contacts contacts = (Contacts) this.getItem(position); 
     contactHolder.t_product_name.setText(contacts.getProduct_name()); 
     contactHolder.t_product_cn.setText(contacts.getProduct_cn()); 
     contactHolder.t_quantity.setText(contacts.getQuantity()); 
     contactHolder.t_price.setText(contacts.getPrice()); 
     return row; 
    } 
    static class ContactHolder{ 
     TextView t_product_name,t_product_cn,t_quantity,t_price; 
     Button buynow; 
    } 
+0

질문을 편집하고 당신이 얻을 오류 넣어주세요. –

+0

은 "getFragmentManager() 메서드를 해결할 수 없습니다"오류를 표시합니다. – harsh

답변

0

당신은 getFragmentManager()를 사용할 수 없습니다.

따라서 생성자에서 전달한 컨텍스트가 액티비티라는 것을 확신한다면이 컨텍스트를 전역으로 만들 수 있으며이를 사용하여 getFragmentManager()을 호출 할 수 있습니다. 이처럼

:

public class Cart_ContactAdapter extends ArrayAdapter{ 
//Declare a global context 
private Context context; 
List list = new ArrayList(); 
public Cart_ContactAdapter(@NonNull Context context, @LayoutRes int resource) { 
    super(context, resource); 
    //Make context global 
    this.context=context; 

} 

public void add(Contacts object) { 
    super.add(object); 
    list.add(object); 
} 

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

@Nullable 
@Override 
public Object getItem(int position) { 
    return list.get(position); 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    View row; 
    ContactHolder contactHolder; 
    row = convertView; 
    if(row==null){ 
     LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = layoutInflater.inflate(R.layout.cart_list_pattern,parent,false); 
     contactHolder=new ContactHolder(); 
     contactHolder.t_product_name= (TextView) row.findViewById(R.id.productname); 
     contactHolder.t_product_cn= (TextView) row.findViewById(R.id.productcn); 
     contactHolder.t_quantity= (TextView) row.findViewById(R.id.quantity); 
     contactHolder.t_price= (TextView) row.findViewById(R.id.price); 
     contactHolder.buynow= (Button) row.findViewById(R.id.buynow); 

     contactHolder.buynow.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       TextView product_name= (TextView) view.findViewById(R.id.productname); 
       TextView product_cn= (TextView) view.findViewById(R.id.productcn); 

       PurchaseFragment p = new PurchaseFragment(); 
       Bundle args = new Bundle(); 
       args.putString("product_name",product_name.getText().toString()); 
       args.putString("product_cn", product_cn.getText().toString()); 
       p.setArguments(args); 
       //Cast the context to an activity but be sure that it's an Activity or AppCompatActivity,Then use it to call getFragmentManager(); 
       ((Activity)context).getFragmentManager().beginTransaction().add(R.id.content_frame, p).addToBackStack("organic acid").commit(); 

      } 
     }); 
     row.setTag(contactHolder); 

    } 
    else 
    { 
     contactHolder= (ContactHolder) row.getTag(); 
    } 
    Contacts contacts = (Contacts) this.getItem(position); 
    contactHolder.t_product_name.setText(contacts.getProduct_name()); 
    contactHolder.t_product_cn.setText(contacts.getProduct_cn()); 
    contactHolder.t_quantity.setText(contacts.getQuantity()); 
    contactHolder.t_price.setText(contacts.getPrice()); 
    return row; 
} 
static class ContactHolder{ 
    TextView t_product_name,t_product_cn,t_quantity,t_price; 
    Button buynow; 
} 
+0

"기호 변수 컨텍스트를 찾을 수 없습니다"오류가 표시됩니다. – harsh

+0

활동을 가져 왔습니까? 이'import android.app.Activity; ' –

+0

을 사용하고 Context를 인스턴스 변수'private Context context;'로 선언했는지 확인하십시오. –

관련 문제