我正在开发一个项目,其中有一个自定义列表视图,显示不同的字段并显示任务状态。数据被接收为类的对象
我用来调用基本适配器的类如下:
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();
}
}
适配器如下:
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;
}
}
问题出在我在列表视图中为textview设置状态为“已完成”的行。
if(job_object.tasklist.get(position).task_status.equals("COMPLETED"))
{
holder.txttaskstatus.setText("DONE");
}
问题是我收到两个字段的完成消息。当它必须只有一个。
此外,我正在使用onviewcreated方法获取对象,当我重新按下片段时。
当我调试代码时,customlistview运行三次以上,即对象的大小。但我在两个列表项中得到完成消息。我只想要一次。请帮助
答案 0 :(得分:1)
这可能会或可能不会最终解决您的问题,但您仍应实施此问题。 Listviews确实可以查看回收。意味着每行可以在以后分配到不同的位置。这在可滚动的列表视图中尤其可能。重新分配后,它们会将所有数据放在其视图中(例如,使用textView.setText()放置的文本)。
通过这样做,程序员不必重新初始化在初始设置后不会改变的值。但它确实意味着你标记的东西"完成"例如,可能会被重新分配到另一行,然后执行"完成"在那边。解决这个问题的方法是,如果在一次性设置方法之后更改了所有文本(例如,在你充气的地方),总是将文本设置为getView()方法中的正确值。
...因此
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");
}