Activity类

时间:2018-03-20 21:19:48

标签: java android android-studio android-fragments android-activity

fragment reminder image - 当前问题

我正在使用的当前类称为Fragment Reminder,用于显示已输入的药物列表。然而,之前的这个类是片段,我决定将其更改为一个Activity,显示名为onCreateView的列表的方法现在尚未使用。当我点击该按钮运行此课程时,由于某种原因屏幕变暗。既然这个类是一个Activity,那么onCreateView就不能用了,因为那是一个Fragment。我该怎么改变,以便我可以创建列表。

FragmentReminder:

package com.example.junai.mrtest2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.GONE;


public class FragmentReminder extends Activity implements View.OnClickListener{

    private ArrayList al;
    private List list=new ArrayList();
    private ArrayAdapter<String> adapter;
    ListView lv;
    TextView tv;
    FloatingActionButton fab;

    public void receiveData(ArrayList al)
    {
        this.al=al;
        list.add(al.get(0));

    }


    //data for customlist
    private String desc[] = {};


   // @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.setTitle("Medicine Reminder");
        View v=inflater.inflate(R.layout.fragment_reminder, container, false);
        fab=(FloatingActionButton)v.findViewById(R.id.floatingActionButton);
        lv=(ListView)v.findViewById(R.id.rem_lv);
        tv=(TextView) v.findViewById(R.id.reminder_tv);

        fab.setOnClickListener(this);
        DatabaseHandler db=new DatabaseHandler(this);
        list=db.getAllReminders();

        if(list.size()==0)
        {
            lv.setVisibility(GONE);
            return v;
        }

        tv.setVisibility(GONE);

        adapter = new CustomList(this,list,desc);
        lv.setAdapter(adapter);

        //***Customised list view add***********************************************************************
       /* CustomList customList = new CustomList(getActivity(),list, desc);
        lv.setAdapter(customList);*/

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Toast.makeText(getActivity(),"You Clicked "+list.get(i),Toast.LENGTH_SHORT).show();
                //addReminderInCalendar();
                Intent in=new Intent(FragmentReminder.this,MedRemInfo.class);
                in.putExtra("id",list.get(i).toString());
                startActivity(in);
            }
        });
        //***************************************************************************

        return v;
    }


    @Override
    public void onClick(View v) {
        Intent in=new Intent(this,AddReminder.class);
        startActivity(in);
    }

}

fragment_reminder.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.junai.mrtest2.FragmentReminder">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:id="@+id/reminder_tv"
        android:textSize="20dp"
        android:text="No Reminders to show, Add a reminder.."
        android:gravity="center"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:src="@drawable/ic_add_black_24dp"
        android:tint="#ffffff"
        android:layout_gravity="bottom|center"
        android:id="@+id/floatingActionButton" />

    <ListView
        android:layout_width="match_parent"
        android:id="@+id/rem_lv"
        android:layout_height="match_parent"
        />

</LinearLayout>

MainActivity:

package com.example.junai.mrtest2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by junai on 20/03/2018.
 */

public class MainActivity extends Activity {

    private Button btn_addReminder, btn_reminderinfo;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_addReminder = (Button) findViewById(R.id.btn_addReminder);

        btn_addReminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(MainActivity.this, AddReminder.class));
            }
        });


        btn_reminderinfo = (Button) findViewById(R.id.btn_reminderinfo);

        btn_reminderinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(MainActivity.this, FragmentReminder.class));

            }
        });
    }
}

CustomList:

package com.example.junai.mrtest2;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Random;


public class CustomList extends ArrayAdapter<String> {
    private List names;
    private String[] desc;
    private Activity context;

    public CustomList(Activity context, List names, String[] desc) {
        super(context, R.layout.list_medinfo, names);
        this.context = context;
        this.names = names;
        this.desc = desc;

    }
    String color_hex[]={"#ff4000","#0000ff","#003EFF","#5C246E","#8B668B","#CD2990","#D41A1F","#FBDB0C","#FF6600"};
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //********************************************************************
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        // If holder not exist then locate all view from UI file.
        if (convertView == null) {
            // inflate UI from XML file
            convertView = inflater.inflate(R.layout.list_medinfo, parent, false);
            // get all UI view
            holder = new ViewHolder(convertView);
            // set tag for holder
            convertView.setTag(holder);
        } else {
            // if holder created, get tag from view
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(getItem(position));
        String firstLetter = null;
        //get first letter of each String item

            Log.d("String",getItem(position));
            String str=getItem(position);

            firstLetter=String.valueOf(str.charAt(0)).toUpperCase();
           // firstLetter.toUpperCase();
            ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
            // generate random color
            //int color = generator.getColor(getItem(position));

            int color = generator.getRandomColor();
            int pos= new Random().nextInt(color_hex.length);
            color = Color.parseColor(color_hex[pos]);
            Log.d("Color",""+pos);

            TextDrawable drawable = TextDrawable.builder()
                    .buildRound(firstLetter, color); // radius in px

            holder.imageView.setImageDrawable(drawable);

        return convertView;
        //***********************************************************************
       /* LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_medinfo, null, true);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.medname);
        TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.textViewDesc);
        ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);

        textViewName.setText(names.get(position).toString());
        textViewDesc.setText(desc[position]);
        image.setImageResource(imageid[position]);
        return  listViewItem;*/
    }

    private class ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ViewHolder(View v) {
            imageView = (ImageView) v.findViewById(R.id.medimage);
            textView = (TextView) v.findViewById(R.id.medname);
            Typeface typeface=Typeface.createFromAsset(context.getAssets(), "fonts/georgia.ttf");
            textView.setTypeface(typeface);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我重构了你的代码:

public class FragmentReminder extends Activity implements View.OnClickListener {

    private ArrayList al;
    private List list = new ArrayList();
    private ArrayAdapter<String> adapter;
    ListView lv;
    TextView tv;
    FloatingActionButton fab;

    public void receiveData(ArrayList al) {
        this.al = al;
        list.add(al.get(0));

    }


    //data for customlist
    private String desc[] = {};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_reminder);

        fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        lv = (ListView) findViewById(R.id.rem_lv);
        tv = (TextView) findViewById(R.id.reminder_tv);

        fab.setOnClickListener(this);
        DatabaseHandler db = new DatabaseHandler(this);
        list = db.getAllReminders();

        if (list.size() == 0) {
            lv.setVisibility(GONE);
        }

        tv.setVisibility(GONE);

        adapter = new CustomList(this, list, desc);
        lv.setAdapter(adapter);

        //***Customised list view add***********************************************************************
       /* CustomList customList = new CustomList(getActivity(),list, desc);
        lv.setAdapter(customList);*/

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Toast.makeText(getActivity(),"You Clicked "+list.get(i),Toast.LENGTH_SHORT).show();
                //addReminderInCalendar();
                Intent in = new Intent(FragmentReminder.this, MedRemInfo.class);
                in.putExtra("id", list.get(i).toString());
                startActivity(in);
            }
        });

    }


    @Override
    public void onClick(View v) {
        Intent in = new Intent(this, AddReminder.class);
        startActivity(in);
    }

}

您只需将代码移至onCreate()而不是onCreateView(),而且您无需访问最近夸大的视图V来执行findViewByID,所有您需要做的就是事先是setContentView()