将一个片段转换成一个活动界面。

huangapple go评论77阅读模式
英文:

facing converting a fragment into an activity

问题

public class TestFragment extends Fragment  {
    // ... (Your imports and member variables)

    public TestFragment() {
        // Required empty public constructor
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        // ... (Initialize your views and set click listeners)

        return view;
    }

    private void makeRecyclerView() {
        // ... (Your recycler view setup)
    }

    private void setDisplay(int item) {
        // ... (Set display content based on the item)
    }

    private void getFData() {
        // ... (Fetching data from Firebase)
    }

    private void setFlagValue() {
        // ... (Setting flag value)
    }

    private void setFlagColor() {
        // ... (Setting flag color)
    }

    private void setButtonColor() {
        // ... (Setting button colors)
    }

    private void displayNextQues() {
        // ... (Display next question)
    }

    private void makeAllDisplay() {
        // ... (Make all display)
    }

    private void displayPrevQues() {
        // ... (Display previous question)
    }
}

Please note that the code translation provided above is a direct translation of your provided Java code. If you have any specific questions about the code or if you encounter any issues while using it, feel free to ask for further assistance.

英文:

when i try to converting an activity than i get following two errors.
1. getActivity () 2. getContext() .please convert my code into activity.
my code is bellow.

public class TestFragment extends Fragment  {
private Button btnA, btnB, btnC, btnD, btnSubmit, btnNext, btnPrev, btnFlag;
private TextView tvQuestion, tvLayout;
private int totalItem;
private int listItem = 0;
private ArrayList<mcqQuestion> quesList = new ArrayList<>();
private int score = 0;
private boolean isLoading = true;
private RelativeLayout rlMcqProgress;
private FirebaseAuth mAuth;
private boolean allAnswered = false;
private RecyclerView rvQuesLayout;
private RecyclerView.LayoutManager layoutManager;
private QuestionLayoutRecyclerAdapter adapter;
private LinearLayout llQuestionAll;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference("tax").child("Questions");
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);
btnA = view.findViewById(R.id.btn_mcq_A);
btnB = view.findViewById(R.id.btn_mcq_B);
btnC = view.findViewById(R.id.btn_mcq_C);
btnD = view.findViewById(R.id.btn_mcq_D);
btnSubmit = view.findViewById(R.id.btn_mcq_submit);
btnNext = view.findViewById(R.id.btn_mcq_next);
btnPrev = view.findViewById(R.id.btn_mcq_prev);
btnFlag = view.findViewById(R.id.btn_mcq_flag);
tvQuestion = view.findViewById(R.id.tv_mcq_question);
tvLayout = view.findViewById(R.id.tv_mcq_question_layout);
rvQuesLayout = view.findViewById(R.id.rv_layout_question_test);
rlMcqProgress = view.findViewById(R.id.rl_mcq_progress);
rlMcqProgress.setVisibility(View.VISIBLE);
llQuestionAll=view.findViewById(R.id.ll_test_all);
mAuth = FirebaseAuth.getInstance();
getFData();
btnA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer("optA");
setButtonColor();
btnA.setBackgroundResource(R.color.sky_blue);
}
});
btnB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer("optB");
setButtonColor();
btnB.setBackgroundResource(R.color.sky_blue);
}
});
btnC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer("optC");
setButtonColor();
btnC.setBackgroundResource(R.color.sky_blue);
}
});
btnD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer("optD");
setButtonColor();
btnD.setBackgroundResource(R.color.sky_blue);
}
});
btnFlag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFlagValue();
setFlagColor();
}
});
tvLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.notifyDataSetChanged();
if (rvQuesLayout.getVisibility() == View.GONE || rvQuesLayout.getVisibility() == View.INVISIBLE) {
rvQuesLayout.setVisibility(View.VISIBLE);
llQuestionAll.setVisibility(View.GONE);
} else {
rvQuesLayout.setVisibility(View.GONE);
llQuestionAll.setVisibility(View.VISIBLE);
}
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayNextQues();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayPrevQues();
}
});
btnSubmit.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
for (int i = 0; i < totalItem; i++) {
if (quesList.get(i).getSelAnswer().equals("none")) {
allAnswered = false;
score++;
break;
} else {
if (quesList.get(i).getSelAnswer().equals(quesList.get(i).getAnswer())) {
score++;
}
allAnswered = true;
}
}
if (allAnswered) {
Intent intent = new Intent(getActivity(),finalresult.class);
intent.putExtra("score" , score);
intent.putExtra("questions" , totalItem);
startActivity(intent);
} else {
Toast.makeText(getActivity(), "Answer all the questions", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
private void makeRecyclerView() {
layoutManager = new LinearLayoutManager(getContext());
//rvQuesLayout.setHasFixedSize(true);
rvQuesLayout.setLayoutManager(layoutManager);
adapter = new QuestionLayoutRecyclerAdapter(quesList);
rvQuesLayout.setAdapter(adapter);
adapter.setOnItemClickListner(new QuestionLayoutRecyclerAdapter.onItemClickListner() {
@Override
public void onClick(int pos) {
listItem=pos;
if(listItem>=0 && listItem<totalItem){
makeAllDisplay();
rvQuesLayout.setVisibility(View.GONE);
llQuestionAll.setVisibility(View.VISIBLE);
}
}
});
}
private void setDisplay(int item) {
btnA.setText(quesList.get(item).getOptA());
btnB.setText(quesList.get(item).getOptB());
btnC.setText(quesList.get(item).getOptC());
btnD.setText(quesList.get(item).getOptD());
tvQuestion.setText(quesList.get(item).getQuestion());
}
private void getFData() {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String qtext = (String) messageSnapshot.child("qText").getValue();
String optA = (String) messageSnapshot.child("optA").getValue();
String optB = (String) messageSnapshot.child("optB").getValue();
String optC = (String) messageSnapshot.child("optC").getValue();
String optD = (String) messageSnapshot.child("optD").getValue();
String answer = (String) messageSnapshot.child("answer").getValue();
quesList.add(new mcqQuestion(qtext, optA, optB, optC, optD, answer, "none", false));
Log.d("TOTAL ITEM", String.valueOf(quesList.size()));
}
totalItem = quesList.size();
isLoading = false;
if (!isLoading) {
setDisplay(listItem);
rlMcqProgress.setVisibility(View.GONE);
tvLayout.setVisibility(View.VISIBLE);
}
makeRecyclerView();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void setFlagValue(){
if (quesList.get(listItem).isFlag()) {
quesList.get(listItem).setFlag(false);
} else {
quesList.get(listItem).setFlag(true);
}
}
private void setFlagColor(){
if (quesList.get(listItem).isFlag()) {
btnFlag.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.black));
btnFlag.setTextColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.grey));
} else {
btnFlag.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.grey_light));
btnFlag.setTextColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.grey));
}
}
private void setButtonColor() {
btnA.setBackgroundResource(R.color.grey_light);
btnB.setBackgroundResource(R.color.grey_light);
btnC.setBackgroundResource(R.color.grey_light);
btnD.setBackgroundResource(R.color.grey_light);
}
private void displayNextQues() {
if (listItem < totalItem - 1) {
listItem++;
makeAllDisplay();
} else {
btnNext.setVisibility(View.GONE);
btnSubmit.setVisibility(View.VISIBLE);
}
}
private void makeAllDisplay(){
setDisplay(listItem);
setButtonColor();
setFlagColor();
switch (quesList.get(listItem).getSelAnswer()) {
case "optA":
btnA.setBackgroundResource(R.color.sky_blue);
break;
case "optB":
btnB.setBackgroundResource(R.color.sky_blue);
break;
case "optC":
btnC.setBackgroundResource(R.color.sky_blue);
break;
case "optD":
btnD.setBackgroundResource(R.color.sky_blue);
break;
default:
setButtonColor();
break;
}
}
private void displayPrevQues() {
btnNext.setVisibility(View.VISIBLE);
if (listItem > 0 && listItem < totalItem) {
listItem--;
makeAllDisplay();
} else {
Toast.makeText(getActivity(), "Cannot GO back!!", Toast.LENGTH_SHORT).show();
}
}
}

答案1

得分: 1

public class TestActivity extends Activity {
    // ... (other variable declarations)

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_test);
        // ... (other view initialization)

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                for (int i = 0; i < totalItem; i++) {
                    if (quesList.get(i).getSelAnswer().equals("none")) {
                        allAnswered = false;
                        score++;
                        break;
                    } else {
                        if (quesList.get(i).getSelAnswer().equals(quesList.get(i).getAnswer())) {
                            score++;
                        }
                        allAnswered = true;
                    }
                }
                if (allAnswered) {
                    Intent intent = new Intent(TestActivity.this, finalresult.class);
                    intent.putExtra("score", score);
                    intent.putExtra("questions", totalItem);
                    startActivity(intent);
                } else {
                    Toast.makeText(TestActivity.this, "Answer all the questions", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // ... (other methods)

    private void getFData() {
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
                    String qtext = (String) messageSnapshot.child("qText").getValue();
                    String optA = (String) messageSnapshot.child("optA").getValue();
                    String optB = (String) messageSnapshot.child("optB").getValue();
                    String optC = (String) messageSnapshot.child("optC").getValue();
                    String optD = (String) messageSnapshot.child("optD").getValue();
                    String answer = (String) messageSnapshot.child("answer").getValue();
                    quesList.add(new mcqQuestion(qtext, optA, optB, optC, optD, answer, "none", false));
                    Log.d("TOTAL ITEM", String.valueOf(quesList.size()));
                }
                totalItem = quesList.size();
                isLoading = false;
                if (!isLoading) {
                    setDisplay(listItem);
                    rlMcqProgress.setVisibility(View.GONE);
                    tvLayout.setVisibility(View.VISIBLE);
                }
                makeRecyclerView();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    // ... (other methods)
}

(Note: I've made the necessary corrections to the code you provided, including changing the Intent target activity from this to TestActivity.this and fixing a missing variable declaration. Make sure to also update your Manifest to include the new finalresult Activity.)

英文:

> when i try to converting an activity than i get following two errors. 1. getActivity () 2. getContext()

When you change Fragment to Activity, all method need getActivity() and getContext() can be change to this. Example new Intent(getActivity(),finalresult.class); to new Intent(this,finalresult.class);

public class TestActivity extends Activity {
private Button btnA, btnB, btnC, btnD, btnSubmit, btnNext, btnPrev, btnFlag;
private TextView tvQuestion, tvLayout;
private int totalItem;
private int listItem = 0;
private ArrayList&lt;mcqQuestion&gt; quesList = new ArrayList&lt;&gt;();
private int score = 0;
private boolean isLoading = true;
private RelativeLayout rlMcqProgress;
private FirebaseAuth mAuth;
private boolean allAnswered = false;
private RecyclerView rvQuesLayout;
private RecyclerView.LayoutManager layoutManager;
private QuestionLayoutRecyclerAdapter adapter;
private LinearLayout llQuestionAll;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference(&quot;tax&quot;).child(&quot;Questions&quot;);
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_test);
btnA = findViewById(R.id.btn_mcq_A);
btnB = findViewById(R.id.btn_mcq_B);
btnC = findViewById(R.id.btn_mcq_C);
btnD = findViewById(R.id.btn_mcq_D);
btnSubmit = findViewById(R.id.btn_mcq_submit);
btnNext = findViewById(R.id.btn_mcq_next);
btnPrev = findViewById(R.id.btn_mcq_prev);
btnFlag = findViewById(R.id.btn_mcq_flag);
tvQuestion = findViewById(R.id.tv_mcq_question);
tvLayout = findViewById(R.id.tv_mcq_question_layout);
rvQuesLayout = findViewById(R.id.rv_layout_question_test);
rlMcqProgress = findViewById(R.id.rl_mcq_progress);
rlMcqProgress.setVisibility(View.VISIBLE);
llQuestionAll = findViewById(R.id.ll_test_all);
mAuth = FirebaseAuth.getInstance();
getFData();
btnA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer(&quot;optA&quot;);
setButtonColor();
btnA.setBackgroundResource(R.color.sky_blue);
}
});
btnB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer(&quot;optB&quot;);
setButtonColor();
btnB.setBackgroundResource(R.color.sky_blue);
}
});
btnC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer(&quot;optC&quot;);
setButtonColor();
btnC.setBackgroundResource(R.color.sky_blue);
}
});
btnD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quesList.get(listItem).setSelAnswer(&quot;optD&quot;);
setButtonColor();
btnD.setBackgroundResource(R.color.sky_blue);
}
});
btnFlag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFlagValue();
setFlagColor();
}
});
tvLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.notifyDataSetChanged();
if (rvQuesLayout.getVisibility() == View.GONE || rvQuesLayout.getVisibility() == View.INVISIBLE) {
rvQuesLayout.setVisibility(View.VISIBLE);
llQuestionAll.setVisibility(View.GONE);
} else {
rvQuesLayout.setVisibility(View.GONE);
llQuestionAll.setVisibility(View.VISIBLE);
}
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayNextQues();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayPrevQues();
}
});
btnSubmit.setOnClickListener(new View.OnClickListener() {
@SuppressLint(&quot;SetTextI18n&quot;)
@Override
public void onClick(View v) {
for (int i = 0; i &lt; totalItem; i++) {
if (quesList.get(i).getSelAnswer().equals(&quot;none&quot;)) {
allAnswered = false;
score++;
break;
} else {
if (quesList.get(i).getSelAnswer().equals(quesList.get(i).getAnswer())) {
score++;
}
allAnswered = true;
}
}
if (allAnswered) {
Intent intent = new Intent(this, finalresult.class);
intent.putExtra(&quot;score&quot;, score);
intent.putExtra(&quot;questions&quot;, totalItem);
startActivity(intent);
} else {
Toast.makeText(TestActivity.this, &quot;Answer all the questions&quot;, Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
private void makeRecyclerView() {
layoutManager = new LinearLayoutManager(this);
//rvQuesLayout.setHasFixedSize(true);
rvQuesLayout.setLayoutManager(layoutManager);
adapter = new QuestionLayoutRecyclerAdapter(quesList);
rvQuesLayout.setAdapter(adapter);
adapter.setOnItemClickListner(new QuestionLayoutRecyclerAdapter.onItemClickListner() {
@Override
public void onClick(int pos) {
listItem = pos;
if (listItem &gt;= 0 &amp;&amp; listItem &lt; totalItem) {
makeAllDisplay();
rvQuesLayout.setVisibility(View.GONE);
llQuestionAll.setVisibility(View.VISIBLE);
}
}
});
}
private void setDisplay(int item) {
btnA.setText(quesList.get(item).getOptA());
btnB.setText(quesList.get(item).getOptB());
btnC.setText(quesList.get(item).getOptC());
btnD.setText(quesList.get(item).getOptD());
tvQuestion.setText(quesList.get(item).getQuestion());
}
private void getFData() {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
String qtext = (String) messageSnapshot.child(&quot;qText&quot;).getValue();
String optA = (String) messageSnapshot.child(&quot;optA&quot;).getValue();
String optB = (String) messageSnapshot.child(&quot;optB&quot;).getValue();
String optC = (String) messageSnapshot.child(&quot;optC&quot;).getValue();
String optD = (String) messageSnapshot.child(&quot;optD&quot;).getValue();
String answer = (String) messageSnapshot.child(&quot;answer&quot;).getValue();
quesList.add(new mcqQuestion(qtext, optA, optB, optC, optD, answer, &quot;none&quot;, false));
Log.d(&quot;TOTAL ITEM&quot;, String.valueOf(quesList.size()));
}
totalItem = quesList.size();
isLoading = false;
if (!isLoading) {
setDisplay(listItem);
rlMcqProgress.setVisibility(View.GONE);
tvLayout.setVisibility(View.VISIBLE);
}
makeRecyclerView();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void setFlagValue() {
if (quesList.get(listItem).isFlag()) {
quesList.get(listItem).setFlag(false);
} else {
quesList.get(listItem).setFlag(true);
}
}
private void setFlagColor() {
if (quesList.get(listItem).isFlag()) {
btnFlag.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(this), R.color.black));
btnFlag.setTextColor(ContextCompat.getColor(Objects.requireNonNull(this), R.color.grey));
} else {
btnFlag.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(this), R.color.grey_light));
btnFlag.setTextColor(ContextCompat.getColor(Objects.requireNonNull(this), R.color.grey));
}
}
private void setButtonColor() {
btnA.setBackgroundResource(R.color.grey_light);
btnB.setBackgroundResource(R.color.grey_light);
btnC.setBackgroundResource(R.color.grey_light);
btnD.setBackgroundResource(R.color.grey_light);
}
private void displayNextQues() {
if (listItem &lt; totalItem - 1) {
listItem++;
makeAllDisplay();
} else {
btnNext.setVisibility(View.GONE);
btnSubmit.setVisibility(View.VISIBLE);
}
}
private void makeAllDisplay() {
setDisplay(listItem);
setButtonColor();
setFlagColor();
switch (quesList.get(listItem).getSelAnswer()) {
case &quot;optA&quot;:
btnA.setBackgroundResource(R.color.sky_blue);
break;
case &quot;optB&quot;:
btnB.setBackgroundResource(R.color.sky_blue);
break;
case &quot;optC&quot;:
btnC.setBackgroundResource(R.color.sky_blue);
break;
case &quot;optD&quot;:
btnD.setBackgroundResource(R.color.sky_blue);
break;
default:
setButtonColor();
break;
}
}
private void displayPrevQues() {
btnNext.setVisibility(View.VISIBLE);
if (listItem &gt; 0 &amp;&amp; listItem &lt; totalItem) {
listItem--;
makeAllDisplay();
} else {
Toast.makeText(this, &quot;Cannot GO back!!&quot;, Toast.LENGTH_SHORT).show();
}
}
}

Don't forget add new Activity to Manifest

huangapple
  • 本文由 发表于 2020年5月4日 11:01:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61584508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定