所以我遇到的问题是,我有一个哈希表与键值是ListView中显示的所有数据,而不只是我指定
关键:整数 值: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} 其中number是散列表中的位置我想存储字符串。
这里是我将数据保存到散列表的地方。一切看起来都适合我,也许我一直盯着它太久了@@
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();
}
}
这似乎只是移动数据而不是修复事物。这是否有意义? 另外,我只是发现我的hashtable以某种方式将所有东西都放在每个位置...... – cj1098
那么我看不到原始代码甚至没有告诉阵列适配器数组是什么。在任何情况下,我都不鼓励使用全局变量(静态)。 (看来我对ArrayList数据有效性的怀疑是正确的;-) –
(对第二段代码添加了一个响应 - 请参阅上面的编辑。) –