Android - Custom Adapter Item Info - android

I have this custom adapter, I added a new item like this;
card = new CardModel(name, email, image1);
card.setOnClickListener(new CardModel.OnClickListener() {
#Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
Toast.makeText(getApplicationContext(), email.toString(), Toast.LENGTH_SHORT).show();
}
});
card.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
#Override
public void onLike() {
Log.i("Swipeable Cards", "I dislike the card");
}
#Override
public void onDislike() {
Log.i("Swipeable Cards", "I like the card");
new sendNewYay().execute(sharedToken, name);
}
});
adapter.add(card);
mCardContainer.setAdapter(adapter);
At the onDislike method, I need to get item name.
in this line :
new sendNewYay().execute(sharedToken, name);
I send the item name, But it doesn't work.
How can I get the item name, in this method?
I have two buttons, one of them for onLike method, another one for onDislike Method. How can I triggered this two method with my button?

I would change how you define the methods in the CardModel.OnCardDimissedListener interface like so:
public interface OnCardDimissedListener {
void onLike(CardModel card);
void onDislike(CardModel card);
}
Whatever is calling these methods should pass the CardModel instance on which the action was performed. Then your listener can get the name like this:
new CardModel.OnCardDimissedListener() {
#Override
public void onLike(CardModel card) {
Log.i("Swipeable Cards", "I dislike the card");
}
#Override
public void onDislike(CardModel card) {
Log.i("Swipeable Cards", "I like the card");
new sendNewYay().execute(sharedToken, card.getName());
}
});

I think Karakuri is right.
You can implement the click event in your custom adapter.
Like this: (I just use BaseAdapter as an example)
class Adapter extends BaseAdapter {
private ArrayList<CardModel> list;
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
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 row;
row = inflater.inflate(R.layout.custom, parent, false);
Button btn1 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// onLike method
}
});
Button btn2 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// onDislike Method and catch the name value
String name = list.get(position).getName;
}
});
return (row);
}
}
And if you want to catch the onClick events in the main activity, you can transfer the CardModelListener into custom adapter. So once user click the button, events will be caught in the main activity.
Like:
public class MyActivity extends Activity {
class CardModelListener implements CardModeInterface {
#Override
public void onLike(CardModel card) {
//onLike method
}
#Override
public void onDisLike(CardModel card) {
//onLike method
}
}
interface CardModeInterface {
onLike(CardModel card);
onDisLike(CardModel card);
}
}
And transfer the instance of CardModelListener to Custom Adapter:
class Adapter extends BaseAdapter {
private CardModelListener listener;
public Adapter(CardModelListener onClickListener) {
this.listener = onClickListener;
}
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
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 row;
row = inflater.inflate(R.layout.custom, parent, false);
Button btn1 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onLike(list.get(position);
}
});
return (row);
}
}
I think this can resolve your first question. Let me know if you need more details.

Related

How to dynamically add listener to button on customAdapter in android

I want to increment/decrement the count and update it to the database when click on button. the problem is button is on adapter, how should i apply an listener to individual button and update the count for particular user on UI as well as in Database.
What I have achieved:
above image shows what i have created. www, aaa, xxx are the users and when I click on the + or - button I want to increment the count and also update in database.
below is my code for this all what I have done:
Admin_Dashbord.java Activity Class
public class Admin_Dashbord extends Activity
{
//TextView a_shop_key,a_shop_name,a_shop_owner,a_shop_ophone,a_shop_address1,a_shop_address2,a_shop_address3,a_e_names;
String key,name,owner,ownerphone,address1,address2,address3,empnames,n_emp1,txtname="",getdata;
int n_emp;
Button submit;
TextView txtv_shopname;
LinearLayout linear;
Customer_Adapter custom;
//ArrayList<No_Of_Emp_Pojo> getdata;
ArrayList<No_Of_Emp_Pojo> newdata=new ArrayList<No_Of_Emp_Pojo>();
ArrayList<No_Of_Emp_Pojo> temp=new ArrayList<No_Of_Emp_Pojo>();
Network_Class net=new Network_Class();
ListView li;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_dashbord);
linear=(LinearLayout)findViewById(R.id.admin_dashbord_layout2);
submit=(Button)findViewById(R.id.dashbord_shop_submit);
li=(ListView)findViewById(R.id.emp_list);
txtv_shopname=(TextView)findViewById(R.id.shopname);
Intent intent=getIntent();
Bundle b=intent.getExtras();
key=b.getString("SHOP_ID");
name=b.getString("SHOP_NAME");
owner=b.getString("OWNER_NAME");
ownerphone=b.getString("OWNER_PHONE");
address1=b.getString("ADDRESS1");
address2=b.getString("ADDRESS2");
address3=b.getString("ADDRESS3");
//empnames=b.getString("EMP_NAMES");
n_emp1=b.getString("N_EMP");
n_emp=Integer.parseInt(b.getString("N_EMP"));
txtv_shopname.setText(name);
newdata=getdetail(key);
System.out.println("This is Admin Dashboard");
custom=new Customer_Adapter(getApplicationContext(), R.layout.shop_listview_item, newdata);
li.setAdapter(custom);
//newdata.clear();
custom.notifyDataSetChanged();
}
public ArrayList<No_Of_Emp_Pojo> getdetail(final String key)
{
Thread th=new Thread(new Runnable()
{
#Override
public void run()
{
getdata=net.n_employees(key);
System.out.println("In Thread first");
temp = no_emp(getdata);
}
});
th.start();
try
{
th.join();
}
catch(Exception e)
{
System.out.println("Thread Jpoin Problem "+e.getMessage());
}
return temp;
}
public ArrayList<No_Of_Emp_Pojo> no_emp(String result)
{
ArrayList<No_Of_Emp_Pojo> emp=new ArrayList<No_Of_Emp_Pojo>();
try
{
JSONArray j_array=new JSONArray(result);
for(int i=0; i<j_array.length(); i++)
{
JSONObject j_object=j_array.getJSONObject(i);
No_Of_Emp_Pojo no=new No_Of_Emp_Pojo();
no.setId(j_object.getInt("emp_id"));
no.setCount(j_object.getInt("count"));
no.setE_name(j_object.getString("emp_name"));
emp.add(no);
}
}
catch(JSONException e)
{
Log.e("Log_tag", "Error Parsing in Area......"+e.toString());
Log.e("Log_Answers","Second Error Parsing in Area......"+result);
}
System.out.println("This is parsing "+emp.toString());
return emp;
}
}
Customer_Adapter.java
public class Customer_Adapter extends ArrayAdapter<No_Of_Emp_Pojo>
{
private int res;
private LayoutInflater inflaters;
Button signal,increment,decrement;
public Customer_Adapter(Context context, int resource, List<No_Of_Emp_Pojo> objects)
{
super(context, resource, objects);
res=resource;
inflaters=LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView=(LinearLayout)inflaters.inflate(res, null);
TextView count=(TextView)convertView.findViewById(R.id.show_waiting);
TextView name=(TextView)convertView.findViewById(R.id.show_emp_name);
signal=(Button)convertView.findViewById(R.id.green_signal);
increment=(Button)convertView.findViewById(R.id.pluse_signal);
decrement=(Button)convertView.findViewById(R.id.minus_signal);
count.setTextColor(Color.parseColor("#000000"));
name.setTextColor(Color.parseColor("#000000"));
No_Of_Emp_Pojo no=getItem(position);
count.setText(Integer.toString(no.getCount()));
name.setText(no.getE_name());
increment.setOnClickListener(no.addlistner);
decrement.setOnClickListener(no.sublistner);
return convertView;
}
}
No_Of_Emp_Pojo.java
public class No_Of_Emp_Pojo
{
int count,id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
String e_name;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getE_name() {
return e_name;
}
public void setE_name(String e_name) {
this.e_name = e_name;
}
public OnClickListener addlistner=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Add Count is "+ getId());
}
};
public OnClickListener sublistner=new OnClickListener()
{
#Override
public void onClick(View v)
{
System.out.println("Minus Count is "+getId());
}
};
}
I surfed and I found, from that I wrote the OnClickListener in No_Of_Emp_Pojo class when I click on button it prints id But i am not able to update it on UI as well as in database because it gives me the listener in pojo so what to do to get it on Admin_Dashbord.java activity so that it will become more easy to update the count on adapter as well as the value in database.
Instead of POJO write inside getView method of the adapter
Button button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(context,"your msg", Toast.LENGTH_LONG)
}
});
basically adapter sets the view for each element of the list differently so for setting listener on the button will also work dynamically as required for reference check.
Add click listener on button of item in getView:
increment.setOnClickListener(this);
decrement.setOnClickListener(this);
then add tag of that object on these button in getView:
increment.setTag(no); // "no" is your cuttent object(mode)
decrement.setTag(no); // "no" is your cuttent object(mode)
Now implement Onclick method in adapter:
public void onClick(View v) {
switch(v.getId()) {
case R.id.pluse_signal:
No_Of_Emp_Pojo selectedItem = (No_Of_Emp_Pojo)v.getTag();
//now you can update this item in db or in any place.
break;
case R.id.minus_signal:
No_Of_Emp_Pojo selectedItem1 = (No_Of_Emp_Pojo)v.getTag();
//now you can update this item in db or in any place.
break;
}
}
And this click listnder will be called for every item of listview. And you can get model related to every item.
In the Adapter, getView method, write onClickListener for specific buttons. You should maintain proper array/pojo files to save the numbers of clicks in the textview.
Write you listener on Custom_Adapter.java not on No_Of_Emp_Pojo.java
Try this code on Customer_Adapter.java
public class Customer_Adapter extends ArrayAdapter<No_Of_Emp_Pojo>
{
private int res;
private LayoutInflater inflaters;
Button signal,increment,decrement;
public Customer_Adapter(Context context, int resource, List<No_Of_Emp_Pojo> objects)
{
super(context, resource, objects);
res=resource;
inflaters=LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView=(LinearLayout)inflaters.inflate(res, null);
TextView count=(TextView)convertView.findViewById(R.id.show_waiting);
TextView name=(TextView)convertView.findViewById(R.id.show_emp_name);
signal=(Button)convertView.findViewById(R.id.green_signal);
increment=(Button)convertView.findViewById(R.id.pluse_signal);
decrement=(Button)convertView.findViewById(R.id.minus_signal);
count.setTextColor(Color.parseColor("#000000"));
name.setTextColor(Color.parseColor("#000000"));
No_Of_Emp_Pojo no=getItem(position);
count.setText(Integer.toString(no.getCount()));
name.setText(no.getE_name());
increment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//your incrementation code here
}
});
decrement.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//your decrementation code here
}
});
return convertView;
}
}

Android dosen't show me keybord. I use Adapter with Edittext indise AlertDialog

AlertDialog works fine, its shows me my adpater, but when I click on the EditText inside my adapter, keybord dosen't open.
Besides that:
Log.d(LOG_TAG, "item:"+item)
inside onClickListener dosen't work.
public class AddProduct extends Activity implements OnClickListener {
String LOG_TAG;
Button fillFields;
ArrayList<String> someArray;
BoxAdapter boxAdapter;
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newproduct);
fillFields=(Button)findViewById(R.id.fillFields);
fillFields.setOnClickListener(this);
someArray=new ArrayList<String>();
boxAdapter = new BoxAdapter(this, someArray);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.fillFields:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Поля товара");
alert.setAdapter(boxAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.d(LOG_TAG, "item:"+item);
}});
alert.create().show();
break;
}
}
}
And adapter:
public class BoxAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<String> objects;
OnEditorActionListener myEditorActionListener;
String LOG_TAG;
EditText editText;
BoxAdapter(Context context, ArrayList<String> getFields){
ctx = context;
objects = getFields;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// используем созданные, но не используемые view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.addproduct_item, parent, false);
}
((TextView) view.findViewById(R.id.addProductTextView)).setText(objects.get(position));
editText = (EditText) view.findViewById(R.id.addProductEditText);
return view;
}
}
So, what I am doing wrong? Thank You.
Well, you must show all code and not partial, and better if show your logcat,
But, to force to show keyboard in a dialog, you could use it (i do not know if it is the best way, but, its one way):
YourDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
It depends how you are getting your dialog, but, if it using fragmentDialog or the method (deprecated) onCreateDialog, you should declare your dialog var as final.
Regards.

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.

dynamically giving data to list

Im new to android im desingnig one app description is...
i have kept one edit text and one button whenevr i click on the button i have to store that value in a list... i tried like this
I didnt get where theerror is....
public class Dynamic extends Activity {
ArrayList<Getters> x=new ArrayList<Getters>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.nameok);
final EditText ed=(EditText)findViewById(R.id.name);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=ed.getText().toString();
Getters y=new Getters(name);
x.add(y);
ListView lv= (ListView)findViewById(R.id.listView1);
Demo demo=new Demo();
lv.setAdapter(demo);
}
});
}
public class Demo extends BaseAdapter
{
#Override
public int getCount() {
// TODO Auto-generated method stub
return x.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#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 = convertView;
if (v == null) {
LayoutInflater vi = LayoutInflater.from(parent.getContext());
v = vi.inflate(R.layout.forlist, null);
}
//View v=LayoutInflater.from(getApplicationContext()).inflate(R.layout.forlist, null);
TextView listname=(TextView)findViewById(R.id.listname);
listname.setText(x.get(position).name);
return v;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
getters..
public class Getters {
public Getters(String name) {
this.name = name;
}
String name;
public String getName() {
Log.v("name---",""+name);
return name;
}
}
please First see this and then implement your code so its easy to implement.
Edit :
http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Hope you will get idea about how to manage list data dynamically.
Edit
Just write below code for add event in List view
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=ed.getText().toString();
your List_view_obj.add(name);
//call adapter class and pass that string
your List_view_obj.setAdapter(your adapter's obj);
your_adapter_obj.notifyDataSetChanged();
}
});
Hope you got it.

Categories

Resources