多维数组列表仅显示第一项

时间:2018-05-18 12:46:47

标签: android arrays arraylist multidimensional-array

我想从我的XML文件中读出一个多维数组列表。该列表来自DeviceInformationOverview类型。

DeviceInformationOverview对象包含Name,Weight和Repetitions属性。

我的第一个问题是,当读出数组元素时,会将名称的值添加到每个属性中。

我的第二个问题是,只有相同的项目才会添加到列表中。我没有6个不同的元素,但列表中只有一个元素。

我不确定这两个问题是否相关或是否是两个不同的错误。

代码:

XML with Array

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="device_0">
        <item name="name">1b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
    <array name="device_1">
        <item name="name">2b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
    <array name="device_2">
        <item name="name">1b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
    <array name="device_3">
        <item name="name">3b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
    <array name="device_4">
        <item name="name">1b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
    <array name="device_5">
        <item name="name">4b</item>
        <item name="weight">5kg</item>
        <item name="repetition">0</item>
    </array>
</resources>

资源助手

import android.content.Context;
import android.content.res.TypedArray;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import ch.lbuehler.updateapp.R;

public class ResourceHelper {

    public static List<TypedArray> getMultiTypedArray(Context context, String key) {
        List<TypedArray> array = new ArrayList<>();
        try {
            Class<R.array> res = R.array.class;
            Field field;
            int counter = 0;

            do {
                field = res.getField(key + "_" + counter);
                array.add(context.getResources().obtainTypedArray(field.getInt(null)));
                counter++;
            } while (field != null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return array;
        }
    }
}

活动

公共类TrainingPlan扩展了AppCompatActivity {

private TrainingPlanListAdapter listAdapter;


GridView listView;
List<DeviceInformationOverview> devices = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_training_plan);

    for (TypedArray item : ResourceHelper.getMultiTypedArray(this, "device")) {
        String packageName = getPackageName();
        String deviceName = item.getString(item.getResources().getIdentifier("name", "string", packageName));
        String weight = item.getString(item.getResources().getIdentifier("weight", "string", packageName));
        String repetition = item.getString(item.getResources().getIdentifier("repetition", "string", packageName));
        DeviceInformationOverview deviceInformationOverview = new DeviceInformationOverview();
        deviceInformationOverview.setDeviceName(deviceName);
        deviceInformationOverview.setWeight(weight);
        deviceInformationOverview.setRepetitionCount(repetition);
        devices.add(deviceInformationOverview);
    }

    setTitle("Trainings Plan");

    listAdapter = new TrainingPlanListAdapter(this, devices);
    listView = (GridView) findViewById(R.id.gridView);
    listView.setAdapter(listAdapter);
}

}

TrainingPlanAdapter 它将为ListView创建一个元素。

公共类TrainingPlanListAdapter扩展了ArrayAdapter {

private final Activity context;
private final List<DeviceInformationOverview> deviceNames;
private Button deviceButton;

public TrainingPlanListAdapter(Activity context, List<DeviceInformationOverview> deviceNames) {
    super(context, R.layout.exercise_overview_item, deviceNames);
    this.context = context;
    this.deviceNames = deviceNames;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View exerciseOverviewItem=inflater.inflate(R.layout.exercise_overview_item, null,true);

    //this code gets references to objects in the listview_row.xml file
    final TextView weightField = (TextView) exerciseOverviewItem.findViewById(R.id.weightInfo);
    final TextView repetitionField = (TextView) exerciseOverviewItem.findViewById(R.id.repeatitionInfo);
    deviceButton = (Button) exerciseOverviewItem.findViewById(R.id.buttonDevice);

    deviceButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent data = new Intent(context, DeviceDetailActivity.class);
            data.putExtra("deviceTitle",((Button)v).getText());
            data.putExtra("actualWeight", weightField.getText());
            data.putExtra("actualRepetition", repetitionField.getText());
            context.finish();
            context.startActivity(data);
        }
    });

    for(DeviceInformationOverview deviceInformationOverview : deviceNames) {
        //this code sets the values of the objects to values from the arrays
        deviceButton.setText(deviceInformationOverview.getDeviceName());
        weightField.setText(deviceInformationOverview.getWeight());
        repetitionField.setText(deviceInformationOverview.getRepetitionCount());

    }
    return exerciseOverviewItem;
}

}

我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

尝试使用已知的索引,而不是在您提及的行上找到标识符。

@SuppressWarnings("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_training_plan);

    for (TypedArray item : ResourceHelper.getMultiTypedArray(this, "device")) {
        String packageName = getPackageName();
        String deviceName = item.getString(0);
        String weight = item.getString(1);
        String repetition = item.getString(2);

在适配器中,您需要根据当前位置设置文本。将为每个项目调用getView 替换:

for(DeviceInformationOverview deviceInformationOverview : deviceNames) {
    //this code sets the values of the objects to values from the arrays
    deviceButton.setText(deviceInformationOverview.getDeviceName());
    weightField.setText(deviceInformationOverview.getWeight());
    repetitionField.setText(deviceInformationOverview.getRepetitionCount());

    }

使用:

//this code sets the values of the objects to values from the arrays
   deviceButton.setText(deviceNames.get(position).getDeviceName());
   weightField.setText(deviceNames.get(position).getWeight());
   repetitionField.setText(deviceNames.get(position).getRepetitionCount());