Advertisement
Guest User

ListViewDemo

a guest
Mar 13th, 2012
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.example.listview;
  19.  
  20. import android.app.Activity;
  21. import android.os.Bundle;
  22. import android.widget.ListView;
  23. import android.widget.TextView;
  24.  
  25. public class ListViewDemo extends Activity {
  26.  
  27. String[] items = { "Test text 1", "Test text 2", "Test text 3", "Test text 4", "Test text 5", "Test text 6" };
  28. @Override
  29. public void onCreate(Bundle icicle) {
  30. super.onCreate(icicle);
  31. setContentView(R.layout.main);
  32. ListView list = (ListView) findViewById(R.id.list);
  33. SpecialAdapter adapter = new SpecialAdapter(this, items);
  34. list.setAdapter(adapter);
  35. }
  36. static class ViewHolder {
  37. TextView text;
  38. }
  39. }
  40. ---------------------------------------------------------------------------------------------------------
  41. package com.example.listview;
  42.  
  43. import java.util.ArrayList;
  44.  
  45. import android.content.Context;
  46. import android.view.LayoutInflater;
  47. import android.view.View;
  48. import android.view.ViewGroup;
  49. import android.widget.BaseAdapter;
  50. import android.widget.TextView;
  51. import com.example.listview.ListViewDemo.ViewHolder;
  52.  
  53. public class SpecialAdapter extends BaseAdapter {
  54. //Defining the background color of rows. The row will alternate between green light and green dark.
  55. private int[] colors = new int[] { 0xAAf6ffc8, 0xAA538d00 };
  56. private LayoutInflater mInflater;
  57.  
  58. //The variable that will hold our text data to be tied to list.
  59. private String[] data;
  60.  
  61. public SpecialAdapter(Context context, String[] results) {
  62. mInflater = LayoutInflater.from(context);
  63. this.data = results;
  64. }
  65.  
  66. @Override
  67. public int getCount() {
  68. return data.length;
  69. }
  70.  
  71. @Override
  72. public Object getItem(int position) {
  73. return position;
  74. }
  75.  
  76. @Override
  77. public long getItemId(int position) {
  78. return position;
  79. }
  80.  
  81. //A view to hold each row in the list
  82. @Override
  83. public View getView(int position, View convertView, ViewGroup parent) {
  84.  
  85. // A ViewHolder keeps references to children views to avoid unneccessary calls
  86. // to findViewById() on each row.
  87. ViewHolder holder;
  88.  
  89. if (convertView == null) {
  90. convertView = mInflater.inflate(R.layout.row, null);
  91.  
  92. holder = new ViewHolder();
  93. holder.text = (TextView) convertView.findViewById(R.id.headline);
  94. convertView.setTag(holder);
  95. } else {
  96. holder = (ViewHolder) convertView.getTag();
  97. }
  98. // Bind the data efficiently with the holder.
  99. holder.text.setText(data[position]);
  100.  
  101. //Set the background color depending of odd/even colorPos result
  102. int colorPos = position % colors.length;
  103. convertView.setBackgroundColor(colors[colorPos]);
  104.  
  105. return convertView;
  106. }
  107.  
  108.  
  109. }
  110. ---------------------------------------------------------------------------------------------------------
  111. main.xml
  112.  
  113.  
  114. <?xml version="1.0" encoding="utf-8"?>
  115. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  116. android:orientation="vertical"
  117. android:layout_width="fill_parent"
  118. android:layout_height="fill_parent"
  119. >
  120. <ListView
  121. android:id="@+id/list"
  122. android:layout_width="fill_parent"
  123. android:layout_height="wrap_content" >
  124. </ListView>
  125. </LinearLayout>
  126. ---------------------------------------------------------------------------------------------------------row.xml
  127.  
  128. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  129. android:layout_width="fill_parent"
  130. android:layout_height="wrap_content" >
  131.  
  132. <TableLayout
  133. android:id="@+id/tableLayout1"
  134. android:layout_width="match_parent"
  135. android:layout_height="wrap_content" >
  136.  
  137. <TableRow >
  138.  
  139. <TextView
  140. android:id="@+id/headline"
  141. android:layout_width="0px"
  142. android:layout_height="wrap_content"
  143. android:layout_weight="0.8"
  144. android:padding="10dp"
  145. android:textSize="20dp"
  146. android:textColor="#FFF"/>
  147.  
  148. <ImageView
  149. android:layout_width="wrap_content"
  150. android:layout_height="wrap_content"
  151. android:layout_gravity="center_vertical|right"
  152. android:layout_marginRight="6dp"
  153. android:src="@drawable/arrow_icon" />
  154. </TableRow>
  155. </TableLayout>
  156. </LinearLayout>
  157.  
  158.  
  159.  
  160. This is my code .. please help me out why list is coming as null??
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement