I have an app that retrieves data from a webservice in the form of an array. In the getView() of the adapter i set the TextViews to the elements in the array. The array is called recordItem and has various elements one of which is "status". Status is a String that can be either completed, ncr, or waiting. if i set the TextView in the listview directly with the value completed, ncr or waiting there's no problem.
I don't want to display the full string in the textview but rather display a C, NCR or W instead. I have a series of "if statements" that check the array element and then sets the textview to the respective character.
The problem is that no character is being displayed. why?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent,
false);
TextView startTime = (TextView) rowView
.findViewById(R.id.rowstarttime);
TextView duration = (TextView) rowView
.findViewById(R.id.rowduration);
TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
String record = list.get(position).toString();
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
Log.e(TAG, "x = " + x);
}
String withoutBraket = recordItem[0].substring(11);
String withoutSecs = withoutBraket.substring(0, 6);
Log.e(TAG, "recordItem = " + recordItem[2]);
if(recordItem[2].toString().equalsIgnoreCase("Completed")){
statusField = "c";
}else if(recordItem[2].toString().equalsIgnoreCase("NCR")){
statusField = "NCR";
}else if(recordItem[2].toString().equalsIgnoreCase("Waiting")){
statusField = "W";
}
Log.e(TAG, "statusField = " + statusField);
startTime.setText(withoutSecs );
duration.setText( recordItem[1]);
status.setText( statusField);
name.setText( recordItem[3] + recordItem[4]);
callID = recordItem[5];
needName = recordItem[6];
return rowView;
}
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp" >
<TextView
android:id="#+id/rowstarttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/rowduration"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp" >
<TextView
android:id="#+id/rowstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/rowclientname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
recordItem[2] needed trim() calling on it. .equals not picking the strings up because of this.
Related
when writing an app for me and my roommates to calculate our food-payments I got stuck on such problem:
when adding new payment, you:
1) select name from RadioGroup
2) give value
3) give description (or leave default 'opis')
then after pressing button "DODAJ" it shows as new item in ListView with certain format. Now the problem begins:
I want to make it so when I click on a Item in this list, i will slightly change format of this certain item (in NowaWplata class I got extra time to be displayed) which I got in another .xml layout file, and when pressing it again it should go back to 'normal' layout.
I tried 2 different approaches, one is commented in the bottom of my code (I tried it from this topic Change layout of selected list item in Android), and the other one is in overriden onItemClick method - neither of them worked
compiler says that the error is :
java.lang.NullPointerException
at com.example.lukasz.dom.MainActivity.onItemClick
in this line of code:
relativeLayoutInflate.addView(child);
in main activity (onClick only for 'lukasz', same for 'marcelina' and 'karolina'):
#Override
public void onClick(View v) {
if (v == addNewPaymentButton) {
//checking which button from RadioGroup is checked and adding new payment to certain person
int checkedRadioButtonId = members.getCheckedRadioButtonId();
switch (checkedRadioButtonId) {
//if Lukasz button is pressed, do:
case R.id.lukaszRadioButton:
if (lukaszRadioButton.isChecked()) {
//add value from addNewPaymentButton to tempLukasz
try {
tempLukasz += valueOf(newPaymentValue.getText().toString());
} catch (NumberFormatException e) {
}
//seting new value to money spent by lukasz
String sumaLukasza = getString(R.string.money_spent_by_lukasz);
sumaLukasza = String.format(sumaLukasza, tempLukasz);
moneySpentByLukasz.setText(sumaLukasza);
//adding new payment to list of all payments with flag 'int == 1' to set color to RED
NowaWplata newPayment = new NowaWplata(lukaszRadioButton.getText().toString(), newPaymentValue.getText().toString(), descriptionOfNewPayment.getText().toString(), 1);
NowaWplata.setWplaty(newPayment);
listView.setAdapter(new NewPaymentAdapter(this, R.layout.new_list_item_layout, NowaWplata.getWplaty()));
}
break;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView hourDate = (TextView) view.findViewById(R.id.hoursDateFieldInSelectedListItem);
TextView dayDate = (TextView) view.findViewById(R.id.daysDateFieldInSelectedListItem);
TextView name = (TextView) view.findViewById(R.id.nameFieldInSelectedListItem);
TextView value = (TextView) view.findViewById(R.id.valueFieldInSelectedListItem);
TextView description = (TextView) view.findViewById(R.id.descriptionFieldInSelectedListItem);
RelativeLayout relativeLayoutInflate = (RelativeLayout) view.findViewById(R.id.layoutOfSelectedListItem);
NowaWplata newPayment = (NowaWplata) listView.getItemAtPosition(position);
View child = getLayoutInflater().inflate(R.layout.selected_list_item, null);
relativeLayoutInflate.addView(child);
hourDate.setText("[" + newPayment.getHourDate() + "]");
dayDate.setText(newPayment.getDayDate());
name.setText(newPayment.getOsoba());
value.setText(newPayment.getWplata() + "zł");
description.setText(newPayment.getOpis());
}
}
//custom adapter class
class NewPaymentAdapter extends ArrayAdapter<NowaWplata> {
public LayoutInflater layoutInflater;
//custom adapter's constructor with values
public NewPaymentAdapter(Context context, int textViewResourceId, List<NowaWplata> wplaty) {
super(context, textViewResourceId, wplaty);
layoutInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
NowaWplata newPayment = getItem(position);
Holder holder = null;
// private int position;
// public void selectedItem(int position)
// {
// this.position = position;
// }
//if there are no items in list - add 1st item
if (view == null) {
view = layoutInflater.inflate(R.layout.new_list_item_layout, null);
TextView name = (TextView) view.findViewById(R.id.osobaWplata);
TextView value = (TextView) view.findViewById(R.id.kwotaWplata);
TextView description = (TextView) view.findViewById(R.id.opisWplata);
holder = new Holder(name, value, description);
view.setTag(holder);
}
// if there are items in list get holder tag
else {
holder = (Holder) view.getTag();
}
//setting text to a new list item
holder.osoba.setText("[" + newPayment.getDate() + "] " + newPayment.getOsoba());
holder.kwota.setText(newPayment.getWplata() + "zł");
holder.opis.setText(newPayment.getOpis());
//setting different color to Lukasz/Marcelina/Karolina text
if (newPayment.getFlag() == 1) {
holder.osoba.setTextColor(Color.RED);
} else if (newPayment.getFlag() == 2) {
holder.osoba.setTextColor(Color.BLUE);
} else if (newPayment.getFlag() == 3) {
holder.osoba.setTextColor(Color.GREEN);
}
//setting color to payment's value
holder.kwota.setTextColor(Color.BLACK);
// if (this.position == position) {
// View view2;
// view2 = layoutInflater.inflate(R.layout.selected_list_item, null);
//
// TextView hourDate = (TextView) view2.findViewById(R.id.hoursDateFieldInSelectedListItem);
// TextView dayDate = (TextView) view2.findViewById(R.id.daysDateFieldInSelectedListItem);
// TextView name = (TextView) view2.findViewById(R.id.nameFieldInSelectedListItem);
// TextView value = (TextView) view2.findViewById(R.id.valueFieldInSelectedListItem);
// TextView description = (TextView) view2.findViewById(R.id.descriptionFieldInSelectedListItem);
//
// hourDate.setText("[" + newPayment.getHourDate() + "]");
// dayDate.setText(newPayment.getDayDate());
// name.setText(newPayment.getOsoba());
// value.setText(newPayment.getWplata() + "zł");
// description.setText(newPayment.getOpis());
//
// return view2;
// }
return view;
}
}
this is the main XML part with the ListView
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_below="#+id/tableLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/newPaymentValue"
android:layout_alignRight="#+id/tableLayout"
android:layout_alignEnd="#+id/tableLayout"
android:choiceMode="none"
android:clickable="false"
android:padding="20dp"
android:transcriptMode="disabled"/>
this is the xml of a new item in list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutOfSelectedListItem">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/hoursDateFieldInSelectedListItem"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/daysDateFieldInSelectedListItem"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/nameFieldInSelectedListItem"
android:layout_below="#+id/hoursDateFieldInSelectedListItem"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/daysDateFieldInSelectedListItem"
android:layout_toStartOf="#+id/daysDateFieldInSelectedListItem"
android:textSize="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/valueFieldInSelectedListItem"
android:layout_below="#+id/daysDateFieldInSelectedListItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/descriptionFieldInSelectedListItem"
android:layout_below="#+id/nameFieldInSelectedListItem"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="14dp" />
I have done that by adding extra layout.
ListItem:
<--Your listItem view--!>
<RelativeLayout>
<--Your extra View--!>
<LinearLayout
android:tag="0"
android:visibility="gone">
</LinearLayout>
</RelativeLayout>
In your OnItemClickListener set the extra layout to visible and also set the tag accordingly to save the state of the extra view.
Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
PlannerMonth m = getItem(position);
ArrayList<PlannerMonthDebt> debts = new ArrayList<PlannerMonthDebt>();
debts = m.debtList;
int debtSize = debts.size();
if (rowView == null) {
rowView = inflater.inflate(R.layout.row_planner_month, null, true);
holder = new ViewHolder();
holder.tv1 = (TextView) rowView.findViewById(R.id.tvPlannerMonth);
holder.tv2 = (TextView)
holder.ll = (LinearLayout) rowView.findViewById(R.id.rlInnerDebtHolder);
for (int i = 0; i < debtSize; i++) {
View custom = inflater.inflate(R.layout.inner_debt_row, null);
TextView tvName = (TextView) custom.findViewById(R.id.tvInnerDebtName);
tvName.setTag(String.valueOf("tvName" + i));
TextView tvPayment = (TextView) custom.findViewById(R.id.tvInnerDebtPayment);
tvPayment.setTag(String.valueOf("tvPayment" + i));
TextView tvDebt = (TextView) custom.findViewById(R.id.tvInnerDebtBalance);
tvDebt.setTag(String.valueOf("tvDebt" + i));
TextView tvBalance = (TextView) custom.findViewById(R.id.tvInnerDebtFee);
tvBalance.setTag(String.valueOf("tvBalance" + i));
holder.ll.addView(custom);
}
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
String month = m.date;
String debtToal = m.debtTotal;
String intTotal = m.intTotal;
holder.tv1.setText(month);
holder.tv2.setText(debtToal);
// now inner debts
int size = 0;
for (PlannerMonthDebt d : debts) {
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
tvName.setText(d.name);
TextView tvPayment = (TextView) convertView.findViewWithTag(String.valueOf("tvPayment" + size));
tvPayment.setText(d.payment);
TextView tvDebt = (TextView) convertView.findViewWithTag(String.valueOf("tvDebt" + size));
tvDebt.setText(d.balance);
TextView tvBalance = (TextView) convertView.findViewWithTag(String.valueOf("tvBalance" + size));
tvBalance.setText(d.fees);
size++;
}
return rowView;
}
Here is the row I am trying to dynamically add:
inner_debt_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvInnerDebtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtFee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="TextView" />
<TextView
android:id="#+id/tvInnerDebtBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
I have a LinearLayout (holder.ll) where I add an X number of custom rows (based on the inner_debt_row layout pasted above).
I know how to show the rows, but I am having trouble using setTag() to find and retrieve them so I can put values to them (via setText().
For the tag, I basically use a same name for each TextView, but ALSO add a unique position to the name. I use the position (or size) to keep each of the dynamically added text fields unique for searching.
In my LogCat, I get a NullPointer on this line:
TextView tvName = (TextView) convertView.findViewWithTag(String.valueOf("tvName" + size));
This is the first encounter with assigning a value to a dynamic TextView so I assume they all can not be found.
How can I locate the correct TextView instance whit by tag?
The findViewWithTag() code looks correct. Did you check that convertView isn't null?
convertView will be null when creating a new row (i.e. not reusing a previous one), but you are assigning rowView only, and not convertView.
You have different debtSize in each position. When convertView != null and have less debtSize you have NullPointer's, when more - you have empty textviews.
This is Relative Layout view which is created dynamically like this :
...
So my question is how to get the values from those dynamic elements and perform some calculation to show result on textview which is also dynamically created.
Thanks in advance guys!!
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
}
}
}
for (int i = 0; i < maalContainer.getChildCount(); i++) {
View view = maalContainer.getChildAt(i);
EditText ed_item = (EditText) view
.findViewById(R.id.edittext1);
EditText ed_value = (EditText) view
.findViewById(R.id.edittext2);
EditText ed_value1 = (EditText) view
.findViewById(R.id.edittext3);
ed_item.getText().toString().trim();
ed_value.getText().toString().trim();
ed_value1.getText().toString().trim();
}
No need to assign/get ID of any View but the best way is to iterate through the child views of LinearLayout:
int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = myLinearLayout.getChildAt(i);
// do whatever you would want to do with this View
}
A few things here:
You shouldn't be using the Application Context to get the LayoutInflater. You should get that from the Activity Context, otherwise your themes and styles will not be respected.
You shouldn't inflate with null as the second parameter (except in special instances where you don't know the View's parent). Just put the android:layout_width="match_parent" and android:layout_height="wrap_content" attributes in player_entry_item.xml and they will be respected upon inflation.
Store the EditText objects upon inflation as a private array.
With that said, my suggested revision:
private EditText[] mEditTextPlayers;
public void goButtonClicked(View view) {
maalContainer.removeAllViews();
int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());
LayoutInflater inflater = LayoutInflater.from(view.getContext());
mEditTextPlayers = new EditText[numPlayers];
for (int i = 0; i < numPlayers; i++) {
//Pass the parent as the second parameter to retain layout attributes
mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
maalContainer.addView(dynamicEntryView);
}
}
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
You can do it by using getId() and setId() methods of the View.
dynamicEntryView.setId(i);
and access the view as below by getting the
dynamicEntryView.getId(i);
<?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" >
<TextView
android:id="#+id/player_name_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:text="Player name"
android:textColor="#202020"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/player_item_edittext_point"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Points"
android:inputType="number"
android:textColor="#202020" />
<EditText
android:id="#+id/player_item_edittext_maal"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Maal"
android:inputType="number"
android:textColor="#202020" />
<TextView
android:id="#+id/player_item_textview_result"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0.00$"
android:textSize="22sp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
Only one id is used. It will loop through according to number of players. Hope it will help you. #user2731584
I have a ListView and a SimpleCursorAdapter. I add two rows (each is a reminder): the first has a title, date and countdown string, the second has a title and an address (location).
For some reason, the ViewHolder's mCountDownString from row #2 has the value from row #1.
Doesn't it create a ViewHolder object for each row? If yes, why isn't mCountDownString empty for the address reminder row?
private static class ViewHolder {
TextView mTitle;
TextView mDate;
String mDateString;
TextView mCountDown = null;
String mCountDownString;
TextView mAddress;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
mCursor = (Cursor) getItem(position);
if(convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, parent, false);
holder.mTitle = (TextView) convertView.findViewById(R.id.titleID);
//holder.mAddress = (TextView) convertView.findViewById(R.id.dateTimeOrLocationID);
//holder.mDate = (TextView) convertView.findViewById(R.id.dateTimeOrLocationID);
// edit: changed to use a separate view
holder.mAddress = (TextView) convertView.findViewById(R.id.addressId);
holder.mDate = (TextView) convertView.findViewById(R.id.dateTimeId);
holder.mCountDown = (TextView) convertView.findViewById(R.id.countdownID);
holder.mCountDownString = null;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
int colorPos = mCursor.getPosition() % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
int col = mCursor.getColumnIndex(ReminderColumns.TITLE);
holder.mTitle.setText(mCursor.getString(col));
col = mCursor.getColumnIndex(ReminderColumns.ADDRESS);
String address = mCursor.getString(col);
if(address != null)
{
// No. 1: it's an address: set only title + address fields
System.out.println("title for address: " + holder.mTitle.getText());
holder.mAddress.setText(address);
holder.mDate.setText(""); // edit
holder.mCountDown.setText("");
holder.mCountDownString = "";
}
else
{
// No. 2: it's date: set only title + date + countDownString fields
col = mCursor.getColumnIndex(ReminderColumns.DATE);
Date date = new Date(mCursor.getLong(col));
SimpleDateFormat timeFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm");
String dateStr = timeFormat2.format(date);
holder.mDate.setText(dateStr);
String countDownStr = getTimeDiff(date);
holder.mCountDown.setText(countDownStr);
holder.mCountDownString = countDownStr;
holder.mAddress.setText(""); // edit
}
return convertView;
}
EDIT
I clear all holder's member variables if they're not used. But the problem still persists: for a date row, it doesn't display holder.mDate and for a address row, it doesn't show holder.mAddress. Something is mixed up.
When I create the adapter, the cursor is initially null. That's why I'm setting it in getView().
Edit 2
I changed my code so I'm not using dateTimeOrLocationID for holder.mAddress and holder.mDate. The problem now is, that there's an empty view which takes up space. F.e. if I add a date and leave R.id.addressId empty, the empty space is still visible. Is there a way to avoid that? And would you mind explaning why my first approach didn't work?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="false"
android:clickable="false"
android:descendantFocusability="afterDescendants">
<TextView
android:id="#+id/titleID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/dateTimeId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/addressId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/countdownID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="#color/black" />
</LinearLayout>
No it doesn't create a viewholder for each row in your list. You got 1 viewholder for each convertview which is about as many rows that can be shown at the screen simoultanously. You need to update the content each time getView is called.
Edit:
Your mDate and mAddress use the same view. Search for: R.id.dateTimeOrLocationID abd it becommes clear to you :)
I'm trying to center two textViews that are in a LinearLayout. This LinearLayout is nested in another one, with a ListView-element.
I think my XML is pretty correct. I fill my textViews dynamically in my Adapterclass.
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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacaturesnummer"
android:layout_width="wrap_content"
android:textColor="#color/Accent"
android:text="x"
/>
<TextView android:layout_height="wrap_content"
android:id="#+id/atlVacatures"
android:layout_width="wrap_content"
android:text="y"
/>
</LinearLayout>
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView android:layout_height="fill_parent"
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:text="Er zijn geen jobs die voldoen aan uw criteria..."
android:gravity="center"/>
</LinearLayout>
Adapterclass:
/*
* Klasse VacatureAdapter
*/
private class VacatureAdapter extends ArrayAdapter<Vacature>{
private ArrayList<Vacature> vacatures;
public VacatureAdapter(Context context, int textViewResourceId, ArrayList<Vacature> vacatures){
super(context, textViewResourceId, vacatures);
this.vacatures = getArray();
//System.out.println("Array vacatureadapter: " + v);
}
#Override
public View getView(int position, View convertview, ViewGroup parent){
View view = convertview;
if(view==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.vacature_list_item, null);
//view.setBackgroundColor((position % 2) == 1? Color.LTGRAY: Color.WHITE);
}
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
Vacature vaca = vacatures.get(position);
if(vaca != null){
TextView tvNaam = (TextView) view.findViewById(R.id.vacatureNaam);
TextView tvWerkveld = (TextView) view.findViewById(R.id.vacatureWerkveld);
TextView tvRegio = (TextView) view.findViewById(R.id.vacatureRegio);
if(tvNaam != null){
tvNaam.setText(vaca.getTitel());
if(tvWerkveld != null){
tvWerkveld.setText("Werkveld: " + vaca.getWerkveld());
if(tvRegio!=null){
tvRegio.setText("Regio: "+vaca.getRegio());
}
}
}
}
return view;
}
}
The weird thing is that if my spinner runs, he shows the texts set in my XML correctly, if the spinner stops and fills my ListView it only shows one digit of my number and when I scroll once he shows my number completly plus the second TextView. I don't quite understand what's wrong, maybe some code needs te be put somewhere else?
I've solved my problem by putting my setText code in my Runnable method, after I dismiss my Dialog. Now It looks like this:
private Runnable returnRes = new Runnable(){
public void run(){
if (arrVacatures.size() == 0){
dialog.dismiss();
}
if(arrVacatures!=null && arrVacatures.size() > 0){
adapter.notifyDataSetChanged();
for(int i= 0; i< arrVacatures.size();i++){
adapter.add(arrVacatures.get(i));
}
dialog.dismiss();
TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
atlVacaturesnr.setText("" + arrVacatures.size());
atlVacatures.setText(" jobs op maat gevonden!");
adapter.notifyDataSetChanged();
}
}
};