Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.listview;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.TextView;
- public class ListViewDemo extends Activity {
- String[] items = { "Test text 1", "Test text 2", "Test text 3", "Test text 4", "Test text 5", "Test text 6" };
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- ListView list = (ListView) findViewById(R.id.list);
- SpecialAdapter adapter = new SpecialAdapter(this, items);
- list.setAdapter(adapter);
- }
- static class ViewHolder {
- TextView text;
- }
- }
- ---------------------------------------------------------------------------------------------------------
- package com.example.listview;
- import java.util.ArrayList;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import com.example.listview.ListViewDemo.ViewHolder;
- public class SpecialAdapter extends BaseAdapter {
- //Defining the background color of rows. The row will alternate between green light and green dark.
- private int[] colors = new int[] { 0xAAf6ffc8, 0xAA538d00 };
- private LayoutInflater mInflater;
- //The variable that will hold our text data to be tied to list.
- private String[] data;
- public SpecialAdapter(Context context, String[] results) {
- mInflater = LayoutInflater.from(context);
- this.data = results;
- }
- @Override
- public int getCount() {
- return data.length;
- }
- @Override
- public Object getItem(int position) {
- return position;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- //A view to hold each row in the list
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // A ViewHolder keeps references to children views to avoid unneccessary calls
- // to findViewById() on each row.
- ViewHolder holder;
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.row, null);
- holder = new ViewHolder();
- holder.text = (TextView) convertView.findViewById(R.id.headline);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- // Bind the data efficiently with the holder.
- holder.text.setText(data[position]);
- //Set the background color depending of odd/even colorPos result
- int colorPos = position % colors.length;
- convertView.setBackgroundColor(colors[colorPos]);
- return convertView;
- }
- }
- ---------------------------------------------------------------------------------------------------------
- main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
- ---------------------------------------------------------------------------------------------------------row.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <TableLayout
- android:id="@+id/tableLayout1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TableRow >
- <TextView
- android:id="@+id/headline"
- android:layout_width="0px"
- android:layout_height="wrap_content"
- android:layout_weight="0.8"
- android:padding="10dp"
- android:textSize="20dp"
- android:textColor="#FFF"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical|right"
- android:layout_marginRight="6dp"
- android:src="@drawable/arrow_icon" />
- </TableRow>
- </TableLayout>
- </LinearLayout>
- This is my code .. please help me out why list is coming as null??
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement