CustomListview showing status twice in the listview

五迷三道 提交于 2019-12-24 12:20:07

问题


I am working on a project where there is a custom listview which shows different fields and shows task status.The data is recieved as an object of a class

The class i use to invoke the base adapter is as follows:

package com.igloo.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.igloo.adapters.CustomTaskListAdapter;
import com.igloo.classes.Applied_job_data;
import com.igloo.marinaretail.R;
import com.igloo.marinaretail.UserMain;

public class TaskDescriptionFragment extends Fragment {
    ListView list_tasks_final;
    Applied_job_data job_object,job_object_new;
    CustomTaskListAdapter ctadp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.taskdescriptionfragment, container, false);
        list_tasks_final=(ListView) view.findViewById(R.id.list_tasks_final);
        job_object=(Applied_job_data) getArguments().getSerializable("job_object");
        ctadp=new CustomTaskListAdapter(getActivity(), job_object);
        list_tasks_final.setAdapter(ctadp);


        return view;

}
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
         job_object_new=((UserMain) getActivity()).new_job_object;

        if(job_object_new!=null)
        {
        ctadp=new CustomTaskListAdapter(getActivity(), job_object_new);
        }
        else
        {
            ctadp=new CustomTaskListAdapter(getActivity(), job_object);
        }
        list_tasks_final.setAdapter(ctadp);
        ctadp.notifyDataSetChanged();

    }
    @Override
    public void onDestroyView() {

        super.onDestroyView();
    }
}

The adapter is as follows:

package com.igloo.adapters;

import com.igloo.adapters.CustomMyJobsAdapter.MyJobHolder;
import com.igloo.classes.Applied_job_data;
import com.igloo.classes.Task_data;
import com.igloo.marinaretail.R;
import com.igloo.marinaretail.UserMain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CustomTaskListAdapter extends BaseAdapter {

    Context context;
    Applied_job_data job_object;
    LayoutInflater tasks_inflate;

    public CustomTaskListAdapter(Context context,Applied_job_data job_object)
    {
        this.context=context;
        this.job_object=job_object;
        tasks_inflate=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return job_object.tasklist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return job_object.tasklist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyTasksHolder holder;
        if(convertView==null)
        {
            convertView=tasks_inflate.inflate(R.layout.list_tasks_single,null);
            holder =new MyTasksHolder();
            holder.txttaskname=(TextView) convertView.findViewById(R.id.txttaskname);
            holder.txttasktype=(TextView) convertView.findViewById(R.id.txttasktype);
            holder.txttaskprice=(TextView) convertView.findViewById(R.id.txttaskprice);
            holder.txttaskstatus=(TextView) convertView.findViewById(R.id.txttaskstatus);
            holder.root_tasks_layout=(RelativeLayout) convertView.findViewById(R.id.root_tasks_layout);
            convertView.setTag(holder);
        }
        else
        {
            holder = (MyTasksHolder) convertView.getTag();
        }
        holder.txttaskname.setText(job_object.tasklist.get(position).task_name);
        holder.txttaskprice.setText("Rs "+job_object.tasklist.get(position).task_price);
        holder.txttasktype.setText(job_object.tasklist.get(position).task_type);
        if(job_object.tasklist.get(position).task_status.equals("COMPLETED"))
        {
            holder.txttaskstatus.setText("DONE");
        }
        holder.root_tasks_layout.setTag(job_object);

        holder.root_tasks_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Applied_job_data data=(Applied_job_data)((RelativeLayout)v).getTag();
                //Toast.makeText(context,"Clicked "+data.tasklist.get(position).task_name,Toast.LENGTH_LONG).show();
                if(data.tasklist.get(position).task_type.equals("IMAGE"))
                {
                    String task_id=data.tasklist.get(position).task_id;
                    String task_status=data.tasklist.get(position).task_status;
                    ((UserMain)context).showcamerafragment(data,task_id,task_status);
                }
                if(data.tasklist.get(position).task_type.equals("TEXT"))
                {

                }
                if(data.tasklist.get(position).task_type.equals("IMAGE_TEXT"))
                {

                }
            }
        });
        ((UserMain) context).new_job_object=null;
        return convertView;
    }
    static class MyTasksHolder
    {
        RelativeLayout root_tasks_layout;
        TextView txttaskname;
        TextView txttasktype;
        TextView txttaskprice;
        TextView txttaskstatus;
    }

}

The issue is at the line where i set status "done" for a textview in the listview.

if(job_object.tasklist.get(position).task_status.equals("COMPLETED"))
        {
            holder.txttaskstatus.setText("DONE");
        }

The issue is i am getting the done message for two fields.When it has to be only for one.

Also i am using onviewcreated method for getting the object when i back press into the fragment.

When i debugged the code, the customlistview runs more than three times,that is the size of the object.But i get the done message in two of the list items.I want it only once.Please help


回答1:


This may or may not ultimately solve your problem, but you should implement this nonetheless. Listviews do view recycling. Meaning each row can be assigned to a different position at a later time. This is especially likely in listviews that are scrollable. When they are reassigned they carry all the data that's been placed in their views (e.g. text you put with textView.setText()).

By doing this, the programmer doesn't have to reinitialize values that do not change after initial setup. But it does mean something you marked "done" for instance, might be reassigned to another row, and carry its "done" over there. The way you get around this is by always setting the text to the correct value in the getView() method if you changed it all after your one-time setup method (e.g. where you inflated it).

Thus...

if(job_object.tasklist.get(position).task_status.equals("COMPLETED"))
    {
        holder.txttaskstatus.setText("DONE");
    }else{
        //I've added this else clause to your code.
        holder.txttaskstatus.setText("MyTextIfNotDone");
    }


来源:https://stackoverflow.com/questions/26945004/customlistview-showing-status-twice-in-the-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!