How to pass a jpg id between activities Android - android

I have a gridView with imageViews in one Activity and I want to load one of them in other activity on a new ImageView when select it, but i have no results
Activity 1:
public class Level extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
GridView gridview = (GridView) findViewById(R.id.gridviewLevel);
ImageAdapter ia = new ImageAdapter(this);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Level.this, "" + position, Toast.LENGTH_SHORT).show();
initImageCheck( v.getId());
}
});
}
public void initImageCheck(int id){
Intent intentIC = new Intent(this, ImageCheck.class);
intentIC.putExtra("ID", id);
startActivity(intentIC);
}
}
Activity 2:
public class ImageCheck extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_check);
Bundle extras = getIntent().getExtras();
int id = extras.getInt("ID");
ImageView imgView = (ImageView) findViewById(R.id.imageViewCheck);
imgView.setImageResource(id);
}
}
Whats the problem??? Thanks.

v.getId() is the ID of the View, not of the image resource.
Have ImageAdapter also hold the id of resource used per item, and also provide a method like getResourceIdForItem(int position).

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

Listview to another activity with image views?

I have a listview inside a fragment class. When a listview item is clicked another activity is opened. The xml file of that activity has 2 imageviews inside a scroll view. The images change with respect to every listview item clicked. No matter which listview item i click on it shows the same image view. In my case it shows the R.drawable.geet94 no mater which listview item i click.
public class urdufrag1 extends Fragment {
public urdufrag1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "urdufrag1";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
main_content, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);
final int[] menuImage = {R.drawable.tgeet94,
R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
final ListView listView = (ListView) view.findViewById(R.id.GeetListU);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
//intent.putExtra("GeetType",
listView.getItemAtPosition(position).toString());
//intent.putExtra("GeetType", Integer.toString(position));
intent.putExtra("position", Integer.toString(position));
startActivity(intent);
}
});
AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
Second Activity
public class PackageG extends AppCompatActivity {
ImageView img1, img2;
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_g);
img1 = (ImageView) findViewById(R.id.imageView8);
img2 = (ImageView) findViewById(R.id.imageView9);
mAttacher = new PhotoViewAttacher(img1);
mAttacher = new PhotoViewAttacher(img2);
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
switch (position){
case 0: img1.setImageResource(R.drawable.geet94);
img2.setImageResource(R.drawable.geet94_1);
break;
case 1: img1.setImageResource(R.drawable.geet95);
img2.setImageResource(R.drawable.geet95_1);
break;
case 2: img1.setImageResource(R.drawable.geet96);
img2.setImageResource(R.drawable.geet96_1);
break;
default:
img1.setImageResource(R.drawable.carol);
img2.setImageResource(R.drawable.carol);
break;
}
}
mAttacher.update();
}
}
intent.putExtra("position", Integer.toString(position));
This is where you made mistake.
Change this to, and will solve your problem.
intent.putExtra("position", position);
Because in your activity you are expecting an integer but you are passing string position from listview. Change the click listener as per the below code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
intent.putExtra("position", position);
startActivity(intent);
}
});

Title on Action Bar displaying garbage value

I'm trying to display the name of the Listview item on the action bar as it's title on the next activity screen using intents.
Here is my code.
MathematicsDBEntry.java
public class MathematicsDBEntry extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
String newActionBarTitle = i.getStringExtra("Position");
super.onCreate(savedInstanceState);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(newActionBarTitle);
setContentView(R.layout.activity_mathematics_dbentry);
}
}
MathsActivity.java
public class MathsActivity extends ListActivity implements AdapterView.OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths);
// storing string resources into Array
String[] math_data = getResources().getStringArray(R.array.maths_list_data);
ListView listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, math_data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
//Toast.makeText(this, textView.getText().toString() + " " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MathematicsDBEntry.class);
intent.putExtra("Position", textView.toString());
startActivity(intent);
}
}
But all I get is the garbage value shown below.
You need to use the getText() method of TextView to get the data from it.
You need to change:
intent.putExtra("Position", textView.toString());
to:
intent.putExtra("Position", textView.getText().toString());

Layout containing listview, unable to switch to new activity onitemclick

I am having a listview as part of a mainLayout and Onclickevent on listview is not working. Can anybody give me pointer where I am wrong?
Here's the code snippet:
public class HostListActivity extends ListActivity {
public ArrayList<String> deviceList;
static LinearLayout mainLayout;
public ListView hostListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
designLayout();
setContentView(mainLayout);
}
public void designLayout() {
mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
int lHeight= LinearLayout.LayoutParams.FILL_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
AddHostListView(lHeight,lWidth);
}
private void AddHostListView(int lHeight, int lWidth) {
deviceList = new ArrayList<String>();
deviceList.add("abc");
deviceList.add("efg");
deviceList.add("hij");
hostListView = new ListView(this);
hostListView.setId(android.R.id.list);
hostListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, deviceList));
hostListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent myIntent = new Intent(v.getContext(), DirBrowserActivity.class);
startActivity(myIntent);
}
});
mainLayout.addView(hostListView, new LinearLayout.LayoutParams (lHeight,lWidth));
}
// DirBrowserActivity class
public class DirBrowserActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, DirBrowse View");
setContentView(tv);
}
}
You extend ListActivity but you do not use any feature from this class. I have noticed that this correlates with not working OnItemClickListener. Simply changing:
public class HostListActivity extends ListActivity {
to
public class HostListActivity extends Activity {
will help.
You should put this code
registerForContextMenu(getListView());
after setting an adapter to your listview, if you want to add a context menu to your items. If you just want to make smth on an item's click, you need to ovverride this method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
Hope this helps.

How do I select a row in a listview not using ListActivity

Here is the code:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find your views
final ListView listPro = (ListView) findViewById(R.id.listProperty);
DBAdapter db = new DBAdapter(this);
db.open();
final Cursor c = db.getAllProperties();
startManagingCursor(c);
// Create the adapter
MainAdapter adapter = new MainAdapter(this, c);
listPro.setAdapter(adapter);
// here is where I think I should put the code to select the row but I
// haven't been able to figure out how to do it.
// all I need to get out is the row id number.. please help
Button addPropBut = (Button) findViewById(R.id.mainButton);
addPropBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i7 = new Intent(Main.this, Home.class);
startActivity(i7);
}
});
}
}
try this one, hope it will help you out.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
you can get the id number by using Log or System.out.println();
Instead of using Button's setOnClickListener use ListView's setOnItemClickListener.
listPro.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
The id parameter will give you the needed id.
Salil.

Categories

Resources