我正在尝试将片段与活动结合起来。但我最终得到了一个错误。
碎片代码:
package com.blipclap.engineering_solution;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class QFragment extends Fragment {@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
return inflater.inflate(R.layout.fragment_q, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("QFragment");
}
}
一个被用作活动,另一个被用作片段。我是碎片概念的新手。
主要活动代码:
package com.bar.example.androidspinnerexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");
list.add("Sales");
list.add("Others");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");
list.add("Breakfast");
list.add("Purchase");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
那么解决这个问题的方法是什么?
答案 0 :(得分:1)
您将把您的实现放在片段中,并使片段事务管理器从活动和此处的代码示例中调用它。 `
package com.bar.example.androidspinnerexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
QFragment fragment = new QFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//container id as first param for replace function
fragmentTransaction.replace(R.id.activity_spinner_ex4, fragment, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
和片段
package com.blipclap.engineering_solution;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class QFragment extends Fragment implements
OnItemSelectedListener{@Nullable
Spinner s1,s2;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View view = inflater.inflate(R.layout.fragment_q, container, false);
s1 = (Spinner)view.findViewById(R.id.spinner1);
s2 = (Spinner)view.findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("QFragment");
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");
list.add("Sales");
list.add("Others");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");
list.add("Breakfast");
list.add("Purchase");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 1 :(得分:1)
对于您提到的3个错误,请将y
替换为您的班级名称
对于这三条线都是this
。
SpinnerEx4Activity.this