我为ListView实现了一个自定义数组适配器,但它从不迭代所提供集合中的项目。进入HistoryCustomArrayAdapter的参数的位置/索引始终为0.我希望这个数字在迭代列表时递增,这样我就可以选择集合中的正确项来生成视图。
我尝试了很多不同的实现(ListFragment),但总是遇到同样的问题。我的日志记录显示我的集合中正确数量的项目被传递到HistoryCustomArrayAdapter中,所以我觉得这不是一个问题,数据没有及时加载以进行渲染。我在下面附上了相关的日志记录。
public class HistoryActivity extends BaseMenuActivity implements BaseMenuFragment.OnBaseMenuClickListener{
private static final Logger logger = LoggerFactory.getLogger(HistoryActivity.class.getSimpleName());
private void initializeDefaultView(Bundle savedInstanceState){
FragmentUtils.applyContentAndTitleFragments(this, new HistoryContentFragment(), new HistoryMenuFragment());
}
@Override
protected void onResume() {
super.onResume();
setHistoryButtionActive(findViewById(R.id.base_menu_fragement_container));
}
@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
setHistoryButtionActive(findViewById(R.id.base_menu_fragement_container));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeDefaultView(savedInstanceState);
}
}
public class HistoryContentFragment extends Fragment{
private static final Logger logger = LoggerFactory.getLogger(HistoryContentFragment.class.getSimpleName());
public HistoryContentFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
logger.debug("Creating custom history content view");
View view = inflater.inflate(R.layout.history_content_fragement, container, false);
ListView listview = (ListView) view.findViewById(R.id.history_items_list_container);
List<Custom> customs = getAllCustoms();
logger.debug("Count of customs to add to adapter ["+customs.size()+"]");
HistoryCustomArrayAdapter adapter = new HistoryCustomArrayAdapter(getActivity(), R.layout.history_item_fragment, customs);
listview.setAdapter(adapter);
return view;
}
private static List<Custom> getAllCustoms(){
List<Custom> customs = new Select().from(Custom.class).execute();
return customs;
}
}
public class HistoryCustomArrayAdapter extends ArrayAdapter<Custom>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
private static final Logger logger = LoggerFactory.getLogger(HistoryCustomArrayAdapter.class.getSimpleName());
private final Context context;
private final List<Custom> values;
private int textViewResourceId;
public class ViewHolder {
ImageView activityImage;
TextView locationText;
TextView dateText;
TextView timeText;
}
public HistoryCustomArrayAdapter(Context context, int textViewResourceId, List<Custom> values) {
super(context, textViewResourceId, values);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.values = values;
logger.debug("Initialized from this constructor");
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return values.size();
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Custom getItem(int position) {
return values.get(position);
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
logger.debug("Position ["+position+"]\n"+this.getCount());
ViewHolder holder;
Custom Custom = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = (ViewGroup) inflater.inflate(textViewResourceId, null);
holder = new ViewHolder();
holder.activityImage = (ImageView) convertView.findViewById(R.id.history_item_activity_btn);
holder.dateText = (TextView) convertView.findViewById(R.id.history_item_date);
holder.timeText = (TextView) convertView.findViewById(R.id.history_item_time);
holder.locationText = (TextView) convertView.findViewById(R.id.history_item_location);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
addCustomLocation(Custom,holder);
addCustomThumbs(Custom,holder);
addCustomActivity(Custom,holder);
addRtatingTimeDate(Custom,holder);
return convertView;
}
private void addCustomThumbs(Custom Custom, ViewHolder viewHolder){
}
private void addCustomConnectionType(Custom Custom, ViewHolder viewHolder){
}
private void addCustomActivity(Custom Custom, ViewHolder viewHolder){
}
private void addRtatingTimeDate(Custom Custom, ViewHolder viewHolder){
}
private void addCustomLocation(Custom Custom, ViewHolder viewHolder){
}
}
撤销主要布局资源(activity_main.xml)
<ScrollView
android:id="@+id/main_content_fragement_scrolling_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:id="@+id/main_content_fragement_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
解决方案MAIN LAYOUT history_activity.xml(拉入自己的布局资源并删除ScrollView):
<LinearLayout
android:id="@+id/main_content_fragement_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
正在使用的碎片;是的,我知道我拼错了片段......就像到处都是:)
history_content_fragement.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/history_activity_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="100dp"
android:paddingTop="60dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical">
<ListView
android:id="@+id/history_items_list_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
history_item_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/history_item_general_info"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/history_item_peg_pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:src="@drawable/tnd_peg_pin" />
<TextView
android:id="@+id/history_item_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/history_item_peg_pin"
android:layout_toRightOf="@id/history_item_peg_pin"
style="@style/HistoryHeaderLiteTheme" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/history_item_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/history_item_activity_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/history_item_connection_type"
android:layout_toRightOf="@id/history_item_connection_type" >
<ImageButton
android:id="@+id/history_item_activity_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:id="@+id/history_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/history_item_activity_type"
android:layout_alignParentRight="true"
style="@style/HistoryLiteTheme" />
<TextView
android:id="@+id/history_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/history_item_date"
android:layout_alignParentRight="true"
style="@style/HistoryLiteTheme" />
</RelativeLayout>
</LinearLayout>
日志:
05-07 12:21:54.604: D/AbsListView(10938): Get MotionRecognitionManager
05-07 12:21:54.614: D/HistoryContentFragment(10938): Count of custom to add to adapter [5]
05-07 12:21:54.614: D/HistoryCustomArrayAdap*(10938): Initialized from this constructor
05-07 12:21:54.624: D/HistoryMenuFragment(10938): onStart
05-07 12:21:54.624: D/HistoryMenuFragment(10938): onResume
05-07 12:21:54.644: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.644: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.654: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.654: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.694: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.694: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.704: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.704: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.704: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.704: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.724: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.724: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.734: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.734: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.794: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.794: D/HistoryCustomArrayAdap*(10938): 5
05-07 12:21:54.804: D/HistoryCustomArrayAdap*(10938): Position [0]
05-07 12:21:54.804: D/HistoryCustomArrayAdap*(10938): 5