listView显示所有数据而不是我指定的数据

时间:2012-10-21 07:54:49

标签: android listview adapter

所以我遇到的问题是我的哈希表的关键值是

键:整数 值:ArrayList

在我的自定义listView适配器中,我将其设置为仅显示散列表中某个位置的值。但此时它正在显示整个散列表。我不太确定我做错了什么..这是适配器

注意:Statics.mainPageListPosition是一个静态int,当用户点击主屏幕上的某个位置时,它会被设置。我希望适配器然后显示该位置,并且只显示该位置的数据。

package assignments;

import java.util.ArrayList;
import java.util.Hashtable;

import com.example.mt_study.R;
import com.example.statics.Statics;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AssignmentListViewAdapter extends ArrayAdapter<Assignment> {

    private LayoutInflater inflater;
    private Hashtable<Integer, ArrayList<Assignment>> assignmentList;

    // class used to hold views when the user scrolls on the listView
    // it stores them and then we can re-use their id's as the user scrolls
    // down or up the screen so we don't have to keep calling findViewById
    private class assignmentViewHolder {
        TextView Title;
        TextView Date;

        public assignmentViewHolder(TextView assignmentTitle, TextView Date) {
            this.Title = assignmentTitle;
            this.Date = Date;
        }

        public TextView getTitle() {
            return Title;
        }

        public TextView getDate() {
            return Date;
        }
    }

    public AssignmentListViewAdapter(Context context,
            Hashtable<Integer, ArrayList<Assignment>> data) {
        super(context, R.id.assignment_row_title, R.id.assignment_row_date);
        inflater = LayoutInflater.from(context);
        assignmentList = data;

    }

    // set of functions useful for the getView function
    public int getCount() {
        return Statics.allAssignments.get(Statics.mainPageListPosition).size();
    }

    public Assignment getItem(int position) {
        return assignmentList.get(Statics.mainPageListPosition).get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // Assignment assignment = this.getItem(position);

        TextView assignmentTitle;
        TextView assignmentDate;

        // if theres no view yet created on the screen, create one
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.assignment_row, null);

            // give the views their respective ID's
            assignmentTitle = (TextView) convertView
                    .findViewById(R.id.assignment_row_title);
            assignmentDate = (TextView) convertView
                    .findViewById(R.id.assignment_row_date);

            // set the tag so we can just use the viewHolder instead of calling
            // findViewById over. It saves memory
            convertView.setTag(new assignmentViewHolder(assignmentTitle,
                    assignmentDate));
        }
        // this gets called when the user scrolls down or up the screen and the
        // adapter
        // wants to maximize efficiency by just calling the viewHolder instead
        // of findViewById
        else {
            assignmentViewHolder viewHolder = (assignmentViewHolder) convertView
                    .getTag();
            assignmentTitle = viewHolder.getTitle();
            assignmentDate = viewHolder.getDate();

        }
        // down here is where you set the values for all your views/objects
        // such as Buttons or textViews.
        assignmentTitle.setText(Statics.allAssignments
                .get(Statics.mainPageListPosition).get(position).getTitle());
        assignmentDate.setText(Statics.allAssignments
                .get(Statics.mainPageListPosition).get(position).getDate_due());

        return convertView;
    }
}

编辑:最近的发现,我的哈希表把我发送的每个值放在每个可用的位置lol。我所做的是当用户点击主页面上的位置时,它保存该位置并使用它来存储他们在哈希表中的该位置保存的数据。 Temp是一个arrayList,用户保存的数据以这种格式存储:

{number,String,number,string} 数字是哈希表中我想存储字符串的位置。

这是我将数据保存到哈希表的地方。一切看起来都对我来说,也许我已经盯着它看了太长时间@。@

for (int i = 0; i < temp.size(); i++) {
                if (isInteger(temp.get(i))) {
                    currentAssignment.setTitle(temp.get(i + 1));
                    //currentAssignment.setDate_due(temp.get(i + 2));
                    currentAssignmentList.add(index, currentAssignment);
                    index++;

                    Statics.allAssignments.put(Integer.parseInt(temp.get(i)), currentAssignmentList);
                    //Statics.allAssignments.get(Integer.parseInt(temp.get(i))).add(currentAssignment);
                    currentAssignment = new Assignment();
                    //Toast.makeText(context, "Called" + Integer.toString(i), Toast.LENGTH_SHORT).show();
                }
            }

2 个答案:

答案 0 :(得分:3)

您在哪里设置ArrayAdapter上的项目列表?我认为您应该使用data构造函数参数,或者addAll(data),或者使用super()。请注意,您目前会在getView()中忽略此数据,而是使用您的静态数据。在getView()中,一旦在超类ArrayAdapter中正确设置了项目,就应该使用Assignment a = getItem(position)并从此对象而不是静态设置字段。

E.g。在构造函数中......

super(context, R.id.assignment_row_title, R.id.assignment_row_date, data);

然后在getView()...

Assignment a = getItem(position);
assignmentTitle.setText(a.getTitle());
assignmentDate.setText(a.getDate_due());

这当然假设您传递的列表data是正确的。希望一旦您的代码只查看一个项目列表,您就可以更轻松地验证此列表是否正确。

修改

在你的第二个代码块中,你在哪里为每个哈希表键创建一个新的currentAssignmentList?如果将相同的对象分配给哈希表的所有条目,则它们将包含相同的列表。

答案 1 :(得分:2)

当您更改notifyDataSetChanged值时,请不要忘记在adapter上致电mainPageListPosition