不使用ListActivity创建菜单列表

时间:2015-09-11 16:39:33

标签: android

我想创建一个带操作栏的列表菜单,因此我无法使用ListActivity。我创建了以下代码,但在测试时没有显示列表:

public class MainActivity extends AppCompatActivity {

ListView RecordList;
ArrayList<String> sectionArr;
ListAdapter adapter1;   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getRecordSet(); //A procedure to get list from SQLite DB (tested ok)

    adapter1 = new CardListAdapter(this, sectionArr);
    RecordList = (ListView)findViewById(R.id.listSet);
    RecordList.setAdapter(adapter1);        

    RecordList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) 
        {

        }
    });
}...

在我的自定义适配器中,代码为:

class CustomAdapter extends ArrayAdapter<String>{

public CustomAdapter(Context context, ArrayList<String> strArr) {
    super(context, R.layout.custom_row, strArr);

}

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

    View rowView = convertView;
    int[] colors = new int[] {0x30A1B6AE, 0x30ECE7E7};
    int colorPos = position % colors.length;

    if (rowView==null) {
        LayoutInflater inflator = LayoutInflater.from(getContext());
        rowView = inflator.inflate(R.layout.custom_row, parent, false);
        rowView.setBackgroundColor(colors[colorPos]);
    }

    String singleMenuItem = getItem(position);
    TextView menuItemText = (TextView) rowView.findViewById(R.id.idItem);

    menuItemText.setText(singleMenuItem);

    return rowView;

}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();  
}

}

布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
tools:context="com.lusterview.studycards.MainActivity" >


<ListView
    android:id="@+id/listSet"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="10dp"
    android:cacheColorHint="@android:color/black" >
</ListView>

请告知。

提前致谢。

0 个答案:

没有答案