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.
Related
Hi I can not delete a row from list View i don't know how to delete it i search a lot from Internet i try out many examples but yet i can't solve the issue . when i click the hldr.delete button the complete row of list View delete from the list solution pls.
PlaceOrder Activity
public class PlaceOrder extends Activity {
String [] pIds;
String [] pNames;
String [] pPrizes;
static ListView lv;
ImageView bck;
String [] listImages;
String food_id;
String userdata[];
Intent i;
TextView totalprze;
float tprize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_order);
lv=(ListView)findViewById(R.id.cart_list);
bck=(ImageView) findViewById(R.id.placeholder_bg_btn);
totalprze =(TextView) findViewById(R.id.place_order_price);
i=new Intent(this,Menu.class);
bck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle=new Bundle();
//bundle.putStringArray("images", ListImages);
bundle.putString("food_id", food_id);
bundle.putStringArray("images", listImages);
bundle.putStringArray("userData",userdata);
i.putExtras(bundle);
startActivity(i);
}
});
if(this.getIntent().getExtras()!=null)
{
Bundle b=this.getIntent().getExtras();
pIds=b.getStringArray("pId");
pNames=b.getStringArray("PName");
pPrizes=b.getStringArray("pPrize");
userdata=b.getStringArray("userData");
tprize=b.getFloat("totalprize");
food_id=b.getString("food_id");
listImages=b.getStringArray("images");
String prz=Float.toString(tprize);
totalprze.setText("$"+prz);
lv.setAdapter(new cartAdapter(PlaceOrder.this, pIds, pNames, pPrizes,userdata));
pIds=null;
pNames=null;
pPrizes=null;
}
}
public void onBackPressed() {
pIds=null;
pNames=null;
pPrizes=null;
}
}
CartAdapter
public class cartAdapter extends BaseAdapter{
String [] pIdz;
String [] pNamz;
String [] pPrizs;
String [] userData;
// List<String> imges;
Context context;
// private ShopingCartHelper obj;
private static LayoutInflater inflater=null;
JSONArray jCat = null;
int count=0;
ProgressDialog pDialog;
public cartAdapter(PlaceOrder ctx,
String[] pIds,String[] pNams, String[] pprise,String [] userdata) {
pIdz=pIds;
pNamz=pNams;
context=ctx;
pPrizs=pprise;
userData=userdata;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
if(pIdz==null){
Toast.makeText(context, "There is issue with net connection.", Toast.LENGTH_LONG).show();
//Intent i=new Intent(context,WelcomeActivity.class);
//context.startActivity(i);
return count ;
}else{
return pIdz.length;
}
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class holder{
TextView pid;
TextView pname;
TextView pprise;
Button delete;
ListView lv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final holder hldr=new holder();
View rowView = null;
Bitmap bitmap = null;
rowView = inflater.inflate(R.layout.place_order_item_list, null);
hldr.pid=(TextView) rowView.findViewById(R.id.item_id);
hldr.pname=(TextView) rowView.findViewById(R.id.item_name);
hldr.pprise=(TextView) rowView.findViewById(R.id.item_price);
hldr.delete=(Button) rowView.findViewById(R.id.delete);
hldr.pid.setText(pIdz[position]);
hldr.pname.setText(pNamz[position]);
hldr.pprise.setText(pPrizs[position]);
/* convertView.setTag(hldr);
hldr.delete.setTag(hldr);*/
//
// Picasso.with(context).load(imgs[position]).into(hldr.img);
hldr.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// rowView.remove(position); //removing from your List
//Toast.makeText(context, "Delete",Toast.LENGTH_LONG).show();
int pid=Integer.parseInt(hldr.pid.getText().toString());
//Toast.makeText(context, "pid"+pid, Toast.LENGTH_LONG).show();
new ShopingCartHelper(context).delProduct(pid);
//PlaceOrder.lv.removeViewAt(position);
notifyDataSetChanged();
}
});
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// // TODO Auto-generated method stub
// cartAdapter.this.pIdz.remove[position];
// Toast.makeText(context, "hi",Toast.LENGTH_LONG).show();
}
});
return rowView;
// TODO Auto-generated method stub
}
}
You can't remove anything from an array - they're always fixed length. Once you've created an array of length 3, that array will always have length 3. And if you want to delete from the listview the array which you are used should be dynamic(Adding & Deleting). So make the pIdz; pNamz;pPrizs;userData into some Modal Object, and prepare the list of Modal Objects and pass it to adapter and make the life easier
You'd be better off with a List, e.g. an ArrayList:
hldr.delete.setTag(position);
hldr.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (int)v.getTag();
list.remove(position);
notifyDataSetChanged();
}
});
First.
You should make a object that stores all your different strings instead of using multiple arrays for each value.
Ej:
class MyClass
{
String id;
String name
String price;
public MyClass(String id, String name, String price)
{
this.id = id;
this.name = name;
this.price = price;
}
}
That way you wouldnt have to delete the position of each of the arrays.
And finally to answer your question, just delete the value of position you want to delete for each of the arrays.
Since they are Array and not ArrayList you will need to recreate it with the correct new dimension (Note it will be much easier with ArrayList).
Then just call notifyDataSetChanged() on your Adapter class.
Hope this helps.
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.
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 ;)
I am new to android development. In my android activity I have a GridView and a Button. The GridView adapter class is displaying a set of icons in the GridView. What I want is, if the Button in the activity is clicked, the set of icons in the GridView should be replaced with another set of icons. And every Button click in the activity should switch the two sets of icons in the GridView.
So, how to do this using notifyDatasetChanged()? Please Help.
GridView Adapter class:
public class CustomAdapter extends BaseAdapter{
boolean imageSetChange = false;
// set 1
public Integer[] mThumbPics = {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15, R.drawable.pic16,
R.drawable.pic17, R.drawable.pic18,
R.drawable.pic19, R.drawable.pic20,
R.drawable.pic21
};
//set 2
public Integer[] mThumbEng = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
private Context mContext;
View MyView;
ImageView imageView;
public CustomAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbEng.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbEng[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
if(imageSetChange==false)
{if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbEng[position]);
return MyView;
}
else{
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbUrdu[position]);
return MyView;
}
}
/** public void changeImages(boolean change){
this.imageSetChange = change;
cda.notifyDataSetChanged();
}
**/
}
Button onclick listener:
Button lang_change_btn = (Button) findViewById(R.id.button1);
lang_change_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
//cda.changeImages(true);
}
});
I am having a grid view and I have to add search functionality in the action bar.So that whatever i type to search,the items should come whatever i searched.Grid view is having image and text.I stuck in this very badly.I dont know how to implement it.I gone through android guide but I unable to get it.
Please suggest some easy way to do it.I need search like in watsaap application.
Any suggestion is appreciated.Thanks.
Here is my gridview code.
public class FragmentDeals extends Fragment implements Checkable{
private boolean mChecked;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragmentdeal,
(ViewGroup)
findViewById(R.id.layoutdeal));
//View view = inflater.inflate(R.layout.griddeal,null);
final GridView mGrid = (GridView) layout.findViewById(R.id.GridDeal);
mGrid.setAdapter(new DealAdapter());
mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
//listView.setLongClickable(false);
int selectCount = mGrid.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount +"items selected");
break;
}
}
});
return layout;
// TODO Auto-generated method stub
}
#Override
public boolean isChecked() {
// TODO Auto-generated method stub
return mChecked;
}
#SuppressWarnings("deprecation")
#Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
mChecked = checked;
layout.setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.bground) : null);
}
#Override
public void toggle() {
// TODO Auto-generated method stub
setChecked(!mChecked);
}
}
private class DealAdapter extends BaseAdapter {
#Override
public int getCount() {
return mThumbIds1.length;
}
#Override
public Object getItem(int position) {
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) {
//CheckableLayout l;
View myView = convertView;
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.griddealitems, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_deal_image);
iv.setImageResource(mThumbIds1[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_deal_text);
tv.setText(names1[position] );
return myView;
}
private Integer[] mThumbIds1= {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
private String[] names1={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
}
I have never tried for search functionality with gridview. But i have done the same for list view. Please have a look on the following link to implement search functionality for listview
Search functionality with list view
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/