单击按钮更改片段布局的数据

时间:2015-12-12 04:56:52

标签: android-fragments

picture of my problem

这是我一直在努力的小项目。 linegraph是从sqlite数据库中检索的ta数据中提取的。通过微调器,当我点击所需的年份时,应该更改线图。但这些变化无法看出。我无法刷新整个片段。

method). Try rendering this component inside of a new top-level component which will hold the ref.

}

这里,应根据firstDate的值更改linegraph。 firstDate的值已更改,但未更改布局。请帮忙

这是我的更新代码

public class CategoryChart extends android.support.v4.app.Fragment{
LineChart chart;
TransactionDatabaseAdapter transactionDatabaseAdapter;
String firstDate=null;
public static CategoryChart getInstance(Context context){
    CategoryChart categoryChart=new CategoryChart();
    Toast.makeText(context, "inside ", Toast.LENGTH_SHORT).show();
    return categoryChart;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_category_chart,container,false);
    spinner(view);
    drawChart(view);

    return view;
}





public void spinner(final View view){

    transactionDatabaseAdapter=new TransactionDatabaseAdapter(getActivity());
    final Spinner dropdown= (Spinner) view.findViewById(R.id.spinner);

    ArrayList items=new ArrayList();
    items.add(2015);
    items.add(2014);
    Integer temp= (Integer) items.get(0);
    firstDate = Integer.toString(temp);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_dropdown_item,items);
    dropdown.setAdapter(adapter);



    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View spinnerTextView, int position, long id) {
            TextView myText= (TextView) spinnerTextView;
            firstDate= (String) myText.getText();
             drawChart(getView());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}
public void drawChart(View view)
{

    transactionDatabaseAdapter=new TransactionDatabaseAdapter(getContext());

    chart= (LineChart) view.findViewById(R.id.chart);
    chart.setBackgroundColor(getResources().getColor(R.color.primary_dark));

    ArrayList<Entry> entries=new ArrayList<>();
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"01"), 0));

    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"02"), 1));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"03"), 2));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"04"), 3));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"05"), 4));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"06"), 5));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"07"), 6));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"08"), 7));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"09"), 8));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"10"), 9));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate,"11"), 10));
    entries.add(new Entry((float) transactionDatabaseAdapter.getChartData(firstDate, "12"), 11));

    LineDataSet dataset= new LineDataSet(entries,"# of calls");

    ArrayList<String> labels = new ArrayList<String>();
    labels.add("January");
    labels.add("February");
    labels.add("March");
    labels.add("April");
    labels.add("May");
    labels.add("June");
    labels.add("July");
    labels.add("August");
    labels.add("September");
    labels.add("October");
    labels.add("November");
    labels.add("December");

    LineData data = new LineData(labels,dataset);
    chart.setData(data);

    chart.setDescription("goo ");
    Toast.makeText(getContext(),firstDate,Toast.LENGTH_SHORT).show();
}

在活动课中,我有一个你建议的功能..

 Activity mActivity;

listener mCallback;
@Override
public void onAttach(Activity activity) {
    mActivity=activity;
    super.onAttach(activity);
    mCallback= (listener) mActivity;
}

LineChart chart;
TransactionDatabaseAdapter transactionDatabaseAdapter;
public String firstDate;
Spinner spinner;
Button selectBtn;
public static ArrayList<String> spinnerDate= new ArrayList<String>(){{
    add("2015");
    add("2014");
}};


public static CategoryChart getInstance(String spinnerDate){

    CategoryChart categoryChart=new CategoryChart();
    Bundle args=new Bundle();
    args.putString("dateValue", spinnerDate);
    categoryChart.setArguments(args);

    return categoryChart;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.activity_category_chart, container, false);
    Bundle bundle = getArguments();

    firstDate=bundle.getString("dateValue");
    spinner(view);
    drawChart(view);
    return view;
}



public void spinner(final View view){

    spinner= (Spinner) view.findViewById(R.id.spinner);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_dropdown_item,spinnerDate);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View spinnerTextView, int position, long id) {
            String pass= String.valueOf(spinner.getSelectedItem());
            mCallback.firstDateBack(pass);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

    });
}

1 个答案:

答案 0 :(得分:0)

您的活动需要添加/替换片段。用户从微调器中选择年份。片段通过接口将firstDate传回活动。然后,Activity需要替换为NEW片段并将firstDate传递给实现。

在你的片段中创建

(a)三个领域:

listener mCallback;
Activity mActivity;
public String firstDate=””;

(b)界面:

Public interface listener(){
    public void firstDateBack(String firstDate);
}

(c)方法

@Override
    public void onAttach(Activity activity) {
        mActivity=activity;
        super.onAttach(activity);
    mCallback = (listener) mActivity;
    }`

然后修改OnSelectedListener:

dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View spinnerTextView, int position, long id) {
        TextView myText= (TextView) spinnerTextView;
        firstDate= (String) myText.getText();
mCallback.firstDateBack(firstDate);
    }

在你的主要活动中:

(i)活动必须“实现CategoryChart.listener”

(ii)实施界面:

public void bringBackString(String stringBroughtBack){

IN HERE create  a NEW CategoryChart  fragment , with a NEW transaction and pass the fragment “stringBroughtBack” e.g.
CategoryChart cc=new CategoryChart();
cc.firstDate=stringBroughtBack; 
replace and commit as usual
}

我在这个例子中使用了android.app.Activity并确保它可行,你也可以使用它。尝试一下,让我知道你是如何进行的