android - listView 显示所有数据,而不仅仅是我指定的数据

标签 android listview adapter

所以我遇到的问题是我有一个哈希表,其键值为

键:整数 值:ArrayList

在我的自定义 listView 适配器中,我将其设置为仅显示哈希表中特定位置的值..但目前它显示的是整个哈希表。我不太确定我做错了什么。这是适配器

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

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

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

{数字,字符串,数字,字符串} 其中数字是哈希表中我要存储字符串的位置。

这里是我将数据保存到哈希表的地方。在我看来一切都很好,也许我盯着它看得太久了@.@

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

最佳答案

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

例如在构造函数中...

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?如果您将相同的对象分配给哈希表的所有条目,那么它们都将包含相同的列表。

关于android - listView 显示所有数据,而不仅仅是我指定的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995997/

相关文章:

Android、SlidingPaneLayout 和 listView

android - 在 Android 中将 JSON 字符串转换为字典

java - 为什么数据没有显示在我的 ListView 上?

android - 选择导航项时,抽屉导航未关闭

c++ - C/C++ : Is there a performance penalty for using the adapter pattern?

android - 带有 4 个 TextView 的 ListView

安卓工作室 : Error:Cause: unable to find valid certification path to requested target

java - 创建一个上面有文字的矩形

Android Listview Rows 需要全屏

c# - 如何使用C#启用禁用的无线网卡