Checkbox in ListView changes when scrolling offscreen - android

I ran into an annoyance, and i couldn’t find an answer anywhere. So here I am.
I have a ListFragment in which I put data retrieved from a database. I’ve extended the SimpleCursorAdapter class and adapted it according to my needs.
My ListView items look like this:
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/txtDetailsListAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Amount"
android:textSize="23dp" />
<TextView
android:id="#+id/txtDetailsListComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Comment"
android:textSize="15dp" />
</LinearLayout>
<CheckBox
android:id="#+id/chkDetailsDebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
The hidden checkbox is displayed once I call startActionMode. The purpose of this checkbox is to be able to select mutliple list items and delete all of them at once.
So deleting works fine when there’s just a couple of entries and no need for scrolling. Yet once i am able to scroll and views are being recycled, the selected list items will change.
I’ve seen solutions but they use ArrayAdapters instead of an extended SimpleCursorAdapter.
Here is my custom SimpleCursorAdapter:
public class CustomDetailsCursorAdapter extends SimpleCursorAdapter {
private DetailsActivity activity;
#SuppressWarnings("deprecation")
public CustomDetailsCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, Activity activity) {
super(context, layout, c, from, to);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.details_custom_list_item, parent,
false);
bindView(v, context, cursor);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
view.setTag(cursor.getString(cursor.getColumnIndex(Persons.COLUMN_ID)));
TextView name = (TextView) view
.findViewById(R.id.txtDetailsListComment);
String comment = cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_COMMENT));
if(comment.equals("")){
comment = "-";
}
name.setText(comment);
TextView debt = (TextView) view.findViewById(R.id.txtDetailsListAmount);
double amount = Double.parseDouble(cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_AMOUNT)));
DecimalFormat df = new DecimalFormat("#.##");
debt.setText(activity.getCurrency()+df.format(amount));
boolean isMyDebt;
if (cursor.getInt((cursor.getColumnIndex(Debts.COLUMN_IS_MY_DEBT))) == 0) {
isMyDebt = false;
} else {
isMyDebt = true;
}
if (!isMyDebt) {
debt.setTextColor(activity.getResources().getColor(
R.color.LawnGreen));
} else {
debt.setTextColor(activity.getResources().getColor(
R.color.Red));
}
}
}

Related

Custom list adapter onClick method returning a null view id

In creating a custom list adapter to be able to click different elements in a single row entry and start different activities, I ran across a problem where my findViewById() method was returning null. Additionally, only the first item in my ListView calls the onClick method; the other rows don't register clicks.
public class CustomListAdapter extends SimpleAdapter implements View.OnClickListener {
Context context;
TextView habitId;
Intent intent;
public CustomListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.habit_entry, null);
}
TextView tv = (TextView) v.findViewById(R.id.habitTitle);
tv.setOnClickListener(this);
ImageView iv = (ImageView) v.findViewById(R.id.plus);
iv.setOnClickListener(this);
return super.getView(position, convertView, parent);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.habitTitle:
Log.d("Test", "habitid = " + v.findViewById(R.id.habitId));
break;
case R.id.plus:
Log.d("Test", "plus clicked");
default:
break;
}
}
When the code is run, the habitTitle case of onClick prints
D/NULL: habitid = null
habit_entry
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_plus"
android:id="#+id/plus"
android:scaleType="centerCrop"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:background="#9bfcff" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/habitId"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="difficulty"
android:id="#+id/habitDifficulty"
android:gravity="right"
android:paddingLeft="15dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/habitTitle"
android:layout_alignEnd="#+id/habitTitle"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/plus"
android:layout_above="#id/habitDifficulty"
android:layout_alignWithParentIfMissing="true"
android:text="Your new habits go here!"
android:id="#+id/habitTitle"
android:padding="5dp"
android:textColor="#444444"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignParentStart="false"
android:gravity="right"
android:allowUndo="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="frequency"
android:id="#+id/habitFrequency"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
call in MainActivity
if(habitList.size() != 0) {
ListView listView = (ListView) findViewById(android.R.id.list);
ListAdapter adapter = new CustomListAdapter(
this, habitList, R.layout.habit_entry, new String[]{"habitId", "title", "difficulty", "frequency"}, new int[]{
R.id.habitId, R.id.habitTitle, R.id.habitDifficulty, R.id.habitFrequency});
listView.setAdapter(adapter);
}
The list populates perfectly, so I'm not sure why I'm having so much trouble with my adapter.
I see a couple problems here. You are inflating the View yourself and then returning the View returned by calling super.getView(position, convertView, parent);. You should instead rely on super.getView(position, convertView, parent); to create the View for you since you are using the default implementation like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
v.findViewById(R.id.habitTitle)
.setOnClickListener(this);
v.findViewById(R.id.plus)
.setOnClickListener(this);
return v;
}
Also v.findViewById(R.id.habitId)); is returning null inside onClick() because the variable v itself is the TextView with id = R.id.habitId.
You set the visibility of the habitId textview to gone.
The textview will not be inflated and is null.
You can read more information about visibility in the android documentation

Show Multiple Data in ListView

I am new in android. I want to show the data that I get from Database into ListView.
For now I can show only a single data.
How to show multiple data into custom ListView?
Here's my MainActivity.class
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
Log.d("Reading: ", "Reading all contacts..");
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllAccommodations();
ArrayList <String> allItems2 = new ArrayList<String>();
for (AllItem cn : allItems) {
allItems2.add(cn.getItem_name());
allItems2.add(cn.getAreaNAme());
}
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,allItems2);
listview.setAdapter(adapter);
I have my own custom ListView like this
Acco.xml
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
AccoLayout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Is there anyone can help me with this?
You need to create CustomAdapter class for this.
Use your listview as it is.
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
And your custom Listview also.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Create your custom adapter class and pass data to this class.
adapter = new ListAdapter(this, allItems2);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Here is CustomAdapter class.
public class ListAdapter extends BaseAdapter {
public ListAdapter(Activity a, List <AllItem> allItems) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.AccoLayout, null);
TextView itemName = (TextView)vi.findviewById(R.id.item_name);
TextView areaName = (TextView)vi.findviewById(R.id.area_name);
// Set your data here
itenName.setText(data.get(position));//like this
return vi;
}
}
I assume that your allItems2 list has a pattern like itemName0, areaName0, itemName1, areaName1, itemName2.. etc. Then, you can write a custom adapter like this.
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final List<String> items;
public CustomAdapter (Activity context, List<String> items) {
super(context, R.layout.AccoLayout, items);
this.context = context;
this.items= items;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.AccoLayout, null, true);
TextView itemName = (TextView) rowView.findViewById(R.id.item_name);
TextView areaName= (TextView) rowView.findViewById(R.id.area_name);
itemName.setText(items.get(2*position));
areaName.setText(items.get(2*position + 1));
return rowView;
}
}
Then call it from your main activity;
CustopAdapter adapter = new CustomAdapter (MainActivity.this, allItems2);
listview.setAdapter(adapter);
Also, by Android's convention, try not to use uppercase letters in XML file names to prevent any complication. You may change to acco_layout.xml.
Consider this to be your Cursor: after you fetch from DB
Cursor mCur = db.getAllAccommodations();
in onCreate:
CurAdapter Cur = new CurAdapter(getActivity(), mCur,0);
final ListView lv = (ListView)findViewById(R.id.listAccommodation);
lv.setFastScrollEnabled(true);
lv.setAdapter(Cur);
Then Make a Subclass:
private class CurAdapter extends CursorAdapter{
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.item_name);
TextView tv1 = (TextView) view.findViewById(R.id.area_name);
String item = (cursor.getString(cursor.getColumnIndexOrThrow("ColumnName1")));
String area = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("ColumnName2")));
tv1.setText(item);
tv.setText(area);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.AccoLayout, null);
return view;
}
}
Where R.layout.AccoLayout is your "listview row" layout, for example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_name"
android:textSize="14sp" />
</RelativeLayout>

get position of selected button in listview

I have a list view every item in list contains textviews and button what I want is when I click on the button of any item in list I want to printout the position of button if i clicked the button in first line i want to print out 0 and go on but it doesn't work
this is my method to display view
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
PhonesDbAdapter.KEY_NAME,
PhonesDbAdapter.KEY_CONTINENT,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.continent,
R.id.name
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.phone_layout,
cursor,
columns,
to,
0);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
public void print(View v)
{
}
and here how my item look like
<?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"
android:padding="6dip"
android:background="#f0f0f0" >
<TextView
android:id="#+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#275a0d"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView"
android:textColor="#000"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/call"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1"
android:background="#drawable/ic_launcher"
android:onClick="print"/>
</RelativeLayout>
and this the layout that contain the listview
<?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"
android:background="#f0f0f0">
<EditText android:id="#+id/myFilter" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10">
</EditText>
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
i spent alot of time with this problem and i hope that u can help me and im really sorry for my bad english
Are you implementing the method getView() on your adapter?
That's where you want to add the OnClickListener to your Button
Once you do that you can set the position as the tag of the Button and retrieve it on the onClick method.
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.your_item_layout, parent, false);
Button myButton = (Button) row.findViewById(R.id. call);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View view) {
int position = (int)view.getTag();
//Do whatever you want with the position here
}
});
myButton.setTag(position);
return row;
}
From last 3 three years I am not working on android so I am not sure my suggestion is correct or not. You need to test it for you purpose.
In your above case you cannot use onItemClickListner because it can only be used when you are tracking your complete list row. What I think is you can create a custom adapter and override its getView method. In getView method you will have your position and you can set button click listner for you button in getview. So In this way you can get your button position.
You try it once if you need some code then I can try to write it for you...
THIS IS HOW YOU CAN CREATE CUSTOM ADAPTER.
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
ArrayList<String> elements = null;
public my_custom_adapter(Context context, int type, ArrayList<String> elements)
{
super(context, type, elements);
this.elements = elements;
this.context = context;
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public Button cb;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (Button) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER
}
});
}
return rowView;
}
}

OnItemClick not responding for listview with checkbox and customcursoradapter

Listview with checkbox and CustomCursorAdapter
In ListActivity.java
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView listView = (ListView) findViewById(R.id.lists);
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//create Cursor called cursor
listAdapter = new DetailListAdapter(this, cursor);
getLoaderManager().initLoader(LOADER_ID, null, this);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
{
//want to call startactionmode with class implementing ListView.MultiChoiceModeListener
}
});
}
In DetailListAdapter.java
public class DetailListAdapter extends CursorAdapter
{
private Context context;
private ArrayList<Boolean> checked = new ArrayList<Boolean>();
private LayoutInflater mLayoutInflater;
public DetailListAdapter(Context context, Cursor c)
{
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View v = mLayoutInflater.inflate(R.layout.list_item, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c)
{
final View view = v;
final int position = c.getInt(0);
String data = c.getString(c.getColumnIndexOrThrow("DATA"));
/**
* Next set the name of the entry.
*/
TextView list_text = (TextView) view.findViewById(R.id.listitemtext);
CheckBox checkbox_text = (CheckBox) view.findViewById(R.id.listitemcheckbox);
list_text.setText(data);
checked.add(position - 1, false);
checkbox_text.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
view.setBackgroundColor(Color.GRAY);
checked.set(position - 1, true);
} else
{
view.setBackgroundColor(Color.BLACK);
checked.set(position - 1, false);
}
}
});
checkbox_text.setChecked(!checkbox_text.isChecked());
}
}
The list row xml is,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="#+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:focusable="false"
/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/check1"
android:layout_toRightOf="#+id/check1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/text1"
android:layout_alignBottom="#+id/text1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/text1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/text2"
android:layout_below="#+id/text1"
android:layout_toRightOf="#+id/check1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</RelativeLayout>
When I click on list item in emulator, checkbox is checked and row turns into gray.
But onitemclick is not getting called. I do have android:focusable="false" and android:focusableInTouchMode="false" on all items in layout which creates a row in list.
I want checkbox to be checked and menu to be displayed at top containing actions that can be perfomed on that selected list item..
Two OnClickListeners cannot be called from one touch event. As you see, the first listener's onCheckedChanged() method consumes the event preventing it from reaching the onItemClick() method.
I recommend using a Checkable layout in your rows that will maintain the check state for you and changing your adapter's getView() to set the appropriate background color on account of the view recycler.
(Post your XML in you question if you need help with this.)

android: Custom SimpleCursorAdapter does not load all data

In need of assistance, I have a listview activity which I want to display couple of textfields per item plus 2 buttons (like contacts app having a call button and a text message button. Reading tutorials I found out how to get the buttons to be clickable, but now my textviews (all except 1) don't display any data other then the stock text I've left in the layout file. The 1 text view does display the data I require it to. No where in my custom SCA do I feed it specific data so I'm assuming it happens in the background.
My R.id.company_address_details_phone loads the needed data without any manipulation on my part, but non of the other TextViews in my ViewHolder display any data. My For loop attached to my call button does output data from my cursor so I know it's not that I'm missing data. Any and all help is appreciated.
As a side note, when first wrote the code and did not use a custom SimpleCursorAdapter all my text views populated as expected (i just couldnt click buttons). This leads me to assume that my CompanyActivity class has no errors but that I'm missing something in my CompanyDetailsCursroAdapter class.
*EDIT***
So I'm answering my own question after I looked back and tried something. non of my views where being populated, I just had one of my views set with default text that made it appear as if it was pulled from my DB. I'm now populating my views with data from the cursor adapter passed to my custom cursor adapter using the below code. I would like to know if this is the best way of doing it or is there a more streamlined or android approved method.
viewHolder.addressTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressStreet")));
viewHolder.cityTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressCity")));
viewHolder.stateTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressState")));
viewHolder.zipTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressZip")));
viewHolder.phoneTextView.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("AddressPhone")));
CompanyDetailsCursorAdapter.java
public class CompanyDetailsCursorAdapter extends SimpleCursorAdapter implements Filterable{
private final Context context;
private int layout;
Cursor companyDetailsCursor;
public CompanyDetailsCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
this.companyDetailsCursor = c;
}
static class ViewHolder {
protected TextView addressTextView;
protected TextView cityTextView;
protected TextView stateTextView;
protected TextView zipTextView;
protected TextView phoneTextView;
protected ImageButton callButton;
protected ImageButton mapButton;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.company_address_details, null);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
viewHolder = new ViewHolder();
viewHolder.addressTextView = (TextView) convertView.findViewById(R.id.company_address_details_street);
viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.company_address_details_city);
viewHolder.stateTextView = (TextView) convertView.findViewById(R.id.company_address_details_state);
viewHolder.zipTextView = (TextView) convertView.findViewById(R.id.company_address_details_zip);
viewHolder.phoneTextView = (TextView) convertView.findViewById(R.id.company_address_details_phone);
viewHolder.callButton = (ImageButton) convertView.findViewById(R.id.company_address_details_call_button);
viewHolder.callButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
companyDetailsCursor.moveToPosition(position);
if (RouteTrackerGlobal.APP_DEBUG) { Log.d(RouteTrackerGlobal.APP_TAG,"getview position set to " + String.valueOf(position));}
for (int i = 0; i < companyDetailsCursor.getColumnCount();i++){
Log.i(RouteTrackerGlobal.APP_TAG, companyDetailsCursor.getString(i));
}
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
CompanyActivity.java
public class CompanyActivity extends ListActivity {
DbAdapter dbAdapter;
int companyID;
Context context = this;
Cursor companyDetailsCursor;
#Override
public void onCreate(Bundle savedInstanceBundle) {
super.onCreate(savedInstanceBundle);
dbAdapter = new DbAdapter(this);
setContentView(R.layout.company_activity);
Bundle companyActivityBundle = getIntent().getExtras();
companyID = companyActivityBundle.getInt("CompanyID");
ListView list = getListView();
list.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
return false;
}
});
}
#Override
public void onPause() {
dbAdapter.close();
super.onPause();
}
#Override
public void onResume() {
dbAdapter.open();
super.onResume();
loadDetails();
}
#Override
protected void onListItemClick (ListView l, View v, int position, long id){
//TODO: When item in list clicked, call activity to display address and directions
}
public void loadDetails() {
TextView companyLabel = (TextView) findViewById(R.id.company_activity_company_label);
companyDetailsCursor = dbAdapter.getCompanyDetails(companyID);
startManagingCursor(companyDetailsCursor);
companyDetailsCursor.moveToFirst();
companyLabel.setText(companyDetailsCursor.getString(companyDetailsCursor.getColumnIndex("CompanyName")));
String[] columns = new String[] {"AddressPhone", "AddressStreet", "AddressCity", "AddressState", "AddressZip"};
int[] to = new int[] {R.id.company_address_details_phone, R.id.company_address_details_street, R.id.company_address_details_city, R.id.company_address_details_state, R.id.company_address_details_zip};
CompanyDetailsCursorAdapter companyDetailsAdapter = new CompanyDetailsCursorAdapter(context, R.layout.company_address_details, companyDetailsCursor, columns, to);
setListAdapter(companyDetailsAdapter);
}
}
Company_Address_Details.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" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/company_address_details_phone"
style="#style/company_address_listview"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/company_address_details_phone_default" />
<ImageButton android:id="#+id/company_address_details_call_button"
style="#style/company_address_listview"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:src="#android:drawable/sym_action_call"
android:contentDescription="#string/company_address_details_phone_hint"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/company_address_details_call_button1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/company_address_details_street"
style="#style/company_address_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/company_address_details_street_default" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/company_address_details_city"
style="#style/company_address_listview"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/company_address_details_city_default" />
<TextView
android:id="#+id/company_address_details_state"
style="#style/company_address_listview"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="#string/company_address_details_state_default" />
<TextView
android:id="#+id/company_address_details_zip"
style="#style/company_address_listview"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="#string/company_address_details_zip_default" />
</RelativeLayout>
</LinearLayout>
<ImageButton
android:id="#+id/company_address_details_call_button1"
style="#style/company_address_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:contentDescription="#string/company_address_details_phone_hint"
android:paddingLeft="20dp"
android:src="#android:drawable/ic_dialog_map" />
</RelativeLayout>
</LinearLayout>

Categories

Resources