以编程方式将RelativeLayout添加到ListView项

时间:2014-04-14 17:23:35

标签: android android-listview relativelayout

我尝试将ListView置于android ListView中的另一个Fragment内,但我最后结束了这样的事情:

enter image description here

在日期之前,我需要打印来自LinkedHashMap<String, String>的键值对。我用这个适配器作为主视图:

public class HistoryAdapter extends BaseAdapter {

private LinkedHashMap<String, LinkedHashMap<String, String>> mapMeasurements = new LinkedHashMap<String, LinkedHashMap<String, String>>();
private LayoutInflater layoutInflater;
private Context context;
private String[] mapKeys;

public HistoryAdapter(Context context, LinkedHashMap<String, LinkedHashMap<String, String>> mapMeasurements) {
    this.mapMeasurements = mapMeasurements;
    this.context = context;
    mapKeys = mapMeasurements.keySet().toArray(new String[mapMeasurements.size()]);
    layoutInflater = LayoutInflater.from(this.context);
}

@Override
public int getCount() {
    return mapMeasurements == null ? 0 : mapMeasurements.size();
}

@Override
public LinkedHashMap<String, String> getItem(int position) {
    return mapMeasurements.get(mapKeys[position]);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = layoutInflater.inflate(
                R.layout.measurements_history_day, null);
        viewHolder = new ViewHolder();
        assert convertView != null;
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.dayDate = (TextView) convertView.findViewById(R.id.measurement_history_day_date);
    viewHolder.dailyMeasurements = (ListView) convertView.findViewById(R.id.measurement_history_day_items);
    viewHolder.dayDate.setText(mapKeys[position]);

    LinkedHashMap<String, String> singleDayMap = getItem(position);

    HistoryDayAdapter historyDayAdapter = new HistoryDayAdapter(context, singleDayMap);
    viewHolder.dailyMeasurements.setAdapter(historyDayAdapter);

    return convertView;
}

private class ViewHolder {
    public TextView dayDate;
    public ListView dailyMeasurements;
}

现在我知道,将ListView放在ListView内是个坏主意,所以我需要以编程方式添加一些RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/measurements_history_day_row"
android:layout_margin="10dp"
android:padding="10dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/measurement_type"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/measurement_value"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true" />
</RelativeLayout>

在dayItem里面。怎么做?

0 个答案:

没有答案