I'm developing with android studio.
I have a ListView that the elements in it are aligned from left to right and I want to align them from right to left, how can I do so?
the java class is:
public class ProductList extends ListActivity {
ArrayList<String> list = new ArrayList<String>(Arrays.asList("במבה אוסם", "בשר טחון", "קוקה קולה שישייה", "קפה עלית", "שמיר", "מילקי","רבעיית דניאלה"));
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_productlist);
Button btnDel = (Button) findViewById(R.id.btnDel);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
/** Defining a click event listener for the button "Delete" */
View.OnClickListener listenerDel = new View.OnClickListener() {
#Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the delete button */
btnDel.setOnClickListener(listenerDel);
/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
public void StartCalck(View view){
Intent intent = new Intent(ProductList.this, SplitBuying.class);
startActivity(intent);
}
}
and the layout is:
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_below="#+id/ChooseStore">
</ListView>
</LinearLayout>
Thank you !
Use RecyclerView instead of ListView.
LinearLayoutManager(Context context, int orientation, boolean reverseLayout)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" ><br>
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/><br>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_toLeftOf="#+id/textView_name"/>
</RelativeLayout>
Manifest.xml
<application
....
android:supportsRtl="true"
...
</application>
you should use custom layout like
myTextView.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="right"
android:background="#bdbdbd"/>
pass this layout to adapter like
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.nameOfLayout, list);
Related
I have a listView in my activity_main.xml . I used a layout(list_layout) for my listview's row. list_layout contains 3 textView. I added a activity called "Setting" into my Mainactivity. I want change visibility of list_layout's 3. textView from settin.java with a button.
I mean when I click button (button code is into setting.java(button is into activity_setting.xml)) list_layout's 3.textview must invisible.
This is from activity_main.xml
<ListView
android:id="#+id/listem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
This is list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
.../>
<TextView
.../>
<TextView
android:id="#+id/turkish_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:visibility="visible"/>
</LinearLayout>
MainActivity.Java
... listview = (ListView) findViewById(R.id.listem);
DataHelper.Database data = new DataHelper.Database(MainActivity.this);
ArrayList<HashMap<String, String>> Liste = data.Listele();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, Liste, R.layout.list_layout, new String[]{"id", "title", "subtitle"}, new int[]{R.id.kelime_id, R.id.english_id, R.id.turkish_id});
listview.setAdapter(adapter);
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, Setting.class);
startActivity(intent);
break; ...
//Setting.Java
public class Setting extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
public void click(View view) {//<-----Here is my button's code
textView=(TextView)view.findViewById(R.id.turkish_id);
textView.setVisibility(View.INVISIBLE);
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
There are different methods for adding a click listener to a button , refer to this link:
add onclick listener to predefined button?
in your case you could implements in your activity the interface OnClickListener
public class Setting extends AppCompatActivity implements OnClickListener
then you should rename your method click to onClick
and in onCreate of your activity you should add the line
findViewById(R.id.yourIdButton).setOnClickListener(this);
don't forget to give an id to your button
<Button
android:"#+id/yourIdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
you can also remove completely the TextView from layout using "View.GONE" in place of "View.INVISIBLE"
I am displaying a Listview of recorded data and when the user clicks on a specific list item, it triggers an intent and brings the user to the profile for that selected user. I worked when i first ran my project but ever since it just will not work. Thank you in advance.
public class CowListView extends ListActivity {
RegisterCowAdapter cowAdapter;
private DataBaseHelper databaseHelper;
public ListView listView;
TextView student_Id;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cow_list_item);
cowAdapter = new RegisterCowAdapter(this);
cowAdapter.open();
updateCowUI();
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
student_Id = (TextView) view.findViewById(R.id.cowtagnolist);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), CowProfile.class);
Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
objIndent.putExtra("student_Id", Integer.parseInt(studentId));
startActivity(objIndent);
}
});
}
private void updateCowUI() {
Cursor cursor = cowAdapter.getAllCowData();
startManagingCursor(cursor);
String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
SimpleCursorAdapter reminders = new SimpleCursorAdapter(
this,
R.layout.cow_list_item,
cursor,
from,
to
);
this.setListAdapter(reminders);
}
}
private void updateCowUI() {
Cursor cursor =cowAdapter.getAllCowData();
startManagingCursor(cursor);
String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
SimpleCursorAdapter reminders = new SimpleCursorAdapter(
this,
R.layout.cow_list_item,
cursor,
from,
to
);
this.setListAdapter(reminders);
}
}
My XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:longClickable="true">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#+id/cowtagnolist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
>
</TextView>
<TextView
android:id="#+id/cowdoblist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
android:textColor="#color/black">
</TextView>
</LinearLayout>
Might be one of two things.
You have implemented OnClickListener but you have not set the ListView to listen to that specific class nor have you called any method in your onCreate that will set the ListView's listener to that class.Secondly I think you might need to implement OnItemClickListener and not onCLickListener since thats what is on the ListView, an Item.Im confused though why do you have onClick and then you set the listener inside this method? How will it know in the first place that the view has been clicked if it is not listening from the beginning i.e. in the onCreate or a method called in the onCreate ? Your logic seems abit off
This is my first app. I had been setting a preference in an activity (with a fragment), but I want to move the fragment to a DialogPreference. It migrated okay, except my text colors have gone weird. See picture. The text should just be black, not white and gray. Any idea what I got wrong?
(Sorry, I know this is pretty sloppy code)
How it looks now:
My xml...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relLay_dialog_semester_root"
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" >
<TextView
android:id="#+id/select_semester_prompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/select_semester_prompt" />
<LinearLayout
android:id="#+id/linLayGradeSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/select_semester_prompt"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" >
<Spinner
android:id="#+id/semester_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/hint_editSemester" />
<Spinner
android:id="#+id/year_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<TextView
android:id="#+id/active_semesters_str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linLayGradeSum"
android:layout_below="#+id/linLayGradeSum"
android:text="#string/active_semester_str"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="#+id/active_semesters_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/active_semesters_str"
android:layout_below="#+id/active_semesters_str"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
My extended DialogPreference class
public class SemesterDialogPreference extends DialogPreference {
SharedPreferences settings;
public static int YEAR_DEFAULT_VALUE = Activity_AddCourse.thisYear - Activity_AddCourse.baseYear;
public static int SEASON_DEFAULT_VALUE = Semester.getDefaultSemester().getSeasonInt();
public static int SEMESTER_DEFAULT_ID = 1;
String semester_id_key;
DBAdapter db;
// this is where I do everything view-related
#Override
public void onBindDialogView(View view){
settings = getSharedPreferences();
int curSemesterId = settings.getInt(semester_id_key, 1);
db = new DBAdapter(this.getContext().getApplicationContext());
db.open();
Semester curSemester = db.getSemester(curSemesterId);
db.close();
ArrayList<String> years = new ArrayList<String>();
for (int i = Activity_AddCourse.baseYear; i <= Activity_AddCourse.maxYear; i++)
{
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter_year = new ArrayAdapter<String>(this.getContext().getApplicationContext(),
android.R.layout.simple_spinner_item, years);
Spinner spinYear = (Spinner)view.findViewById(R.id.year_spinner);
adapter_year.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinYear.setAdapter(adapter_year);
spinYear.setSelection(curSemester.getYear() - Activity_AddCourse.baseYear);
Spinner spinner = (Spinner) view.findViewById(R.id.semester_spinner);
ArrayAdapter<CharSequence> adapter_spinner = ArrayAdapter.createFromResource(this.getContext().getApplicationContext(),
R.array.semester_spinner, android.R.layout.simple_spinner_item);
adapter_spinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_spinner);
spinner.setSelection(Semester.seasonStringToInt(curSemester.getSeason()));
db = new DBAdapter(this.getContext().getApplicationContext());
db.open();
Semester[] s = db.getAllSemesters();
db.close();
String activeSem;
LinearLayout semesterList = (LinearLayout) view.findViewById(R.id.active_semesters_list);
if(s != null){
for(int i = 0; i < s.length; i++){
activeSem = s[i].getSemesterString();
TextView tv = (TextView) new TextView(this.getContext().getApplicationContext());
tv.setText(activeSem);
semesterList.addView(tv);
System.out.println(s[i].getSemesterString());
}
}
super.onBindDialogView(view);
}
public SemesterDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_set_semester);
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
persistBoolean(positiveResult);
}
}
UPDATE:
The DialogPreference pops up from my settings activity, as shown below.
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Also, I was able to get the spinner text to black by making my own xml layouts for them, just as Huy Tran suggested. However, I had to make one for each the spinner_item and the spinner_dropdown_item. See below.
ArrayAdapter<String> adapter_year = new ArrayAdapter<String>(this.getContext().getApplicationContext(),
R.layout.my_spinner_item, years);
Spinner spinYear = (Spinner)view.findViewById(R.id.year_spinner);
adapter_year.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
Why did I have to do this? I don't know. Also, dropdown items are each now about 50% taller. I copied exactly from source code into my own xml and it still renders differently. Very weird.
Create a layout like below and use that layout in setDropDownViewResource instead of android.R.layout.simple_spinner_dropdown_item.
Create my_spinner_dropdown_item.xml in res/layout:
EDITED:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textColor="#color/black"
/>
my_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
Try using margin to change the size of each item.
And then in your SemesterDialogPreference:
setDropDownViewResource(R.layout.my_spinner_dropdown_item);
I need to output data from lines cm1 and cm2 in a ListView, every time when I press the button. Line should be shown in various TextView.
Code TEST.java:
public String cm1;
public String cm2;
public class TEST extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyList = (ListView) this.findViewById(R.id.listStrings);
setListAdapter();
// button
Button setup = (Button) this.findViewById(R.id.send);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cm1 = "cm1text";
cm2 = "cm2text";
//xxxx
}
});
}
// adapter
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row //idknow what im must writehere to set cm1 string to cm1text tv
List.setAdapter(adapter);
}
Code TEST.xml:
<?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="vertical" >
<ListView
android:id="#+id/listStrings"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
Code row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/cm1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/cm2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Should look ListView after 2 clicks:
cm1
cm2
cm1
cm2
Maybe what you should/can do is have this;
String cm = cm1 + "\n" + cm2
Which should output;
cm1
cm2
Then you change the xml to just have one textview and the adapter code as follows;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.textViewCM, listItems);
yourListView.setAdapter(aa);
Then, whenever you press the button;
// ...code to initialise/change cm
listItems.add(cm);
aa.notifyDataSetChanged(); // notifies listView to update it's view
That should do what you're after unless you've got to have the two individual textViews, in which case, you'll need to write a custom adapter, I've done a bit of a tutorial on my website if you follow that link.
For some reason setCurrentTab is not switching tabs after the first time.
This is the code that I use.
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
}
};
It is connected to buttons each of which have a tag that equals the number of of tab to be shown.
When the button is clicked the first time and this method is called then the tab shows up. I can also see that it executes the code that creates the tab contents.
However, once a tab has been shown once and I have moved to another one, clicking back to it doesn't work.
The method is definitely called and the tag is also correct. I put in commands to print to the log to confirm that. Also, it works first time round so must be okay.
Any idea?
Full code:
public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTabs();
}
#Override
protected void initTabs() {
addTab("0",this);
addTab("1",this);
addTab("2",this);
addTab("3",this);
}
/*
*
* #param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
* #return An array containing all of the objects in the list.
*/
protected Serializable[] getDataArray(Bundle extras) {
int size = extras.size();
Serializable[] data = new Serializable[size];
for (int i = 0; i < size; i++) {
data[i] = extras.getSerializable(Integer.toString(i));
}
return data;
}
#Override
public View createTabContent(String tag) {
// Get the data returned from the servelet and display it in the ListView
Log.d("TestTabs","createTabContent");
ListView lv = (ListView) findViewById(R.id.list_view);
List<String> list1Strings = new ArrayList<String>();
switch (Integer.parseInt(tag)){
case 0:
Log.d("TestTabs","Case 0");
lv.setAdapter(new MyAdapter(this,lv,null));
break;
case 1:
Log.d("TestTabs","Case 1");
list1Strings.add("Item 21");
list1Strings.add("Item 22");
list1Strings.add("Item 23");
list1Strings.add("Item 24");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 2:
Log.d("TestTabs","Case 2");
list1Strings.add("Item 31");
list1Strings.add("Item 32");
list1Strings.add("Item 33");
list1Strings.add("Item 34");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 3:
Log.d("TestTabs","Case 3");
list1Strings.add("Item 41");
list1Strings.add("Item 42");
list1Strings.add("Item 43");
list1Strings.add("Item 44");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
}
return lv;
}
protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName); // Don't set tab layout since we are going to make it invisible
tabSpec.setContent(tabFactory);
tabHost.addTab(tabSpec);
addButton(tabName);
}
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
Log.d("TabbedScreen","Set tab to " + v.getTag());
View view;
for (int i=0; i< childCount; i++)
if ((view = buttonHolder.getChildAt(i)) != v){
view.setBackgroundResource(R.drawable.button_tab);
view.invalidate();
}
}
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/content_screen_user_details">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<TabWidget android:id="#android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:visibility="gone"/>
<LinearLayout android:id="#+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button3"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="3"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button2"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="2"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button1"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="1"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button0"
android:background="#drawable/tab_selected" android:visibility="invisible"
android:tag="0"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.applicat.meuchedet.views.RoundedListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:dividerHeight="1dip"
android:footerDividersEnabled="true"
android:listSelector="#drawable/listview_selected_item_background"
android:fadingEdge="none"
android:cacheColorHint = "#00000000"
roundedListView:radius="20"
roundedListView:border="2"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
Edit:
I was wondering if the problem is in the row:
ListView lv = (ListView) findViewById(R.id.list_view);
Am I getting back the same object each time?
If so, how do I get a new one each time based on the one defined in the FrameLayout.
How can you be sure that the value assigned to button in your xml is the same you get with only nextChild-- .
not so good solution, you can test it.
void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.getTag() != String.valueOf(nextChild); ERROR;
but It's better to set it to the same as you always get with buttonHolder.getChildAt()
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setTag(String.valueOf(nextChild));
modified:
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
// ----------- modified -----------
button.setTag(String.valueOf(nextChild));
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
// ----------- modified -----------
Button btc = (Button)v;
int idx;
try {
idx = Integer.parseInt((String) btc.getTag());
} catch(NumberFormatException exx) {
System.out.println("Could not parse " + exx);
}
if ( idx < childCount) {
Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
Button butv;
for (int i=0; i< childCount; i++)
if ( i != idx){
butv = (Button) buttonHolder.getChildAt(i);
butv.setBackgroundResource(R.drawable.button_tab);
butv.invalidate();
}
tabHost.setCurrentTab(idx);
tabHost.focusCurrentTab(idx);
}
// ----------- modified -----------
}
};
}
In order to use getTag, you must need to set it first. So I believe you forgot to add the following line in your addButton method:
button.setTag(tabName);
use this
ListView lv = getListView();
instead of
ListView lv = (ListView) findViewById(R.id.list_view);
The getListView() method returns the the list view associated with the 'current' layout.
Also, for this to work you have to change the list view's id in the xml as
<ListView
android:id="#+android:id/list"
may be getTag() creating the problem just store its return value in variable an check if the value is correct (the integer no you actually want)??