Reputation: 1549
I am developing an app for Android using Eclipse.
I have a fragment that has a listview. I want to change the text of one Textview located inside one row of the listview.
public class FragmentShowList extends Fragment {
public FragmentShowList() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_show_list,
container, false);
ListView lv = (ListView) rootView.findViewById(R.id.lv_show);
DBAdapter helper = new DBAdapter(getActivity());
String tableName = Tables.TABLENAME_CATEGORIES_MAIN;
Tables table = new Tables(tableName);
Cursor cursor = helper.getCursor(tableName);
int[] to = new int[] {R.id.row_category_color, R.id.row_category_name};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.row_category, cursor,
table.getColumns(), to, 0);
lv.setAdapter(adapter);
View view = lv.getChildAt(0);
//TextView tv = (TextView) view.findViewById(R.id.row_category_name);
//tv.setText("haha");
return rootView;
}}
I'm able to create and view the list, but when I try to run the two last lines that are with the "//" the app crashes due to a "null pointer exception"
and the xml file for the rows is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/row_category_color"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/rectangle"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/row_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/row_category_color"
android:layout_toRightOf="@+id/row_category_color"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
It seems that I'm not able to access to the children of the listview and I don't know why. Any idea?
Thanks in advance.
Upvotes: 0
Views: 139
Reputation: 10338
The null pointer you are getting is because the listview elements accessible from getChildAt() range from firstvisible item in your listview to its last visible item currently shown in the screen. If you try to log the position variable in your getView() method you will clearly see that it is not called for all your items. As you scroll, the firstVisible item also changes and so does lastvisible item. On scrolling getView() is again called. Refer to [this answer] for more details.1
Upvotes: 1
Reputation: 164
You'll need to subclass CursorAdaptor
and provide a custom implementation for getView(...)
.
getView(...)
of your custom adaptor is the place to change individual list items.
http://developer.android.com/reference/android/widget/CursorAdapter.html
Upvotes: 2