您好我尝试在Gridview
上使用我自己的适配器。
我的问题是getView中的位置没有增加。
这是我的代码:
public class WorkourLogBuilder {
String[] values = new String[11];
Context mContext;
Map<Integer, String[]> valuesArray;
int rowsCount = 0;
int mode;
int adappterRowsCount = 0;
static final int AEROBIC = 1;
static final int ANAEROBIC = 0;
public WorkourLogBuilder(Context context, int modeChoosed)
{
mContext = context;
mode = modeChoosed;
valuesArray = new HashMap<Integer, String[]>();
}
public void commintRow()
{
valuesArray.put(rowsCount, values);
rowsCount++;
Log.i("rowsCount", rowsCount+"");
values = new String[11];
}
public void setWorkoutWeight(String value)
{
values[0] = value;
}
public void setSets(String value)
{
values[1] = value;
}
public void setReps(String value)
{
values[2] = value;
}
public void setWorkTime(String value)
{
values[3] = value;
}
public void setWDayTime(String value)
{
values[4] = value;
}
public void setDistance(String value)
{
values[5] = value;
}
public void setSpeed(String value)
{
values[6] = value;
}
public void setRestTime(String value)
{
values[7] = value;
}
public void setCaloires(String value)
{
values[8] = value;
}
public void setHeartBeat(String value)
{
values[9] = value;
}
public void setComment(String value)
{
values[10] = value;
}
public View build()
{
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View v = inflater.inflate(R.layout.workout_log_row, null, false);
GridView grid = (GridView) v.findViewById(R.id.table);
Log.i("count", valuesArray.size()+"");
grid.setAdapter(new ImageAdapter());
return v;
}
public class ImageAdapter extends BaseAdapter {
public int getCount() {
return (valuesArray.size()+6);
}
public Object getItem(int position) {
return valuesArray.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("position", position+"");
if(position < 6)
{
ImageView image = new ImageView(mContext);
if(position < 3)
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.small_green_apple));
else
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.anonymous));
image.setScaleType(ImageView.ScaleType.FIT_XY);
return image;
}
TextView textValue = new TextView(mContext);
textValue.setTextColor(Color.BLACK);
GridView.LayoutParams lp = new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.MATCH_PARENT);
textValue.setLayoutParams(lp);
String[] currecntRow = valuesArray.get(adappterRowsCount);
switch(position % 6)
{
case 0:
{
if(adappterRowsCount != 6)
adappterRowsCount++;
if(mode == AEROBIC)
textValue.setText(currecntRow[5]);
else
textValue.setText(currecntRow[0]);
break;
}
case 1:
{
if(mode == AEROBIC)
textValue.setText(currecntRow[3]);
else
textValue.setText(currecntRow[2]);
break;
}
case 2:
{
if(mode == AEROBIC)
textValue.setText(currecntRow[6]);
else
textValue.setText(currecntRow[1]);
break;
}
case 3:
{
if(mode == AEROBIC)
textValue.setText(currecntRow[1]);
else
textValue.setText(currecntRow[7]);
break;
}
case 4:
{
if(mode == AEROBIC)
textValue.setText(currecntRow[7]);
else
textValue.setText(currecntRow[3]);
break;
}
case 5:
{
if(mode == AEROBIC)
textValue.setText(currecntRow[10]);
else
textValue.setText(currecntRow[10]);
break;
}
}
return textValue;
}
}`enter code h
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:numColumns="6" >
</GridView>
</RelativeLayout>
这是我使用WorkourLogBuilder
class
public void setTheLogViewTest(String date)
{
String[][] values = data.dayLog(date);
ArrayList<ExpandListGroup> listAll = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> listChilds = new ArrayList<ExpandListChild>();
ExpandListGroup group = new ExpandListGroup();
ExpandListChild child = new ExpandListChild();
int length = values[0].length;
WorkourLogBuilder builder = null;
for(int i = 0; i < length; i++)
{
String exrcise = values[3][i];
String restTime;
String setTime;
String weight;
String reps;
String comment;
String sets;
String speed;
String distance;
if (values[9][i] == null || values[9][i].equals(""))
speed = mResources.getString(R.string.Undefined);
else {
if (data.getUnits().equals("metric"))
speed = values[9][i] + " " + mResources.getString(R.string.km_hour);
else
speed = values[9][i] + " " + mResources.getString(R.string.miles_hour);
}
if (values[10][i] == null || values[10][i].equals(""))
restTime = mResources.getString(R.string.Undefined);
else
restTime = getTimesPharsed(values[10][i]);
if (values[6][i] == null || values[6][i].equals(""))
setTime = mResources.getString(R.string.Undefined);
else
setTime = getTimesPharsed(values[6][i]);
if (values[4][i] == null || values[4][i].equals(""))
weight = mResources.getString(R.string.Undefined);
else {
if (data.getUnits().equals("metric"))
weight = values[4][i] + " " + mResources.getString(R.string.kg);
else
weight = values[4][i] + " " + mResources.getString(R.string.lbs);
}
if (values[5][i] == null || values[5][i].equals(""))
reps = mResources.getString(R.string.Undefined);
else
reps = values[5][i];
if (values[11][i] == null || values[11][i].equals(""))
comment = mResources.getString(R.string.Undefined);
else
comment = values[11][i];
if (values[12][i] == null || values[12][i].equals(""))
sets = mResources.getString(R.string.Undefined);
else
sets = values[12][i] + checkForFailedSets(values[1][i]);
if (values[8][i] == null || values[8][i].equals(""))
distance = mResources.getString(R.string.Undefined);
else {
if (data.getUnits().equals("metric"))
distance = values[8][i] + " " + mResources.getString(R.string.km);
else
distance = values[8][i] + " " + mResources.getString(R.string.miles);
}
if(i == 0)
{
group = new ExpandListGroup();
group.setName(exrcise, this);
Log.i("test", "1");
if(data.getAerobic(exrcise))
builder = new WorkourLogBuilder(this, ANAEROBIC);
else
builder = new WorkourLogBuilder(this, AEROBIC);
builder.setWorkoutWeight(weight);
builder.setReps(reps);
builder.setSets(sets);
builder.setWorkTime(setTime);
builder.setRestTime(restTime);
builder.setComment(comment);
builder.setDistance(distance);
builder.setSpeed(speed);
builder.commintRow();
}
else if (i > 0 && exrcise.equals(values[3][i - 1]) == false && i == length - 1 == false)
{
Log.i("test", "2");
View v = builder.build();
child = new ExpandListChild();
child.setLayouts((RelativeLayout) v);
listChilds.add(child);
group.setItems(listChilds);
listAll.add(group);
group = new ExpandListGroup();
group.setName(exrcise, this);
if(data.getAerobic(exrcise))
builder = new WorkourLogBuilder(this, ANAEROBIC);
else
builder = new WorkourLogBuilder(this, AEROBIC);
builder.setWorkoutWeight(weight);
builder.setReps(reps);
builder.setSets(sets);
builder.setWorkTime(setTime);
builder.setRestTime(restTime);
builder.setComment(comment);
builder.setDistance(distance);
builder.setSpeed(speed);
builder.commintRow();
}
else if (i == length - 1 && exrcise.equals(values[3][i - 1]) == false)
{
Log.i("test", "3");
View v = builder.build();
child = new ExpandListChild();
child.setLayouts((RelativeLayout) v);
listChilds.add(child);
group.setItems(listChilds);
listAll.add(group);
group = new ExpandListGroup();
group.setName(exrcise, this);
if(data.getAerobic(exrcise))
builder = new WorkourLogBuilder(this, ANAEROBIC);
else
builder = new WorkourLogBuilder(this, AEROBIC);
builder.setWorkoutWeight(weight);
builder.setReps(reps);
builder.setSets(sets);
builder.setWorkTime(setTime);
builder.setRestTime(restTime);
builder.setComment(comment);
builder.setDistance(distance);
builder.setSpeed(speed);
builder.commintRow();
v = builder.build();
child = new ExpandListChild();
child.setLayouts((RelativeLayout) v);
listChilds.add(child);
group.setItems(listChilds);
listAll.add(group);
}
else if (i == length - 1 && exrcise.equals(values[3][i - 1]) == true)
{
Log.i("test", "4");
if(data.getAerobic(exrcise))
builder = new WorkourLogBuilder(this, ANAEROBIC);
else
builder = new WorkourLogBuilder(this, AEROBIC);
builder.setWorkoutWeight(weight);
builder.setReps(reps);
builder.setSets(sets);
builder.setWorkTime(setTime);
builder.setRestTime(restTime);
builder.setComment(comment);
builder.setDistance(distance);
builder.setSpeed(speed);
builder.commintRow();
View v = builder.build();
child = new ExpandListChild();
child.setLayouts((RelativeLayout) v);
listChilds.add(child);
group.setItems(listChilds);
listAll.add(group);
}
else
{
Log.i("test", "5");
builder.setWorkoutWeight(weight);
builder.setReps(reps);
builder.setSets(sets);
builder.setWorkTime(setTime);
builder.setRestTime(restTime);
builder.setComment(comment);
builder.setDistance(distance);
builder.setSpeed(speed);
builder.commintRow();
}
}
ExpAdapter = new ExpandListAdapter(DatePage.this, listAll);
ExpandList.setAdapter(ExpAdapter);
}
当我看到logcat i时,位置记录始终为0。
为什么位置没有增加?
答案 0 :(得分:3)
您未正确返回getItem()
:
public Object getItem(int position) {
return position;
}
应该是:
public Object getItem(int position) {
return valuesArray.get(position);
}
答案 1 :(得分:2)
getItem()
public Object getItem(int position) {
return valuesArray.get(position);
}
它应该返回Object
而不是位置,所以基本上它总是返回0