添加到您选择的微调器的图片

时间:2016-12-05 13:34:40

标签: android spinner android-arrayadapter

我有一个简单的待办事项。我想添加所选的优先级并显示ArrayList组件的图片。

像这样:

enter image description here

目前,该程序看起来像这样(现在所选择的是优先级,因为将显示纯文本。):

public class MainActivity extends AppCompatActivity {

    ListView todolista;
    ArrayAdapter<Feladat> adapter;
    ArrayList<Feladat> feladatok;
    EditText what;
    EditText when;
    Spinner spnrTodoPriority;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        todolista = (ListView) findViewById(R.id.listView);
        Calendar myCalendar = Calendar.getInstance();

        what = (EditText) findViewById(R.id.editText);
        when = (EditText) findViewById(R.id.editText2);
        feladatok = new ArrayList<Feladat>();

        adapter = new ArrayAdapter<Feladat>(this, android.R.layout.simple_list_item_1, feladatok);
        todolista.setAdapter(adapter);

        spnrTodoPriority = (Spinner) this.findViewById(R.id.spinner);
        String[] priorities = new String[3];
        priorities[0] = "Low";
        priorities[1] = "Medium";
        priorities[2] = "High";

        spnrTodoPriority.setAdapter(new
                ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                priorities));


        when.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int mYear,mMonth,mDay;
                //To show current date in the datepicker
                Calendar mcurrentDate = Calendar.getInstance();
                mYear = mcurrentDate.get(Calendar.YEAR);
                mMonth = mcurrentDate.get(Calendar.MONTH);
                mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker = new DatePickerDialog( MainActivity .
                        this, new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        // TODO Auto-generated method stub
                        when.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                    /*      Your code   to get date and time    */
                    }
                }, mYear, mMonth, mDay);
                mDatePicker.setTitle("Select date");
                mDatePicker.show();

            }
        });


        todolista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {



                Feladat torlendo = feladatok.get(position);

                ;

                feladatok.remove(position);
                adapter.notifyDataSetChanged();

                return true; 
            }
        });


    }
    public void buttonAddOnClick(View v) {

        String task = what.getText().toString();
        String time = when.getText().toString();
String spin = spnrTodoPriority.getSelectedItem().toString();


        Feladat ujfeladat= new Feladat(task,time,spin);


         feladatok.add(ujfeladat);

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPause() {
        super.onPause();

        File sdcard = Environment.getExternalStorageDirectory(); // megkeresi az SDkártyát
        File folder = new File(sdcard, "Tasks");        
        folder.mkdir();
        File file = new File(folder, "todo.csv");              

        try {

            PrintWriter writer = new PrintWriter(file);


            for (int i = 0; i < feladatok.size(); i++) {
                writer.println(feladatok.get(i).task + "," + feladatok.get(i).time + "," + feladatok.get(i).spin);
            }


            writer.close();

        } catch (IOException e) {

            Log.e("FELADATOK", "Valami baj van :("+e.getMessage());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        File sdcard = Environment.getExternalStorageDirectory();
       File folder = new File(sdcard, "Tasks");          
       folder.mkdir();
      File file = new File(folder, "todo.csv");              

        try {

            BufferedReader reader = new BufferedReader(new FileReader(file));


            while (reader.ready()) {
                String sor = reader.readLine();
                 String[] dolgok = sor.split(",");

                Feladat add= new Feladat(dolgok[0],dolgok[1],dolgok[2]);
                feladatok.add(add);

            }

            reader.close();
            adapter.notifyDataSetChanged();
        } catch (IOException e) {
            Log.e("FILE",e.getMessage());
        }

    }
}

提前感谢您的帮助!

0 个答案:

没有答案