【问题标题】:OnCreateView method in Activity classActivity 类中的 OnCreateView 方法
【发布时间】:2018-08-29 20:41:22
【问题描述】:

fragment reminder image - 当前问题

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

片段提醒:

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>

主活动:

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));

            }
        });
    }
}

自定义列表:

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);
        }
    }
}

【问题讨论】:

  • Activity使用onCreate而不是onCreateView方法
  • 需要注意两件事: 1. 确保您没有在主线程中调用数据库操作。因此,将数据库加载调用移动到 AsyncTask 或其他一些后台操作,一旦获得结果,就在主线程中设置适配器。希望这将消除“黑屏”问题。 2.如果你使用的是Activity,使用oncreate()方法。
  • 是的,已替换为 onCreate,现在可以使用了,谢谢

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


【解决方案1】:

我已经重构了你的代码:

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()

【讨论】:

  • 感谢您重构代码,它现在可以工作,但我唯一的问题是屏幕只显示列表中的项目数。例如,我在列表中有 2 种药物,它就像我之前提到的黑屏,但列表就像一个弹出窗口,并且该屏幕的大小基本上包含了该列表中的内容。
  • 你的活动应该是全屏的
  • 我已经包含了显示我如何打开片段提醒的主要活动
  • 对不起,我查看了您的代码,一切正常。 FragmentReminder 活动应该是全屏的,而不是弹出窗口。你确定你替换了代码吗,对我来说它看起来像一个 FragmentDialog
  • 我也替换了代码,它不会改变,我已经包含了 customlists java 代码。这就是该列表的创建方式,尽管我认为问题不存在
猜你喜欢
  • 1970-01-01
  • 2013-02-18
  • 2018-05-11
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多