Start Activity from GridView - android

I create a GridView and I want to start some activities from each button.
The gridView is in a class called Status, the code as follows:
public class Status extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
public static String[] gridViewStrings = {
"Stato",
"Allarmi",
"Impostazioni",
"Numero SIM",
"Dati Impianto",
"Esci",
};
public static int[] gridViewImages = {
R.drawable.chart,
R.drawable.alert,
R.drawable.set,
R.drawable.sim_status,
R.drawable.impianto2,
R.drawable.close,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(getApplicationContext(), Status.class);
i.putExtra("id", position);
startActivity(i);
break;
case 1:
finish();
break;
}
}
});
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("ESA MOBILE App");
}
}
And my Adapter class as follows : I want to start activity from first button and to close the activity from second one, but doesn't work.
class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#SuppressLint("InflateParams")
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = convertView;
}
return grid;
}
}

I think you should update your getView() to set the correct click listener. It also seems you have a bug when you recycle views as you don't update them in the adapter (BTW, using a the View Holder pattern would make this code more efficient):
#SuppressLint("InflateParams")
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
} else {
grid = convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
// Here you can set your click listener.
// Another option is to have a OnClickListener array (like you have for the strings and images and use it in a similar way).
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (p) {
case 0:
// Start activity
break;
case 1:
// Close
break;
default:
// Do something else
break;
}
}
});
return grid;
}

I am not sure but i think you are requesting click on the item(not on the ImageView inside the item)
so implement click in adapter after get the id of your parent or you can implement the click on your image in adapter like:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast here
}});

Related

How to implement onClick() method on ImageView

I have dynamically populated ImageViews and Textviews.
Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.
here is my code - my main fragment:
public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
"string1",
"string2",
"string3",
"string4",
"string5",
"string6"
};
public static int[] gridViewImages = {
R.drawable.delivery,
R.drawable.shipping_logs,
R.drawable.meassurement,
R.drawable.takeovers,
R.drawable.settings,
R.drawable.download_data
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, parent, false);
gridView = (GridView) view.findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));
imageView = (ImageView) view.findViewById(R.id.gridview_image);
}
my custom gridview adapter:
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(final int p, final View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
QUESTION: How to implement onClick() on ImageView and switch to fragment and activity?
public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
this.context = context;
this.wrapperOutletList = wrapperOutletList;
this.listener = listener; //---> pass in listener....
}
#Override
public int getCount() {
return wrapperOutletList.size();
}
#Override
public Object getItem(int position) {
return wrapperOutletList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!wrapperOutletList.isEmpty()) {
WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
Outlet outlet = wrapperOutlet.getOutlet();
if (outlet != null) {
if (TextUtils.isEmpty(outlet.getTel())){
viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
} else {
viewHolder.ivCall.setImageResource(R.drawable.icon_call);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);
}
}
}
return convertView;
}
Pass in listener. Give you an example above. code below is where you call the adapter in fragment. My fragment implements View.OnClickListener
lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));
Interface Callback:
public interface LaunchFragmentWithImage{
void launchFragmentWithImage(int imageID);
}
Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.
public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{
#Override //Method from interface we've defined.
public void launchFragmentWithImage(int imageID){
FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
Bundle imageData = new Bundle();
imageData.putInt(imageID, "key");
fragmentToLaunch.setArguments(imageData);
getSupportFragmentManager().beginTransaction() //Call replace not add
.replace(containerID, fragmentToLaunch)
.commit();
}
}
In AdapterClass
imageViewVar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
}
});
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
why are you pointing the grid variable to two separate View objects?
if (convertView == null) {
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(choosedPosition){
case 0:do something;break;
case 1:do something;break;
...
}
}
});
}
It's just an example and can't running.

Replace the text of a button on button click from a GridView with a custom adapter?

I have a GridView which is created via a custom GridViewAdapter which extends a BaseAdapter to create its view. Each cell in the GridView is represented by a button.
Inside of the View getView method, I use a ViewHolder to cache the button.
holder.gridPanel.setText(panel.getNumberPanel());
Then I tried to apply an OnClickListener to this button however the text of the button does not seem to change even though the Toast notification returns the correct value to set the new text of the button to.
holder.gridPanel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, panel.getAlphabetPanel(), Toast.LENGTH_SHORT).show();
holder.gridPanel.setText(panel.getAlphabetPanel());
}
});
What is the correct way of changing the text of a button which has been stored inside of a ViewHolder?
Code:
static class ViewHolder {
Button gridPanel;
}
public class GridViewAdapter extends BaseAdapter {
private Context context;
ArrayList<Panel> panelList;
Panel panel;
public GridViewAdapter(Context context, ArrayList<Panel> panelList){
this.context = context;
this.panelList = panelList;
}
#Override
public int getCount() {
return panelList.size();
}
#Override
public Object getItem(int position) {
return panelList.get(position);
}
#Override
public long getItemId(int position) {
return panelList.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.game_grid_item_layout, parent, false);
holder = new ViewHolder();
holder.gridPanel = (Button) convertView.findViewById(R.id.grid_AlphabetPanel);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
panel = panelList.get(position);
holder.gridPanel.setText(panel.getNumberPanel());
holder.gridPanel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, panel.getAlphabetPanel(), Toast.LENGTH_SHORT).show();
holder.gridPanel.setText(panel.getAlphabetPanel());
}
});
return convertView;
}
// below methods are in a different activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ArrayList<Panel> panelList = new ArrayList<>();
ArrayList<String> alphaList = new ArrayList<>();
ArrayList<String> numberList = new ArrayList<>();
alphaList.add("A");
alphaList.add("B");
alphaList.add("C");
alphaList.add("D");
alphaList.add("E");
numberList.add(Integer.toString(1));
numberList.add(Integer.toString(2));
numberList.add(Integer.toString(3));
numberList.add(Integer.toString(4));
numberList.add(Integer.toString(5));
for(int i = 0; i < 5; i++){
String alp = alphaList.get(i);
String numb = numberList.get(i);
Panel panelObject = new Panel(alp, numb);
panelList.add(panelObject);
}
GridView panelGrid = (GridView) findViewById(R.id.gridView_Panel);
GridViewAdapter gridViewAdapter = new GridViewAdapter(this, panelList);
panelGrid.setAdapter(gridViewAdapter);
}
So I came to an answer if anyone was wondering.
Calling the setOnClickListener when the ViewHolder is being initialised works whereas doing it outside of the loop will not allow the data to be changed.
In code:
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.game_grid_item_layout, parent, false);
holder = new ViewHolder();
holder.gridPanel = (Button) convertView.findViewById(R.id.grid_AlphabetPanel);
holder.gridPanel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.gridPanel.setText(panel.getAlphabetPanel());
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

how to get listview item on button click and save to sqlite

I've four textviews and a button in listview. when user clicks on the button i want to get the value of these four textviews and pass to the database. I'm confuse how to save the data of selected item to sqlite.
public class PlannningListViewAdapter extends BaseAdapter{
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity planActivity;
public PlannningListViewAdapter(Context context,
ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvDocCode = (TextView) convertView.findViewById(R.id.Plan_no);
TextView tvDocName = (TextView) convertView.findViewById(R.id.doc_name);
TextView tvmon = (TextView) convertView.findViewById(R.id.mon);
TextView tvAdr = (TextView) convertView.findViewById(R.id.adr);
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setOnClickListener(ButtonClickListener);
tvDocCode.setText(p.getDocCode());
tvDocName.setText(p.getDocName());
tvmon.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
return convertView;
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
// Log.v(TAG, "item clicked, row %d", position);
}
};
}
You can use setTag()and getTag() method on your button.
Please look at the code below:
You have to change getItem method:
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
Update your getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setTag(position);
...
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = (int)v.getTag();
Planning planning = getItem(position);
yourDatabaseHelper. getRuntimeExceptionDao(Planning.class).createOrUpdate(planning);
}
};
One more thing. You should implement ViewHolder please read about this pattern, and the best solution for you is use RecyclerView, getting position is already implemented there in RecyclerView.ViewHolder.

Android favorite option in custom gridview changes position in scroll

Hi I am working with android . I had created a custom grid view with an imageview to add this item as favorite and when click on that item its background chaged as selected.Now the problem is that I can select all these favorite button and when I scroll down its selector position is changed and when I click on one item other item is automatically get selected. How can i solve this problem please help me. Thanks in advance :)
here is my adapter class
![public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String\[\] web1;
private final int\[\] Imageid;
public CustomGrid(Context c,String\[\] web,int\[\] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web1 = web;
}
#Override
public int getCount() {
return web1.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView button01;
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.grid_single, parent, false);
} else {
v = convertView;
}
TextView text = (TextView)v.findViewById(R.id.grid_text);
text.setText(web1\[position\]);
ImageView image = (ImageView)v.findViewById(R.id.grid_image);
image.setBackgroundResource(Imageid\[position\]);
button01 = (ImageView)v.findViewById(R.id.star);
button01.setOnClickListener(new OnClickListener() {
int button01pos = 0;
public void onClick(View v) {
if (button01pos == 0) {
button01.setImageResource(R.drawable.star);
button01pos = 1;
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.startclicked);
button01pos = 0;
}
}
});
return v;
}
}

Dynamic GridView items in Android

Hey i'm trying to build an app in which the user can input some numbers and these numbers
will be shown in a gridview with custom grid view items. If i use the common gridview items of android it works. But if i use my own layout it doesnt work. Only the first element in
the array will be shown. Does anybody have a clou why this doesn't work?
public class GridViewAppActivity extends Activity {
private ArrayList<String> items;
private GridViewAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<String>();
adapter = new GridViewAdapter(this);
GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.add_button);
final EditText itemEditText = (EditText) findViewById(R.id.number_field);
// Click listener for Button
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
items.add(itemEditText.getText().toString());
itemEditText.setText("");
adapter.notifyDataSetChanged();
}
});
}
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.grid_item_text);
tv.setText(items.get(position));
} else {
v = convertView;
}
return v;
}
}
}
Try this,
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
} else {
v = convertView;
}
TextView tv = (TextView)v.findViewById(R.id.grid_item_text);
tv.setText(items.get(position));
return v;
}

Categories

Resources