0

我只是想使用一个简单的适配器创建一个列表视图。setadapter 不工作。这是我的代码。我的 setcontentview 在这段代码之前,它在 oncreate 中。

        ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("CType", "Alarm");
map.put("RType", "Once");
map.put("hour", "1");
map.put("minute", "1");
map.put("second", "30");
arraylist.add(map);
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"};
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce,
        R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds};
ListView clocklistview = (ListView)findViewById(R.id.clocklistview);
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray);

clocklistview.setAdapter(adapter);

这是包含我的列表视图的 xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
        android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ListView 
    android:id="@+id/clocklistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView></LinearLayout>

这是给我的 list_clock.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3">
    <TextView
        android:id="@+id/clocktextviewalarmtimer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/clocktextviewrepeatonce"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/clocktextviewhours"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="0.3"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/clocktextviewminutes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="0.3"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/clocktextviewseconds"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_weight="0.3"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>
4

1 回答 1

1

这是一个自定义适配器的示例。尝试这个 ...

public class ClockListAdapter extends BaseAdapter {

    ArrayList<HashMap<String, String>> _arrayList;

    ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
        this._arrayList = arrayList;
    }

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

    @Override
    public Object getItem(int position) {
        return (_arrayList != null) ? _arrayList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0;
    }

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

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            // REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv
            convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder._textView.setText("set some text here");
        return convertView;
    }

    public class ViewHolder {
        // ADD YOUR VIEW(S) vvvvvvvvv
        TextView _textView;

        ViewHolder(View v) {
            // REPLACE WITH YOUR TEXT vvvvvvvvv
            _textView = (TextView) v.findViewById(R.id.textView1);
        }
    }
}
于 2013-03-23T20:30:31.053 回答