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/
Related
I want to delete a particular row from my listview generated by a cursoradapter and also from my database.
In my database the _id column is primary key and also autoincrement so if i delete a particular row its id is deleted so will getposition() work in this case.I want only that particular row to be deleted.
public class DataViewer extends Fragment{
static ListView listdata;
DataAdapter data;
Cursor cursor;
DataCursorAdapter datacursor;
public DataViewer() {
// TODO Auto-generated constructor stub
}
public void update(){
cursor=data.fetchdata();
datacursor.swapCursor(cursor);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootview =inflater.inflate(R.layout.data_fragment, container, false);
listdata=(ListView) rootview.findViewById(R.id.listdata);
data=new DataAdapter(getActivity());
cursor=data.fetchdata();
datacursor=new DataCursorAdapter(getActivity(), cursor);
listdata.setAdapter(datacursor);
registerForContextMenu(listdata);
return rootview;
}
class DataCursorAdapter extends CursorAdapter{
public DataCursorAdapter(Context context, Cursor c) {
super(context, c,0);
// TODO Auto-generated constructor stub
}
#Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
TextView time=(TextView) arg0.findViewById(R.id.texttime);
TextView descriptiontext=(TextView) arg0.findViewById(R.id.textdescription);
String timer=arg2.getString(arg2.getColumnIndexOrThrow("date"));
time.setText(timer);
String description=
arg2.getString(arg2.getColumnIndexOrThrow("description"));
descriptiontext.setText(description);
String mood=arg2.getString(arg2.getColumnIndexOrThrow("mood"));
int i=arg2.getInt(0);
Log.d("The value of id is", ""+i);
ImageView image=(ImageView) arg0.findViewById(R.id.dataimage);
switch (mood) {
case "Excited":
image.setImageResource(R.drawable.excited);
break;
case "Happy":
image.setImageResource(R.drawable.happy);
break;
case "Lonely":
image.setImageResource(R.drawable.lonely);
break;
default:
image.setImageResource(R.drawable.others);
break;
}
}
#Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return
LayoutInflater.from(arg0).inflate(R.layout.data_row, arg2,false);
}}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, R.id.menudelete, 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menudelete:
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
I think something like the following should work but I haven't tested it...
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menudelete:
deleteItem(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
private void deleteItem(int position) {
Cursor c = dataCursor.getCursor();
c.moveToPosition(position);
int databaseId = c.getInt(c.getColumnIndex("_id"));
// Use databaseId to delete the row from the database...
// ...then re-query the database to update the Cursor...
// ...and call notifyDatasetChanged() on the Adapter.
}
So far, I have managed to follow this tutorial and run it successfully. But what I wanna do now is to actually delete selected items on my listview. This is the code I'm using:
private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"};
private SelectionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new SelectionAdapter(this,
R.layout.row_list_item, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
mAdapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
mode.finish();
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");
}
});
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return false;
}
});
}
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
Question is how do I delete the selected item/s? I can't figure it out and if anyone of you knows this and can help, I'd gladly appreciate it. Thanks.
In your code instead of using string [] data you can use ArrayList<String> data since it is dynamically sized array where the object is actually removed and the list (array) size is adjusted accordingly. With this change you can use following method.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
List<Integer> keyList = new ArrayList<Integer>(noteAdptr.getCurrentCheckedPosition());
Collections.sort(keyList, new Comparator<Integer>() {
#Override
public int compare(Integer lhs, Integer rhs) {
return lhs.compareTo(rhs);
}
});
Collections.reverse(keyList);
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
for (Integer i:keyList){
Log.e("", "items selected is : "+i);
data.remove((int)i);
}
mAdapter .notifyDataSetChanged();
mAdapter.clearSelection();
mode.finish();
}
return true;
}
Hope this helps
You can set a OnLongClickListener on the items in the ListView that just gets the item that is long pressed.
Afterwards you can reference/remove that object in your OnActionItemClicked.
I am not sure if you have already solved your problem, but here is what worked for me:
Create a private (or public) variable in your class: private int selectedPosition;
In your onItemLongClick() method (as far as I see that's the one, where you are selecting an item), put:
selectedPosition = position;
in OnActionItemClicked() you can get your adapter and do this:
MyAdapter adapter = (MyAdapter) this.getAdapter();
adapter.remove(adapter.getItem(selectedPosition);
adapter.notifyDataSetChanged();
That way you passed the position of the selected item to your adapter.
Hope that helps!
You have to code the functionality yourself: in the onActionItemClicked, you have to code the logic that will erase selected items from your ArrayList.
Don't forget to call notifyDatasetChanged() on your adapter after that... ;-)
So far this is what I've tried:
private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"};
private SelectionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new SelectionAdapter(this,
R.layout.row_list_item, R.id.textView1, data);
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
mAdapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
mAdapter.setNewSelection(position, checked);
} else {
nr--;
mAdapter.removeSelection(position);
}
mode.setTitle(nr + " selected");
}
});
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
return false;
}
});
}
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
But I need to know how to get the selected items and put it inside OnActionItemClicked like what I've tried here:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
I commented out mAdapter.removeSelection() because I don't know what to put inside.
Any ideas? help is pretty much appreciated. Thanks.
Their are multiple things you could do.. The easiest way i think is as follows:
It doesn't work because the onActionItemClicked expects one item. But you select multiple items.
The easiest way is to create a loop in which you handle the normal procedure the onActionItemClicked expects
ListView mListView = getListView();
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_delete:
nr = 0;
Long[] longConverts = new Long(mListView.getCheckedItemIds());
private List<Long> itemids = new ArrayList<Long>(Arrays.asList(longConverts));
for(int i=0;i<itemids.size();i++){
mListView.remove(itemids.get(i));
mAdapter.clearSelection();
// TODO app mAdapter.removeSelection(position);
mode.finish();
}
return true;
}
I think this should work.. It retrieves the selected items first from the ListView then put them in a array and loops over that array to remove the items one by one..
I hope this works for you
I am making an application in which I am having three tabs and respective fragment to that.But now I want to add one more tab but when I am adding it there is no issue but when i run my app it throws nullpointerexception.I dont why it is happening so.
Please tell me where I m wrong.
This my Activity in which all tab is there:
public class MainActivity extends Activity {
RelativeLayout rl;
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
ActionBar.Tab TabOrder , TabCart,TabHistory;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getOverflowMenu();
rl = (RelativeLayout) findViewById(R.id.mainLayout);
//fragMentTra = getFragmentManager().beginTransaction();
ActionBar actionbar = getActionBar();
actionbar.setTitle("Select To Order");
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// bar.addTab(bar.newTab().setText("ORDER").setTabListener((TabListener) this));
// bar.addTab(bar.newTab().setText("CART").setTabListener(this));
// bar.addTab(bar.newTab().setText("HISTORY").setTabListener(this));
//TabOrder = actionbar.newTab().setText("DEAL");
TabOrder = actionbar.newTab().setText("ORDER");
TabCart = actionbar.newTab().setText("CART");
TabHistory = actionbar.newTab().setText("HISTORY");
//Fragment FragmentDeal = new FragmentDeal();
Fragment FragmentOrder = new FragmentOrder();
Fragment FragmentCart = new FragmentCart();
Fragment FragmentHistory = new FragmentHistory();
//TabOrder.setTabListener(new MyTablistenerClass(FragmentDeal));
TabOrder.setTabListener(new MyTablistenerClass(FragmentOrder));
TabCart.setTabListener(new MyTablistenerClass(FragmentCart));
TabHistory.setTabListener(new MyTablistenerClass(FragmentHistory));
//actionbar.addTab(TabDeal);
actionbar.addTab(TabOrder);
actionbar.addTab(TabCart);
actionbar.addTab(TabHistory);
}
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
FragmentOrder
#SuppressLint({ "ValidFragment", "NewApi" })
public class FragmentOrder extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.g, null);
View view = inflater.inflate(R.layout.gridview,null);
final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
listView.setAdapter(new OrderAdapter());
//listView.setSelection(setselected,true);
listView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
listView.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 = listView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount +"items selected");
break;
}
}
});
return view;
}
}
OrderAdapter
private class OrderAdapter extends BaseAdapter {
#Override
public int getCount() {
return mThumbIds.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.grid_items_ontap, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
tv.setText(names[position] );
return myView;
}
}
private Integer[] mThumbIds = {
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[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
FragmentHistory
#SuppressLint("ValidFragment")
public class FragmentHistory extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_fragmenthistory,
(ViewGroup)
findViewById(R.id.layout_root_history));
ListView lv = (ListView) layout.findViewById(R.id.listViewHistory);
lv.setAdapter(new HistoryListViewAdapter(MainActivity.this));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("ORDERID");
dlg.setPositiveButton("REORDER",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton("EDIT AND ORDER", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
ListView listView = new ListView(MainActivity.this);
listView.setAdapter(new DialogListAdapter(MainActivity.this));
dlg.setView(listView);
//((Dialog) dlg).setCanceledOnTouchOutside(true);
// show it
dlg.show();
}
});
return layout;
}
}
FragmentCart
#SuppressLint("ValidFragment")
public class FragmentCart extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.activity_fragmentcart,
(ViewGroup)
findViewById(R.id.layout_root_cart));
ListView lv = (ListView) layout.findViewById(R.id.listViewCart);
lv.setAdapter(new CartListViewAdapter(MainActivity.this));
//lv.invalidateViews();
return layout;
}
}
FragmentDeal
#SuppressLint("ValidFragment")
public class FragmentDeal extends Fragment{
#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 listView = (GridView) layout.findViewById(R.id.GridDeal);
listView.setAdapter(new DealAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return layout;
// TODO Auto-generated method stub
}
}
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
};
private String[] names1={"ab","cd","ef","gh"};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_settings:
Intent intentForSettings = new Intent(MainActivity.this, SettingsMenu.class);
startActivity(intentForSettings);
return true;
case R.id.action_info:
Intent intentForInformation = new Intent(MainActivity.this,InformationMenu.class);
startActivity(intentForInformation);
return true;
case R.id.action_ContactUs:
Intent dial = new Intent();
String no = "9579839314";
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"+ no));
startActivity(dial);
return true;
}
return false;
}
public class MyTablistenerClass implements android.app.ActionBar.TabListener {
Fragment fragment1;
#SuppressLint("NewApi")
public MyTablistenerClass(Fragment fragment){
this.fragment1 = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fragMentTra;
if (tab.getText().equals("DEAL")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
FragmentDeal Fram1 = new FragmentDeal();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram1);
fragMentTra.commit();
}
else if (tab.getText().equals("ORDER")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
FragmentOrder Fram2 = new FragmentOrder();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), Fram2);
fragMentTra.commit();
}
else if(tab.getText().equals("CART")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
FragmentCart fram3 = new FragmentCart();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram3);
fragMentTra.commit();
}
else if(tab.getText().equals("HISTORY")){
try {
rl.removeAllViews();
} catch (Exception e) {
}
FragmentHistory fram4 = new FragmentHistory();
//fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction();
fragMentTra.add(rl.getId(), fram4);
fragMentTra.commit();
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
On adding tabdeal I am facing problem.FragmentDeal is its respective Fragment.If anyone can see where I am wrong tell me.
Thanks.
//TabOrder = actionbar.newTab().setText("DEAL");
TabOrder = actionbar.newTab().setText("ORDER");
You have set both tabs to TabOrder . Set the first as TabDeal.
EDIT:
Of course you also need to initialize the TabDeal here too:
ActionBar.Tab TabOrder , TabCart,TabHistory;
And by the way, usually only class names start with an uppercase letter. This lead to some confusion for me.
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.