android: Custom SimpleCursorAdapter does not load all data - android

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>

Related

Create a List of Custom form Layout to enter details based on the count entered

I am a novice in Android App Development, learning through online materials, I am developing an app in which i'm stuck at one place, I enter the Number of Participants in layout1 based on the count i want to generate a custom form that i have designed (shown in layout2) to get the input of so many people. I tried using Array Adapter and Inflator concepts to display the custom layout in a ListView but i couldn't do it, or if it could be acheived please help me. My desired output is shown in layout3
After navigating from Layout1 with the No of Participants I'm doing this so that i create my desired ouput as attached in Layout3. These are the things i tried but am stuck, please forgive if the code is ameateur and help me find a way to complete this, your help would be appreciated.
Layout3Activity
public class Layout3Activity extends Activity{
//
//
//
ListView listView;
FriendsLayoutAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout3);
listView = (ListView) findViewById(R.id.listView1);
adapter = new FriendsLayoutAdapter(getApplicationContext(),R.layout.layout2);
listView.setAdapter(adapter);
}
//
//
//
//
}
layout2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="160dp"> <!-- setting layout height to 160dp so that it only fits in the box we have created-->
<TextView
android:text="Friend i"
android:textAppearance="?android:textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friendLabel"
android:layout_above="#+id/nameEditText"
android:layout_alignStart="#+id/nameTextView"
android:layout_marginBottom="15dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name here..!!"
android:id="#+id/nameEditText"
android:layout_toRightOf="#+id/nameTextView"
android:layout_alignBaseline="#+id/nameTextView"
android:layout_alignBottom="#+id/nameTextView"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Amount Spent..!!"
android:id="#+id/amountEditText"
android:layout_toRightOf="#+id/amountTextView"
android:layout_alignBaseline="#+id/amountTextView"
android:layout_alignBottom="#+id/amountTextView"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp" />
<TextView
android:text="Amount:"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:id="#+id/amountTextView"
android:layout_marginTop="17dp"
android:layout_below="#+id/nameEditText"
android:layout_alignParentStart="true" />
<TextView
android:text="Name:"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:id="#+id/nameTextView"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/amountTextView" />
<!--<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/amountEditText"
android:background="#color/jetBlack">
</View>-->
</RelativeLayout>
layout3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
FriendsLayoutAdapter
public class FriendsLayoutAdapter extends ArrayAdapter{
String[] friends = {"Friend 1", "Friend 2","Friend 3","Friend 4"};
public FriendsLayoutAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.entries_layout,viewGroup,false);
TextView friendLabel = (TextView) view.findViewById(R.id.friendLabel);
TextView name = (TextView) view.findViewById(R.id.nameTextView);
TextView amount = (TextView) view.findViewById(R.id.amountTextView);
EditText nameEdit = (EditText) view.findViewById(R.id.nameEditText);
EditText amountEdit = (EditText) view.findViewById(R.id.amountEditText);
return super.getView(i, view, viewGroup);
}
//
//
//
}
You need to modify your adapter, you are displaying 0 elements.
Try this :
public class FriendsLayoutAdapter extends ArrayAdapter{
String[] friends = {"Friend 1", "Friend 2","Friend 3","Friend 4"};
public FriendsLayoutAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public int getCount() {
return friends.length();
}
#Override
public Object getItem(int i) {
return friends[i];
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
String value = friends[i];
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.entries_layout,viewGroup,false);
TextView friendLabel = (TextView) view.findViewById(R.id.friendLabel);
TextView name = (TextView) view.findViewById(R.id.nameTextView);
TextView amount = (TextView) view.findViewById(R.id.amountTextView);
EditText nameEdit = (EditText) view.findViewById(R.id.nameEditText);
EditText amountEdit = (EditText) view.findViewById(R.id.amountEditText);
friendLabel.setText(value);
return super.getView(i, view, viewGroup);
}
}

How to add checkboxes to a listview that uses a CursorAdapter

I have a listview which uses a CursorAdapter to populate the listview. I want to allow users to choose multiple items and perform actions, such as delete items, on all of the selected items at the same time. I tried adding a CheckBox item to the layout xml of the item item_vocab.xml. The checkbox showed up beside each item, but I wasn't able to figure out how to keep track of the status of the checked items. I also tried adding setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) instead but the checkbox didn't show up.
Here are some of my code:
item_vocab.xml
<?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:longClickable="true">
<ImageView
android:layout_width="36sp"
android:layout_height="36sp"
android:id="#+id/vocabLevel"
android:layout_gravity="right"
android:src="#drawable/level_bars"
android:scaleType="fitXY"
android:contentDescription="#string/vocab_level"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/vocabName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/vocabLevel"
android:layout_toStartOf="#+id/vocabLevel"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/vocabDefinition"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/vocabLevel"
android:layout_toStartOf="#+id/vocabLevel"
android:layout_below="#id/vocabName"/>
</RelativeLayout>
activity_my_vocab.xml
<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"
tools:context="charlesli.com.personalvocabbuilder.io.MyVocabActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/mVocabList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty_text_view"
android:id="#android:id/empty"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
MyVocabActivity.java
public class MyVocabActivity extends ActionBarActivity {
private VocabCursorAdapter mVocabAdapter;
private ListView mVocabListView;
private TextView mEmptyTextView;
private Cursor mCursor;
private String mSelectedVocab;
private CheckBox mCheckBox;
private VocabDbHelper mDbHelper = new VocabDbHelper(this);
private ArrayList<String> mCheckedItems = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_vocab);
mVocabListView = (ListView) findViewById(R.id.mVocabList);
mEmptyTextView = (TextView) findViewById(android.R.id.empty);
mVocabListView.setEmptyView(mEmptyTextView);
mCursor = mDbHelper.getCursorMyVocab(mDbHelper);
mVocabAdapter = new VocabCursorAdapter(this, mCursor, 0);
mVocabListView.setAdapter(mVocabAdapter);
mVocabListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mVocabListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedVocab = (String) ((TextView) view.findViewById(R.id.vocabName)).getText();
editVocabAlertDialog(mSelectedVocab, view, position, id);
return true;
}
});
}
VocabCursorAdapter.java
public class VocabCursorAdapter extends CursorAdapter {
private static final int DIFFICULT = 0;
private static final int FAMILIAR = 1;
private static final int EASY = 2;
private static final int PERFECT = 3;
public VocabCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_vocab, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvVocabName = (TextView) view.findViewById(R.id.vocabName);
TextView tvVocabDefinition = (TextView) view.findViewById(R.id.vocabDefinition);
ImageView tvVocabLevel = (ImageView) view.findViewById(R.id.vocabLevel);
String vocab = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_VOCAB));
String definition = cursor.getString(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_DEFINITION));
int level = cursor.getInt(cursor.getColumnIndexOrThrow(VocabDbContract.DatabaseInfo.COLUMN_NAME_LEVEL));
tvVocabName.setText(vocab);
tvVocabDefinition.setText(definition);
if (level == DIFFICULT) {
tvVocabLevel.setImageResource(R.drawable.level_bars_difficult);
}
else if (level == FAMILIAR) {
tvVocabLevel.setImageResource(R.drawable.level_bars_familiar);
}
else if (level == EASY) {
tvVocabLevel.setImageResource(R.drawable.level_bars_easy);
}
else if (level == PERFECT) {
tvVocabLevel.setImageResource(R.drawable.level_bars_perfect);
}
}
}
Please let me know if I need to provide further information.
You can use CustomListview for this, make another xml that have textview and checkbox and use that layout in your custom adapter class for populating list then it will work like you want. For example look this tutorial:
http://techlovejump.com/android-listview-with-checkbox/

ListView updated from EditText

I have a Custom Listview that it is updated from a EditText component in a dialog. I have the custom row, the adapter class and the custom dialog all working but I can't seem to trigger the code in the adatper class that would add the text from the edit text control to the list. Here is my activity code, let me know if you want the adapter code. It worked before I added the custom row and adapter to the list :(
Symptom of problem: emailAdapter.notifyDataSetChanged(); does nothing
public class InvitePlayers_Activity extends Activity {
ListViewAdapter emailAdapter = null;
ImageView imgView_mail;
ImageView imgView_confirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //remove title bar
setContentView(R.layout.activity_inviteplayers);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//assign controls
final ListView listView = (ListView) findViewById(R.id.listView_invitePlayers);
imgView_mail = (ImageView)findViewById(R.id.imgView_mail);
//Test data
ArrayList<String> inviteNew = new ArrayList<String>();
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
emailAdapter = new ListViewAdapter(this,inviteList);
listView.setAdapter(emailAdapter);
// Assign adapter to ListView
listView.setTextFilterEnabled(true);
//Edit listeners
imgView_mail.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
//variables
final String enteredMail = "testListViewEntry";
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
ArrayList<String> invite = new ArrayList<String>();
invite.add(0, enteredMail);//add first email
invite.add(1,"icon_invitestatussent.png"); //add first status icon
inviteList.add(invite);
emailAdapter.notifyDataSetChanged();
listView.setAdapter(emailAdapter);
}
});
}
}
Adapter code as requested
public class ListViewAdapter extends BaseAdapter {
private Activity context;
ArrayList<ArrayList<String>> inviteDetails = new ArrayList<ArrayList<String>>();
public ListViewAdapter(Activity context, ArrayList<ArrayList<String>> inviteDetails ) {
this.inviteDetails = inviteDetails;
this.context = context;
}
#Override
public int getCount() {
return inviteDetails.size();
}
#Override
public Object getItem(int i) {
return inviteDetails.get(i).get(0);
}
#Override
public long getItemId(int i) {
return i;
}
public View getView(int position, View view, ViewGroup parent){
//Inflater
LayoutInflater inflater = context.getLayoutInflater();
//get row view
if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.list_item_email, null);
}
//assign controls
final TextView textView_playerEmail = (TextView) view.findViewById(R.id.textView_playerEmail);
ImageView imgView_inviteStatus = (ImageView) view.findViewById(R.id.imgView_inviteStatus);
//Assign control values that are dynamic
textView_playerEmail.setText(inviteDetails.get(position).get(0));
imgView_inviteStatus.setImageResource(R.drawable.icon_invitestatussent);
return view;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Custom row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="#+id/textView_playerEmail"
android:textColor="#color/white"
android:text="item1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_inviteStatus" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_remove"
android:src="#drawable/btn_cancel" />
</LinearLayout>
The activity layout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:background="#color/yellow"
android:layout_margin="20dp"
android:padding="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:gravity="left|center">
<ImageView
android:layout_width="45dp"
android:layout_height="34dp"
android:id="#+id/imgView_mail"
android:src="#drawable/btn_mail"
android:layout_weight="0.22"
android:padding="3dp" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView_invitePlayers"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/btn_confirm"
android:src="#drawable/btn_confirm"
android:clickable="false"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:padding="2dp"
android:layout_weight="1" />
</LinearLayout>
</FrameLayout>
Well, for what we discussed, you wanted something like this:
When you want to make a custom ListView, you have to write your own adapter. In this particular case I'm subclassing the BaseAdapter class.
This custom adapter will take hold of my data model, and will inflate the data to my ListView rows.
First, I'll create the XML of my custom row. You can see the code below.
item_mail.xml
<?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: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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:id="#+id/tv_mail"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iv_icon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_menu_report_image" />
</RelativeLayout>
Here I've created a row that displays a text and an image.
Now, I'll create my custom adapter to handle this XML. As you can see below.
MailAdapter.java
public class MailAdapter extends BaseAdapter {
private static final String LOG_TAG = MailAdapter.class.getSimpleName();
private Context context_;
private ArrayList<ArrayList<String>> mailitems;
public MailAdapter(Context context, ArrayList<ArrayList<String>> mailitems) {
this.context_ = context;
this.mailitems = mailitems;
}
#Override
public int getCount() {
return mailitems.size();
}
#Override
public Object getItem(int position) {
return mailitems.get(position).get(0);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context_.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.item_mail, null);
}
TextView tv_mail = (TextView) convertView.findViewById(R.id.tv_mail);
ImageView iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
String mail = mailitems.get(position).get(0);
String icon = mailitems.get(position).get(1);
Log.d(LOG_TAG,"Mail: " + mail + " mail_icon: " + icon);
tv_mail.setText(mail);
// iv_icon.setImageURI(); Here you can do whatever logic you want to update your image, using URI's, ID's, or something else.
return convertView;
}
}
Ok. Now we have everything to make this work. In your Activity class, do something like that:
activity_main.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:text="#string/hello_world"
android:id="#+id/tv_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tv_header">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add mail"
android:id="#+id/button"
android:layout_gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private int numMail = 1; // Dummy int to create my items with different numbers.
private MailAdapter mailAdapter; // Your custom adapter.
private ArrayList<ArrayList<String>> mailItems; // This is going to be your data structure, everytime you change it, call the notifyDataSetChanged() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button);
ListView lv_mail = (ListView) findViewById(R.id.listView);
mailItems = new ArrayList<>();
mailAdapter = new MailAdapter(this,mailItems);
lv_mail.setAdapter(mailAdapter);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addItem(); // The method I'm using to insert the item. Look for it below.
}
});
}
// Here I'm creating a new ArrayList, and appending it to my 'mailItems' Array. After that, I'm notifying the adapter that my data changed.
private void addItem() {
ArrayList<String> mail = new ArrayList<>();
mail.add(0,"mail " + numMail++);
mail.add(1,"path_to_image"); // Depending on what you want to do, put your path, URI, or whatever other way you want to store that image data.
mailItems.add(mail); // Inserting the data on the ArrayList.
mailAdapter.notifyDataSetChanged(); // Notifying the adapter that my ArrayList was modified.
}
}
This should do the trick.
I guess your problem was that you weren't updating the same ArrayList that was in your custom adapter. That's why when you called notifyDataSetChanged()nothing happened, I mean. You were creating a new ArrayList, which wasn't the same that was in your adapter. So here's what I did... I've made the ArrayList global, and then I've used it in my custom adapter constructor. After that, when the user triggers the onClick() method of my button, I'm inserting some new data on my global Array, and then I'm notifying the adapter that the data changed.
You can read a little more about that here and I've found a similar question here, which you can read as well.
Edit: Another related question, which might be an interesting read.

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;
}
}

ListView selection problem with rating control?

friends,
i have created very simple custom listview adapter with ratingBar in it.
now i have noticed one thing i cannot rate those rating bars in listview because
when i click on listview that row particular row gets selected.
any one guide me how to select individual items in android listview?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
/>
</LinearLayout>
and listview_item design
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="265dip"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:textSize="25dip"
android:text="This is text1"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="This is text2"/>
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="#+id/star"
android:numStars="10"
android:stepSize="0.1"
android:isIndicator="true"
/>
</LinearLayout>
</LinearLayout>
any help would be appreciated.
Dear UMAR
In order to implement the listeners to the each element to the listview(Single row) use Custom adapter and in the getview(....) method of the custom adapter ,implements the listeners which u want.. sample code `public class FindFriendsListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private LayoutInflater mInflater;
private Context context;
private String userid1;
private int usersno1;
private DBAdapter db;
public FindFriendsListAdapter(Context context, ArrayList<SingleElementDetails> results,String userid,int usersno) {
this.context=context;
this.userid1=userid;
this.usersno1=usersno;
allElementDetails = results;
mInflater = LayoutInflater.from(context);
db=new DBAdapter(context);
db.open();
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.friendlisthelper, null);
ImageView imageview = (ImageView) convertView.findViewById(R.id.friendimageview);
TextView textview = (TextView) convertView.findViewById(R.id.friendtextview);
Button button=(Button)convertView.findViewById(R.id.friendbutton);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do what u want
}
});
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// do what u want
}
});
return convertView;
}
}
`
android:isIndicator="false" in order to rate it. and listview selection will be gone automatically and control will be transferred to that rating control.
Try this
ListView lv_party;
lv_party.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position,long id)
{
// TODO Auto-generated method stub
view.findViewById(R.id.btn_del).setOnClickListener(new View.OnClickListener(){
//your code
}

Categories

Resources