Hai I need help. How can I save image that choosen from gridView (on the action bar) to device gallery .
Here my code :
Gallery activty
public class GalleryActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(GalleryActivity.this, ViewImageActivity.class);
//int item =(int) parent.getSelectedItemPosition();
intent.putExtra("id", position);
startActivity(intent);
// Toast.makeText(GalleryActivity.this,"" + position,Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(GalleryActivity context) {
mContext = context;
}
public ImageAdapter() {
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(400, 400));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.car,
R.drawable.wolf,
R.drawable.dolphin,
R.drawable.cat,
R.drawable.car,
R.drawable.car,
};
}
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
View image activity
public class ViewImageActivity extends GalleryActivity {
ImageAdapter imageAdapter = new ImageAdapter(this);
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_image);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
Related
So.. I stayed up all night and finally got XSharedPreferences to work with my module. But today when I tried to add other elements such as a Navigation Drawer, and have the preference page only show when tapped, it stopped working.
This is the MainActivity code that has been working just fine.
Working:
Code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
addPreferencesFromResource(R.xml.prefs);
}
}
}
Below is the code where it stopped working. I tried back tracking and only with the code above does it work.
Not working:
Code:
public class MainActivity extends AppCompatActivity implements NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.fragment_drawer);
// Set up the drawer.
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
// populate the navigation drawer
mNavigationDrawerFragment.setUserData("John Doe", "[email protected]", BitmapFactory.decodeResource(getResources(), R.drawable.avatar));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch (position) {
case 0: //search//todo
break;
case 1: //Minor theme fixes
fragment = getFragmentManager().findFragmentByTag(PrefsFragment.TAG);
if (fragment == null) {
fragment = new PrefsFragment();
}
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment, PrefsFragment.TAG)
.addToBackStack(null)
.commit();
break;
case 2: //Full themed apps
fragment = getFragmentManager().findFragmentByTag(PrefsFragment2.TAG_2);
if (fragment == null) {
fragment = new PrefsFragment2();
}
android.app.FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction()
.replace(R.id.container, fragment, PrefsFragment2.TAG_2)
.addToBackStack(null)
.commit();
break;
case 3: //settings //todo
break;
}
}
@Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
PrefsFragment fragment = new PrefsFragment();
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PrefsFragment extends PreferenceFragment {
private final static String TAG = "PrefsFragment";
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
addPreferencesFromResource(R.xml.prefs);
}
}
public static class PrefsFragment2 extends PreferenceFragment {
private final static String TAG_2 = "PrefsFragment2";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs2);
}
}
}
93Akkord said:
So.. I stayed up all night and finally got XSharedPreferences to work with my module. But today when I tried to add other elements such as a Navigation Drawer, and have the preference page only show when tapped, it stopped working.
This is the MainActivity code that has been working just fine.
Working:
Code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
addPreferencesFromResource(R.xml.prefs);
}
}
}
Below is the code where it stopped working. I tried back tracking and only with the code above does it work.
Not working:
Code:
public class MainActivity extends AppCompatActivity implements NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.fragment_drawer);
// Set up the drawer.
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
// populate the navigation drawer
mNavigationDrawerFragment.setUserData("John Doe", "[email protected]", BitmapFactory.decodeResource(getResources(), R.drawable.avatar));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch (position) {
case 0: //search//todo
break;
case 1: //Minor theme fixes
fragment = getFragmentManager().findFragmentByTag(PrefsFragment.TAG);
if (fragment == null) {
fragment = new PrefsFragment();
}
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment, PrefsFragment.TAG)
.addToBackStack(null)
.commit();
break;
case 2: //Full themed apps
fragment = getFragmentManager().findFragmentByTag(PrefsFragment2.TAG_2);
if (fragment == null) {
fragment = new PrefsFragment2();
}
android.app.FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction()
.replace(R.id.container, fragment, PrefsFragment2.TAG_2)
.addToBackStack(null)
.commit();
break;
case 3: //settings //todo
break;
}
}
@Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
PrefsFragment fragment = new PrefsFragment();
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PrefsFragment extends PreferenceFragment {
private final static String TAG = "PrefsFragment";
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesMode(MODE_WORLD_READABLE);
addPreferencesFromResource(R.xml.prefs);
}
}
public static class PrefsFragment2 extends PreferenceFragment {
private final static String TAG_2 = "PrefsFragment2";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs2);
}
}
}
Click to expand...
Click to collapse
Well.. seems like moving the code that worked to its own activity and the following to the mainactivities oncreate has done the trick.
Code:
pref = getSharedPreferences(getPackageName() + "_preferences", MODE_WORLD_READABLE);
I am unable to get XSharedPreferences to work and the standard shared preferences work perfectly. I create a shared preferences file elsewhere in the application. The file is in the shared_pref folder and I can read the file which is perfectly OK.
However, I cannot make the XSharedPreferences work, I. e. I have the opposite problem.
Therefore, can I ask you to provide the code or a guidance how to make the XSharedPreferences work, so I can read the file inside of the Xposed module? In case you do not want to publish anything in this topic, can you please, send a message?
Hi. Can you link to some simple tutorial or other help for Drawer navigation with navigation toggle. I plan to use lot of fragments.
I have implemented Navigation drawer. But when I open fragment and add back arrow when fragment is open instead "navigation hamburger icon", it doesn't go back to starting page but instead open drawer again. Here is the code I mostly got from tutorial,but want to move on:
MainActivity:
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
toolbar=(Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
final NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout),toolbar);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,sports);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.closeDrawers();
}
});
}
protected void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void selectItem(int position)
{
switch (position) {
case 0:
setFragment(new SoccerFragment());
break;
}
}
and for navigation fragment:
Code:
public NavigationDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
if (savedInstanceState!=null)
{
mFromSavedInstanceState=true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}
public void setUp(int fragmentId,DrawerLayout drawerLayout,Toolbar toolbar) {
//containerView=getActivity().findViewById(fragmentId);
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close)
{
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!mUserLearnedDrawer)
{
mUserLearnedDrawer=true;
saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
//if (mUserLearnedDrawer && mFromSavedInstanceState)
//{
// mDrawerLayout.openDrawer(containerView);
//}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void saveToPreferences(Context context,String preferenceName,String preferenceValue)
{
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(preferenceName, preferenceValue);
editor.apply(); //or commit()
}
public static String readFromPreferences(Context context,String preferenceName,String defaultValue)
{
SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(preferenceName,defaultValue);
}
}
is it because of many operations executing from main UI ?
In my app i just have a splash screen and a main activity. In the main thread i have three EditText boxes and a spinner with a string array. On clicking the Button, input from three EditText and spinner selection is posted to my mysql database. For the button click network operation, i used Volley since its east and i dont have to use AsyncTask which am not familiar with.
Apart from this, on entering the main UI .. app first check for network connectivity using ConnectivityManager class. After onClick app checks for empty/invalid imputs using TextUtils.
Now the problem is that when i run my app, its very slow and taking upto 65mb of RAM. IS something wrong with my code. Should i run something else as AsynTask ? Can someone check my code and refine it .. thank you
SplashActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
int SPLASH_TIME_OUT = 5000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Click to expand...
Click to collapse
MainActivity.java
Code:
public class MainActivity extends Activity {
EditText name, phonenumber, address;
Button insert;
RequestQueue requestQueue;
Spinner spinner;
String insertUrl = "localhost";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner s = (Spinner) findViewById(R.id.spinner);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/* CHECK INTERNET CONNECTION */
boolean mobileNwInfo;
ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
try { mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); }
catch (NullPointerException e) { mobileNwInfo = false; }
if (!mobileNwInfo) {
Toast.makeText(this, "No Network, please check your connection. ", Toast.LENGTH_LONG).show();
}
/* CHECK INTERNET CONNECTION PROCEDURE DONE */
name = (EditText) findViewById(R.id.editText);
phonenumber= (EditText) findViewById(R.id.editText2);
address = (EditText) findViewById(R.id.editText3);
insert = (Button) findViewById(R.id.insert);
requestQueue = Volley.newRequestQueue(getApplicationContext());
spinner = (Spinner) findViewById(R.id.spinner);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* CHECK EMPTY STRING */
EditText txtUserName = (EditText) findViewById(R.id.editText);
EditText txtUserAddress = (EditText) findViewById(R.id.editText3);
EditText txtUserPhone = (EditText) findViewById(R.id.editText2);
String strUserName = name.getText().toString();
String strUserAddress = address.getText().toString();
String strUserPhone = phonenumber.getText().toString();
if(TextUtils.isEmpty(strUserName)) {
txtUserName.setError("You can't leave this empty.");
return;
}
if(TextUtils.isEmpty(strUserPhone)) {
txtUserPhone.setError("You can't leave this empty.");
return;
}
if(TextUtils.isEmpty(strUserPhone) || strUserPhone.length() < 10) {
txtUserPhone.setError("Enter a valid phone number.");
return;
}
if(TextUtils.isEmpty(strUserAddress)) {
txtUserAddress.setError("You can't leave this empty.");
return;
}
/* LOADING PROCESS DIALOG */
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Booking Service ....");
pd.show();
/* REQUEST RESPONSE/ERROR */
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pd.hide();
System.out.println(response);
name.setText("");
phonenumber.setText("");
address.setText("");
Toast.makeText(getApplicationContext(), "Service successfully booked !!", Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.hide();
Toast.makeText(getApplicationContext(), "Error: Please try again later.", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<>();
parameters.put("name", name.getText().toString());
parameters.put("phonenumber", phonenumber.getText().toString());
parameters.put("address", address.getText().toString());
parameters.put("service", spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString());
return parameters;
}
};
requestQueue.add(request);
}
});
}
}
Hey everyone,
I am working on a schoolproject and I want to make an app for that, but haven't coded any app before, so I recently started on it.
Right now I have a listview working with a custom arrayadapter with a custom class.
However, whenever I reload the app, it doesn't change the textview to the name of my custom class but just stays at "Textview"
This is my code:
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LockManager lockManager = new LockManager();
lockManager.AddNewLock("Lock1");
lockManager.AddNewLock("Testielock2");
ListView listView = (ListView) findViewById(R.id.LockListView);
listView.setAdapter(new MyListAdapter(this, R.layout.listitem2, lockManager.Locks));
}
private class MyListAdapter extends ArrayAdapter<Lock>{
private int _layout;
public MyListAdapter(Context context, int resource, List<Lock> objects) {
super(context, resource, objects);
_layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Viewholder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(_layout, parent, false);
Viewholder viewholder = new Viewholder();
viewholder._image = (ImageView) convertView.findViewById(R.id.list_item_picture);
viewholder._text = (TextView) convertView.findViewById(R.id.list_item_text);
viewholder._switch = (Switch) convertView.findViewById(R.id.list_item_switch);
viewholder._switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getItem(position).Toggle();
Toast.makeText(getContext(), "Clicked switch for " + getItem(position).Name, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewholder);
}
else
{
mainViewHolder = (Viewholder) convertView.getTag();
Lock lock = getItem(position);
mainViewHolder._text.setText(lock.Name);
}
return convertView;
}
}
public class Viewholder{
ImageView _image;
TextView _text;
Switch _switch;
}
Thank you in advance for your help,
Patrick de Mooij
Question about custom arrayadapter
http://stackoverflow.com/questions/36862968/android-custom-arrayadapter-with-custom-object
Hello !
I'm doing a small personal project with slides and all that, i'm having trouble creating a button with 3 actions depending on witch slide i'm on. I know how to put a different text in my 3 different slides but can't manage to make my buttons work.
Please help !
Code:
public class SliderAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
Button slide_btn;
public SliderAdapter(Context context) {
this.context = context;
}
public int[] slide_images = {
R.drawable.small,
R.drawable.medium,
R.drawable.big
};
public String[] slide_headings = {
"*****",
"OKAY",
"WARRIOR"
};
public String[] slide_descs = {
"tutu",
"toto",
"tata"
};
public Button[] getSlide_btn = {
//action1,
//action2,
//action3
};
@Override
public int getCount() {
return slide_headings.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == (RelativeLayout) o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
container.addView(view);
return view;
}
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
I'll take any advice ! Thx !