I have a ListView with a custom adapter. It countains CheckBoxes. When one of them is checked, I would like to have the others unchecked. I tried creating a CheckBox list, and then created a method to return this list, but it kept giving IndexOutOfBounds (I added the CheckBoxes to the list at the getView method).
This is my adapter.
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox android:id="#+id/checkBoxListView"
android:layout_height="50px"
android:layout_width="50px"/>
<EditText android:id="#+id/editTxtListView"
android:layout_height="50px"
android:layout_width="680px"
android:editable="false"
android:focusable="false" />
why not have a global integer representing the checked position and in the getview method see if the global variable's value and the position, for which getView is called, are same then make check box checked else unchecked.Saw your code in the pastebin.You are making it quite a complex thing.Anyway, just a quick comment, Dont do this.pos=position;in the getView().Instead of this check if(postion==this.pos){//Check this checkbox}else{//uncheck this check box}Also, "pos" should be controlled by the activity or the pojo itself handling the data for the list.
Related
In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}
I have created an android project in which i have a list view with 6 items.
I want that every time I select any item from the list, it gets a colour orange which stays until I press button submit.
the code is:
<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.SafeWalkApp.SecondActivity" >
<ListView
android:id="#+id/sampleListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:entries="#array/vol_list" >
</ListView>
<Button
android:id="#+id/onsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/submit" />
</RelativeLayout>
this is my xml code.
I have tried adding the color to color.xml but that is showing me an error message and basically it is for the backgroung. So please help me out on this.
Well if you are using a custom listview and had an item set by custom adapter then you can change the color of the listview item in
nList = (ListView) findViewById(android.R.id.yourlistname);
nList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
In above function nList is the list in which you want to make change and in its on click listner you can get the selected item view as in its parameters as View arg1 and from that you can get the linear layout and its all other controls and make changes to any control color, text etc as you want
get the view in here and set any color you want by getting the view and setting its background etc.
I can not write the whole code here but I hope you get my?
You must create state drawable colors. And some xml.files to define different colors depending on the state of your row.
Maybe this can help you.
How to change color of ListView items on focus and on click
Android LinearLayout with color resource: What am I doing wrong?
Make a class that contains all the info that is needed for your adapter item + a boolean variable, that you are going to check in adapters getView, if the variable is going to be true, change the color to orange else normal color.
in onItemClick find the position of the correct object in the list that you gave to your adapter and change its boolean value.
after that refresh the adapters referenced list.
Call notifyDataSetChanged() on your adapter. Now according to point(1.) the adapter is going to check the boolean value of the item and change its color to orange.
So I've googled, and scoured SO for an answer to what I think is probably a ridiculous oversight, but here goes.
I have a ListView that I'm populating with an ArrayAdapter that I'm building out of a list of objects I'm using elsewhere in my application. I have checked via getCount that there are items in the adapter, both before and after I call .setAdapter(). Nothing is showing up in my application however.
My main layout res/layout/playlistview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playlist_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#80000000"
android:gravity="center"
android:orientation="horizontal" >
<ListView
android:id="#+id/playlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#206"
android:background="#602" >
</ListView>
</LinearLayout>
(I set the colors so I could see what's going on more easily)
the textview for each item res/layout/singlelistitem:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/single_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="5dp"
android:background="#206">
</TextView>
and the code I use to populate it:
private ListView playlistView;
private void buildPlaylistView() {
playlistView = (ListView)findViewById(R.id.playlistview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlelistitem, R.id.single_item, playlist.titlesAsArray());
playlistView.setAdapter(adapter);
playlistView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
((ArrayAdapter<String>)playlistView.getAdapter()).notifyDataSetChanged();
}
playlist.titlesAsArray() does return String[] and it works.
I have both of the .notifyDataSetChanged() in there, since I found that on SO and gave it a try.
When I change the android:layout_height in the ListView object in my XML to wrap_content, I see only the opaque background that is in the LinearLayout that wraps it. When I set the ListView android:layout_height to match_parent, the whole screen is #206 (magentish).
When i check getCount() on the adapter before setAdapter() it says there are items. When I check it after, from the view itself, it says there are the same number of items. I'm totally lost on this one, but nothing is displayed.
Try changing from android:orientation="horizontal" to android:orientation="vertical" this may fix.
I'm tearing my hair out over this. I would like a ListView where each item in the list is an ImageView. I want each item to have a radio button next to it and only one item can be selected at a time (i.e. single choice mode). This code works fine for creating a list of text box with radio buttons:
ListView listView = ...
String [] value = {"test1","test2"};
listView.setAdapter(
new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_single_choice,value
));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
For my ImageView items, I implemented by own ArrayAdapter. In the getView method of my ArrayAdapter object, I load the follow xml file for each list row:
e.g.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="center"
>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
<ImageView android:id="#+id/preview"
android:layout_width="75dip" android:layout_height="100dip"
android:scaleType="fitCenter"
android:enabled="true"
android:focusable="false"
/>
</RelativeLayout>
I copied the CheckedTextView item from the simple_list_item_single_choice.xml file used in the previous example. I assume that ListView must look for an item with id "text1" to use for displaying the selection status (I'm just guessing here as I cannot work out where the radio buttons are meant to come from). If I don't include the CheckedTextView item, I don't see any radio buttons at all.
When I use my custom adapter with the above xml file, I can see the radio buttons but selecting them does nothing. In addition, I've implemented the selection listener for the list view and this do not fire when I click items. What am I doing wrong?
ListView
[Text] [Button 1][Button 2]
[Text] [Button 1][Button 2]
... (and so on) ...
Button 1 should be like Check Box and other button which allows to redirect on other page
Is above mentioned thing possible??
Thanks in advance..
Each row in a list view can have anything your heart desires. A row is basically defined as its own layout, and you can define the layout as you wish. In your case, you probably want to do a RelativeLayout. Check out the examples, they come pretty close to what you want.
You can have your ListView row to contain a CheckedTextView and a Button ina Linear Layout.
In \layout\ folder, define your customised my_row.xml, roughly so
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Then for populating your ListView, give this in your Adapter.
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.my_row, strings);
myList.setAdapter(myAdapter);