Reputation: 1583
I have made a Custom List View in android. And i want that Whenever list view click event called it goes into another activity and the data which is on that list will be displayed on new activity. Below code is my Like MainActivity(Here named as ListofWorkers) which have also Layout file.
package com.example.dispatchmedemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import java.util.ArrayList;
import java.util.List;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.dispatchmedemo.CustomListViewAdapter;
import com.example.dispatchmedemo.RowItem;
public class ListofWorkers extends Activity {
public static final String[] name = new String[] { "ABC",
"YZA", "ASDSA", "ASD", "ASD", "SDFS" };
public static final String[] landmarks = new String[] {
"AFDDF",
"ADF", "ADFD",
"ADFAD", "ADFADF", "ADFAD" };
public static final String[] rates = new String[] {
"100",
"500", "300",
"400", "120", "600" };
public static final Integer[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
ListView listView;
List<RowItem> rowItems;
Button responseSubmit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listof_workers);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < name.length; i++) {
RowItem item = new RowItem(images[i], name[i], landmarks[i], rates[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_single, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view,int position, long id) {
Intent i = new Intent(ListofWorkers.this, WorkerProfile.class);
i.putExtra("name", name);
i.putExtra("landmarks", landmarks);
i.putExtra("rates", rates);
i.putExtra("image", images);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
Now this is my another activity "WorkerProfile" where i got name,landmarks and rates but only in image, i got null pointer exception, what i have put in TOAST.
package com.example.dispatchmedemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class WorkerProfile extends Activity {
// Declare Variables
TextView txtname;
TextView txtlandmark;
TextView txtrate;
ImageView imgpro;
String[] name;
String[] landmarks;
String[] rate;
int[] images;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_worker_profile);
Intent i = getIntent();
position = i.getExtras().getInt("position");
name = i.getStringArrayExtra("name");
landmarks = i.getStringArrayExtra("landmarks");
rate = i.getStringArrayExtra("rates");
images = i.getIntArrayExtra("image");
txtname = (TextView) findViewById(R.id.textView1);
txtlandmark = (TextView) findViewById(R.id.textView2);
txtrate = (TextView) findViewById(R.id.textView3);
imgpro = (ImageView)findViewById(R.id.ActivityWorkerProflePic);
txtname.setText(name[position]);
txtlandmark.setText(landmarks[position]);
txtrate.setText(rate[position]);
imgpro.setImageResource(images[position]);
}
}
Upvotes: 1
Views: 367
Reputation: 151
Here you are passing integer as an object defined by Integer which is a wrapper class.
And at the receiver activity you are retrieving it as an array of primitive type(int). That is why you are getting Null pointer exception.
Please change the below code,
public static final Integer[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
to,
public static final int[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
Hope It will help.
Upvotes: 1
Reputation: 24853
Try this..
Put images array as IntegerArrayList in sending intent
i.putIntegerArrayListExtra("image", new ArrayList<Integer>(Arrays.asList(images)))
then get like
ArrayList<Integer> images = i.getIntegerArrayListExtra("image");
the use it like
imgpro.setImageResource(images.get(position));
Upvotes: 0
Reputation: 3359
Try something like this:
Bundle bundel = new Bundle();
bundel.putIntArray("image",images);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);
and in nect.class use like:
Integer img[] = getIntent().getExtras().getIntArray("image");
Upvotes: 0