简体   繁体   English

按下对象后,将TextView文本更改为“普通”

[英]Change TextView text to Normal after the object has been pressed

I currently have a ListView which uses a CustomAdapter to display the information from my Firebase Database. 我目前有一个ListView,它使用CustomAdapter来显示Firebase数据库中的信息。 Which looks like the following: 如下所示:

列表显示

Upon creation I want the text in the green boxes (The ListView Items) to be bold. 创建后,我希望绿色框中的文本(ListView项目)为粗体。 Once the user presses on that ListView item I want the text to go to Normal, unbolded. 一旦用户按下该ListView项,我希望文本变为非粗体的“普通”。

I want it to work similar to an email. 我希望它的工作方式类似于电子邮件。 When you get a new email the title is bold and once that email is opened and you go back to your inbox the text is the Normal, unbolded. 收到新电子邮件时,标题为粗体,一旦打开该电子邮件,然后返回到收件箱,则该文本为普通文本,不加粗体。

The following is all my relevant code: 以下是我所有相关的代码:

MyJobsFragment, SelectItem MyJobsFragment,SelectItem

// Press the object and display the information and sign the job of with signature pad

    jobListViewMyAcJobs.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
            bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
            activeJobDetailsFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
        }
    });

MyCustomAdapter MyCustomAdapter

public class MyCustomAdapter extends BaseAdapter
    {
        private ArrayList<JobInformation> mData = new ArrayList<>();
        private ArrayList<JobInformation> mDataOrig = new ArrayList<>();
        private ArrayList<String> mDataKeys = new ArrayList<>();

        private LayoutInflater mInflater;

        ... Removed Code ...

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            // Bid on holder
            MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn holderBidOn;
            // Accepted holder
            final MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted holderAccepted;
            // Completed holder
            MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted holderCompleted;

            if (convertView == null)
            {
                // Bid on
                if (host.getCurrentTab() == 0)
                {
                    convertView = mInflater.inflate(R.layout.job_info_bid_on, null);
                    holderBidOn = new MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn();

                    holderBidOn.textViewJobName = convertView.findViewById(R.id.textName);
                    holderBidOn.textViewJobDescription = convertView.findViewById(R.id.textDesc);

                    holderBidOn.textViewJobName.setText(mData.get(position).getAdvertName());
                    holderBidOn.textViewJobDescription.setText(mData.get(position).getAdvertDescription());

                    convertView.setTag(holderBidOn);
                }
                // Accepted
                else if (host.getCurrentTab() == 1)
                {
                    convertView = mInflater.inflate(R.layout.job_info_accepted, null);
                    holderAccepted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted();

                    holderAccepted.textViewJobName = convertView.findViewById(R.id.textName);
                    holderAccepted.textViewDescription = convertView.findViewById(R.id.textDesc);

                    holderAccepted.textViewJobName.setText(mData.get(position).getAdvertName());
                    if(text != null)
                    {
                        text.setTypeface(null, Typeface.BOLD);
                    }

                    holderAccepted.textViewDescription.setText(mData.get(position).getAdvertDescription());
                    // TODO - Was going to add the the users bid into this here, however it's difficult as the bid isnt stored in the jobs table
                    // TODO - I tried to add it in, looped through the bids table and found the bids with the mDataKeys. But it always displayed the last value.

                    convertView.setTag(holderAccepted);
                }
                // Completed
                else if (host.getCurrentTab() == 2)
                {
                    convertView = mInflater.inflate(R.layout.job_info_list_completed, null);

                    holderCompleted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted();

                    holderCompleted.textViewJobName = convertView.findViewById(R.id.textName);
                    holderCompleted.textViewJobName.setText(mData.get(position).getAdvertName());

                    convertView.setTag(holderCompleted);
                }
            } else
            {
                if (host.getCurrentTab() == 0)
                {
                    holderBidOn = (MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn) convertView.getTag();
                } else if (host.getCurrentTab() == 1)
                {
                    holderAccepted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted) convertView.getTag();
                } else if (host.getCurrentTab() == 2)
                {
                    holderCompleted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted) convertView.getTag();
                }
            }

            return convertView;
        }

        public class GroupViewHolderBidOn
        {
            public TextView textViewJobName;
            public TextView textViewJobDescription;
        }

        public class GroupViewHolderAccepted
        {
            public TextView textViewJobName;
            public TextView textViewDescription;
        }

        public class GroupViewHolderCompleted
        {
            public TextView textViewJobName;
        }
}

Well there multiple ways of achieving that. 好了,有多种方法可以实现这一目标。 I would suggest you to add a field in your JobInformation class, something like 我建议您在JobInformation类中添加一个字段,例如

public class JobInformation{
    .
    .
    .
    .
    . // other fields

    private boolean isSelected;

    public boolean isSelected() {
        return isSelected;
    }
    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    .
    .
    . // Other getters/setters

}

Now you need to set this field when you click an item, in your onItemClick function do this 现在,您需要在单击项目时设置此字段,在onItemClick函数中执行此操作

public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
            Bundle bundle = new Bundle();
            adapterActiveJobs.mData.get(position).setSelected(true); // add this line in your code
            bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
            bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
            activeJobDetailsFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
        }

finally in your getView method in your custom adapter, check for isSelected field and style your textview accordingly, add this in your getView method. 最后,在自定义适配器的getView方法中,检查isSelected字段并相应地设置textview样式,然后将其添加到getView方法中。

if(mData.get(position).isSelected()){
    // set your textview typeface to normal
}
else{
    // set your texview typeface to bold.
}

and you would need to call adapter.notifyDataSetChanged() whenever you update selected variable so that your getView is called. 并且每次更新selected变量时都需要调用adapter.notifyDataSetChanged()以便调用getView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM