getting data from edittext placed in listview - android

I created a list with textview & edittext using holder... it looks like
Textview Edittext
---------------------------------
Textview Edittext
---------------------------------
Textview Edittext
---------------------------------
but I cant get the data from each every Edittext...
can any one assist me.. how to achieve it... assist with sample code...

I had a similar problem, i did as follows;
View view = listview.getChildAt(position);
EditText text = (EditText)view.findViewById(R.id.myEditBox);
String contents = text.getText().toString();
Hope it helps!
// Fredrik

for (int i = 0; i < telefoneListView.getAdapter().getCount(); i++) {
View viewTelefone = telefoneListView.getChildAt(i);
EditText tipoEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_tipo);
EditText telefoneEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_telefone);
Log.d(TAG, tipoEditText.getText().toString() + ":" + telefoneEditText.getText().toString());
}
To navigate into all itens and get EditText on each.

Hi try the following code u will reach your requirement,
public class TestListView extends Activity {
ListView listView,listView2;
String[] titles = {"List 1 title1","List 1 title2","List 1 title3","List 1 title4","List 1 title5","List 1 title6","List 1 title7","List 1 title8","List 1 title9"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = new ListView(this);
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
listView.setAdapter(new CustomListAdapetr(this, titles));
((LinearLayout)findViewById(R.id.mailLayout)).addView(listView);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
CustomView customView= (CustomView) arg0.getChildAt(arg2);
TextView textView = (TextView) customView.getChildAt(0);
Log.v("", ""+textView.getText());
}
});
}
public class CustomListAdapetr extends BaseAdapter{
private Context mContext;
private String[] list;
public CustomListAdapetr(Context context, String[] titles) {
mContext = context;
list = titles;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomView cv;
if (convertView == null) {
cv = new CustomView(mContext,""+list[position]);
}
else {
cv = (CustomView) convertView;
cv.setTitle(list[position]);
}
return cv;
}
}
private class CustomView extends LinearLayout {
public CustomView(Context context, String itemName) {
super(context);
this.setOrientation(HORIZONTAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(itemName);
mTitle.setTextSize(25);
addView(mTitle, new LinearLayout.LayoutParams(200, LayoutParams.WRAP_CONTENT));
}
/** * Convenience method to set the title of a SpeechView */
public void setTitle(String title) {
mTitle.setText(title);
}
/** * Convenience method to set the dialogue of a SpeechView */
private TextView mTitle;
}
}

Through
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
});
method we can get the click on item position .by using that position we get the appropriate edittext value

Related

How do I add items to a listView (for duplicates increase quantity) and view in following activity?

I have an a gridView that has items, each item has a name and quantity. When I click the item in the gridView, I would like to add the item and the quantity to a dynamic list view that is in the same activity. If I click on the item a several times, the items quantity in the dynamic listView should be increased (no duplicate items are allowed.) This is my current gridView, I have removed the code that adds and a previous listView that failed and I am having it just toast for now:
public class GridviewAdapter extends BaseAdapter
{
private ArrayList<String> listItem;
private ArrayList<Integer> listPicture;
private Activity activity;
public GridviewAdapter(Activity activity, ArrayList<String> listItem, ArrayList<Integer> listPicture) {
super();
this.listItem = listItem;
this.listPicture = listPicture;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listItem.size();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listItem.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView itemPicture;
public TextView itemName;
public TextView itemPrice;
public TextView itemStock;
public TextView itemAvailability;
}
#Override
public View getView(int position, View convertView, ViewGroup
parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.item_grid_row,
null);
view.itemName = (TextView)
convertView.findViewById(R.id.itemName);
view.itemPrice = (TextView)
convertView.findViewById(R.id.itemPrice);
view.itemStock = (TextView)
convertView.findViewById(R.id.itemStock);
view.itemAvailability = (TextView)
convertView.findViewById(R.id.itemAvailability);
view.itemPicture = (ImageView)
convertView.findViewById(R.id.itemPicture);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.itemName.setText(listItem.get(position));
// view.itemPicture.setImageDrawable(listItem.get(position));
return convertView;
}
}
this is the code for the checkout fragment where the gridView is clicked.
public class CheckoutFragment extends Fragment {
private CheckoutViewModel checkoutViewModel;
private EditText editText1;
private GridviewAdapter mAdapter;
private ArrayList<String> listItem;
private ArrayList<Integer> listPicture;
private GridView gridView;
DBHelper db;
LinearLayout layout_total, layout_grid;
private Button sum;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle
savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_checkout,
container, false);
db = new DBHelper(getActivity());
Display display =
getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
editText1 = (EditText)root.findViewById(R.id.editText1);
editText1.setHeight(height/12);
editText1.setShowSoftInputOnFocus(false);
editText1.setFocusable(false);
editText1.setTextColor(Color.parseColor("#FFFFFF"));
editText1.setHintTextColor(Color.parseColor("#FFFFFF"));
editText1.getBackground().setColorFilter(Color.parseColor("#008577"),
PorterDuff.Mode.SRC_ATOP);
editText1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cart = new Intent(getActivity(),
CartActivity.class);
startActivity(cart);
}
});
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(getActivity(),db.getAll(),
listPicture);
// Set custom adapter to gridview
gridView = (GridView)root.findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int
position,
long arg3) {
//this is where the grid is clicked and items added to dynamic listView
Toast.makeText(getActivity(),
mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
return root;
}
public void prepareList() {
// listItem = new ArrayList<String>();
db = new DBHelper(getActivity());
List<Item> listItem = new ArrayList<>();
listItem = db.getAllItems(db.ITEMS_TABLE_NAME);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.action_add_customer:
//startActivity(getActivity(),
NewCustomerActivity.class);
Intent newCustomer = new Intent(getActivity(),
NewCustomerActivity.class);
startActivity(newCustomer);
return true;
/* case R.id.action_add_customer:
return true;*/
default:
return super.onOptionsItemSelected(item);
}
}
}
You need to explain the situation a bit better. For now i assume your problem is that the items are duplicating the second list instead of appending the count. For this there is no
one line solution. What you need to do is once an item is selected in the grid, search if it exists in the second list. In that case update the quantity, otherwise add it to the list. Since your second list is based on a Table , you have to execute 2 queries. One to identify if the item already exists (Select Query) and other to change the value (Insert or Update Query). If i misunderstood please correct me so that i can help you better.
Why don't you return the model class in your GridView getItem(..) method instead of 'String' it should work like this
public class GridviewAdapter extends BaseAdapter
{
ArrayList<GridModel> gridItems;
//Constructor
public GridviewAdapter(Activity activity, ArrayList<GridModel> gridItems) {
super();
this.gridItems = gridItems;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return griditems.size();
}
#Override
public GridModel getItem(int position) {
// TODO Auto-generated method stub
return gridItems.get(position);
}
//...add your getView...
}
//GridModel Should be Like So..
class GridModel {
public String itemPicture;
public String itemName;
public String itemPrice;
public String itemStock;
public String itemAvailability;
//or use private variables with getter-setter methods
}

How to update values in database using an update image button in custom list adapter?

This is my custom list adapter. I want to update the values in table using the update ImageButton in the list. On clicking it, the old values should be shown in a new activity and then the edited value must be stored in the database. However, I am unable to pass an intent inside the onClick() method.
Please suggest me a solution
public class CustomListAdapter extends BaseAdapter implements ListAdapter
{
private ArrayList<String> list = new ArrayList<String>();
private Context context;
OnItemSelectedListener onItemSelectedListener;
public int pos;
String pass,pass2,edit,epass;
public CustomListAdapter(List list, Context context) {
this.list = (ArrayList<String>) list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
//pass2 = list.toString();
return list.get(pos);
}
//#Override
//public Long getItemId(int pos) {
//
// //just return 0 if your list items do not have an Id variable.
//}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_custom_list, null);
}
//Handle TextView and display string from your list
final TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.delete_btn);
ImageButton editBtn = (ImageButton)view.findViewById(R.id.edit_btn);
//Button addBtn = (Button)view.findViewById(R.id.add_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position);
pass = listItemText.getText().toString();
notifyDataSetChanged();
pass2 = pass.substring(0,pass.indexOf(' '));
System.out.println(pass2);
Moneydb.delete(pass2);
}
});
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
edit=listItemText.getText().toString();
epass = listItemText.getText().toString();
edit = epass.substring(0,epass.indexOf(' '));
Moneydb.edit(edit);
}
});
return view;
}
protected Context getContext() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return list.get(position).getId();
return 0;
}
public void clear() {
//CustomListAdapter collection = null;
// TODO Auto-generated method stub
list.clear();
notifyDataSetChanged();
}
I suggest you to assign and ContextMenu to your list view with two MenuItem, Edit and Delete and write associated code outside of adapter
or you can start Activity by :
Intent new_intent = new Intent(v.getRootView().getContext(),edit_activity.class);
new_intent.putExtra("Key","Value");
v.getRootView().getContext().startActivity(new_intent);
i think the first method is best ;)

How to set radiobutton unchecked in adapter class

I have a customdialog box ,in this i have a listview and a spinner item.The listview contains textview and a radio button in each row.I can select only one row at a time with the help of radio buttons.This is happening.
Problem is coming when i select spinner items and then i select radio button ,the previous radio button is still selected.Now what i want when i select spinner items ,the previously selected radiobuttons should be unchecked.
Note:my spinner is in Main class and radio button is in CustomAdapterClass which i am calling in my Main class
Here is my code: Adapter Class:
public class APTRequestCustomAdapter extends BaseAdapter{
Context context;
ArrayList<APTRequestCustomdetails> APTRequestCustomitems;
private static String display_aptstatus1="";
public static String adapterbookingid="";
private boolean userSelected = false;
public static RadioButton mCurrentlyCheckedRB;
String patientid1="";
public static String from2="";
public static String to2="";
public static String testing2="";
public static String cancelbookid="";
public static String radiostring="";
public static String troubleradiobutton="";
private int selectedItemIndex=-1;
public APTRequestCustomAdapter(Context context,
ArrayList<APTRequestCustomdetails> aPTRequestCustomitems) {
super();
this.context = context;
this.APTRequestCustomitems = aPTRequestCustomitems;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return APTRequestCustomitems.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return APTRequestCustomitems.get(arg0);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v=convertView;
final int pos=position;
if(v==null){
v = LayoutInflater.from(context).inflate(R.layout.backupcustomdailoguniversalappointment,null);
}
TextView patientname=(TextView)v.findViewById(R.id.txtpatientname);
TextView tag=(TextView)v.findViewById(R.id.txttype);
TextView phone=(TextView)v.findViewById(R.id.txtphone);
TextView age=(TextView)v.findViewById(R.id.txtage);
TextView Apptstatus=(TextView) v.findViewById(R.id.txtapptstatus);
TextView bookid=(TextView) v.findViewById(R.id.txtbookingid);
ImageView remove=(ImageView)v.findViewById(R.id.txtremove);
RadioButton radio=(RadioButton)v.findViewById(R.id.txtRadiobutton);
patientname.setPaintFlags(patientname.getPaintFlags() |Paint.UNDERLINE_TEXT_FLAG);
patientname.setText(APTRequestCustomitems.get(position).getPatient_Name());
tag.setText(APTRequestCustomitems.get(position).getTag());
phone.setText(APTRequestCustomitems.get(position).getPhone());
age.setText(APTRequestCustomitems.get(position).getAge());
Apptstatus.setText(APTRequestCustomitems.get(position).getAppointmentstatus());
bookid.setText(APTRequestCustomitems.get(position).getBookingId());
Apptstatus.setVisibility(View.INVISIBLE);
bookid.setVisibility(View.INVISIBLE);
//APT_CustomRequestResponse.radiostring="";
// adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();// wrong code
patientname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Hello buddy");
Intent io=new Intent(context, PatientSummaryActivity.class);
DoctorGlobal.patid=APTRequestCustomitems.get(pos).getPatient_ID();
DoctorGlobal.patname=APTRequestCustomitems.get(pos).getPatient_Name();
System.out.println("PATID"+DoctorGlobal.patid);
context.startActivity(io);
}
});
if (position==getCount()-1 && userSelected==false) {
// radio.setChecked(true);
mCurrentlyCheckedRB = radio;
} else {
radio.setChecked(false);
}
if(APTRequestCustomitems.get(position).getAppointmentstatus().equals("1")){
remove.setVisibility(View.INVISIBLE);
radio.setVisibility(View.INVISIBLE);
v.setBackgroundColor(Color.parseColor("#1569C7"));
}else
{
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
remove.setVisibility(View.VISIBLE);
radio.setVisibility(View.VISIBLE);
radio.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
APTRequestCustomAdapter.radiostring="checkedtrue";
if (mCurrentlyCheckedRB !=null) {
if (mCurrentlyCheckedRB ==null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
// Toast.makeText(context, ""+pos, Toast.LENGTH_LONG).show();
adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();
}
if (mCurrentlyCheckedRB == v)
return;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton) v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton) v;
}
});
}
return v;
}
}
And this is the code where i am calling the adapter class:
This method is in Async Class on PostExecute()method
public void showCustomDialog() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(context);
List<String> list=new ArrayList<String>();
list.add("Normal");
list.add("Low");
list.add("High");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.customdialoguniversalappointment);
ListView listcustomuniversalappt=(ListView) dialog.findViewById(R.id.listcustomuniversalappt);
LinearLayout layoutsubject=(LinearLayout) dialog.findViewById(R.id.layoutsubject);
LinearLayout layoutappt=(LinearLayout) dialog.findViewById(R.id.layoutappt);
spinnerappt=(Spinner)dialog.findViewById(R.id.permissionspinner);
ImageView cancel=(ImageView)dialog.findViewById(R.id.imgcancel);
Button cancelappt=(Button)dialog.findViewById(R.id.btncancelappt);
Button confirmappt=(Button)dialog.findViewById(R.id.btnconfirmappt);
EditText subject=(EditText) dialog.findViewById(R.id.edtsubject);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerappt.setAdapter(dataAdapter);
spinnerappt.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index = arg0.getSelectedItemPosition();
selected_item=arg0.getItemAtPosition(arg2).toString();
APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);//this line of code is not executing,the radio button remains checked in the view
if(APTRequestCustomAdapter.mCurrentlyCheckedRB.isChecked()){// this lines of codes are not executing even though the radio button is checked ,i don't know why
APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);// And this line is also not executing
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
APTRequestCustomAdapter adap=new APTRequestCustomAdapter(context,run_custom_universal_apptdata());
listcustomuniversalappt.setAdapter(adap);
/*//runapptstatus_method();
listcustomuniversalappt.setChoiceMode(1);
/
dialog.show();
}
Try to use your Custom Adapter like,
public class MyRadioAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<Variation> mVariations;
private int mSelectedVariation;
public MyRadioAdapter(Context context, ArrayList<Variation> variations, int selectedVariation)
{
mContext = context;
mVariations = variations;
mSelectedVariation = selectedVariation;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_radio_adapter_item, null);
}
final Variation variation = mVariations.get(position);
TextView name = (TextView) view.findViewById(R.id.name);
RadioButton radio = (RadioButton) view.findViewById(R.id.radio);
name.setText(variation.getName());
if(position==mSelectedVariation) radio.setChecked(true);
else radio.setChecked(false);
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
mSelectedVariation = position;
MyRadioAdapter.this.notifyDataSetChanged();
}
});
return view;
}
here Radio buttons are usually grouped by Radio Group.When one RadioButton within a group is selected, all others are automatically deselected.

Update Listview item and get values using BaseAdapter Android

I have a listview of that have multiple items.I want to update the items value on click of button and want to get the the values of all the items on click of the button.But i am unable to do that.Please help me to get this functionality
Activity code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_ItemValue);
setContentView(R.layout.activity_iween_booking_page);
listView = (ListView) findViewById(R.id.passengerList);
showPassengerListView(passengerList);
}
getValue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(){
}else{
//Here i have to get all the value of the listview items in a array
}
}
});
}
private void showPassengerListView(final String[] passengerList) {
listView.setAdapter(new PassengerListUpdate(passengerList));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public void passengerInformationPopup(final String[] passengerList, final int position) {
final Dialog dialog= new Dialog(MainActivity.this,R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.passenger_details_dialog);
dialog.show();
setValueDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
isSuccess= true;
if(isSuccess){
dialog.cancel();
//Here i Have to Update the item at position with some value
}
}
});
}
public void goBack(View v) {
finish();
}
class PassengerListUpdate extends BaseAdapter {
String[] ItemValue;
public PassengerListUpdate(String[] text) {
ItemValue = text;
}
public int getCount() {
// TODO Auto-generated method stub
return ItemValue.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View vi;
vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView ItemValue;
ItemValue = (TextView) vi.findViewById(android.R.id.text1);
ItemValue.setText(ItemValue[position]);
return (vi);
}
}
Please help me to how to set a items value and how to get the listview values
[Want] to get the the values of all the items on click of the button
To get all values, create a method getAllValues() inside PassengerListUpdate:
public String[] getAllValues() {
return ItemValue;
}
Declare your adapter globally:
PassengerListUpdate myAdapter;
Initialize it in showPassengerListView(String[]):
myAdapter = new PassengerListUpdate(passengerList);
listView.setAdapter(myAdapter);
To get the list of all values on button click:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (myAdapter != null) {
String[] allValues = myAdapter.getAllValues();
}
}
});
I want to update the items value on click of button
To update the values, you can simply reinitialize and set myAdapter:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myAdapter = new PassengerListUpdate(updatedPassengerList);
listView.setAdapter(myAdapter);
}
});
First, in order to get whole the values inside ListView. You can get number of item in adapter, and then get value one by one by its position.
ArrayList<String> results = new ArrayList<String>();
final int count = mMyAdapter.getCount();
for( int i = 0; i < count; ++i ){
String item = mMyAdapter.getItem(i);
results.add(item);
}
return results;
Second, in order to add or update the value of ListView, in general extending ArrayAdapter is much easier than implementing BaseAdapter, because of ArrayAdapter support add / remove / insert method by itself. For example,
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context) {
super(context, 0);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View vi;
vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView ItemValue;
ItemValue = (TextView) vi.findViewById(android.R.id.text1);
ItemValue.setText(ItemValue[position]);
return (vi);
}
}
And you can updating adapter's value quiet directly,
mMyAdapter.add("Add Item");
mMyAdapter.remove("Remove Item");
mMyAdapter.insert("Insert Item", 0);
After finishing the jobs, you can call adapter's notifyDatSetChanged() method in order to update the UI, and I think this is more efficient way than setting up the new adapter every time whenever data is updated.
mMyAdapter.notifyDataSetChanged();

is it possible to have two TextView in a listView item?

i would like to create a listView, but i want in every listitem to have 2 different textViews and a button..is that possible?
i m using a row xml file for my list items..
public class main extends Activity {
private ListView lv1;
private String lv_arr[]={"a","b","c","d"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.row , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(chania.this, cafe.class);
break;
//...........
}
}
});
}
}
Yes, just wrap them in a Layout, for example LinearLayout.
Here is an example - look for Row Layout.
Create your own custom Adapter which extends Base Adapter.
Try the code below(I have used 7 textview,wrapped in a customized layout xml file):
public class Received_invitationAdapter extends BaseAdapter{
Context ctx_invitation;
public Received_invitationAdapter(Context ctx_invitation)
{
super();
this.ctx_invitation = ctx_invitation;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PartyName.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return PartyName[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
try
{
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
v = li.inflate(R.layout.receivedinvitations, null);
TextView tv_partyname = (TextView)v.findViewById(R.id.tv_receivedinvitation_PartyTitle);
TextView tv_partydate = (TextView)v.findViewById(R.id.tv_receivedinvitation_date);
TextView tv_partytime = (TextView)v.findViewById(R.id.tv_receivedinvitation_time);
TextView tv_partylocation = (TextView)v.findViewById(R.id.tv_receivedinvitation_PartyLocation);
TextView img_chkbox = (TextView)v.findViewById(R.id.img_chkbox_receivedinvitation);
TextView img_inv_accepted = (TextView)v.findViewById(R.id.img_accept_receivedinvitation);
TextView img_inv_rejected = (TextView)v.findViewById(R.id.img_reject_receivedinvitation);
tv_partyname.setText(PartyName[position]);
tv_partydate.setText(PartyDate[position]);
tv_partytime.setText(PartyTime[position]);
tv_partylocation.setText(PartyLocation[position]);
System.out.println(""+img_chkbox.getVisibility());
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
The receivedinvitations.xml file contains the 7 textview defined in the getview method.

Categories

Resources