我在android中制作了自定义列表视图。我希望每当列表视图点击事件被调用它进入另一个活动,该列表上的数据将显示在新活动上。下面的代码是我喜欢的MainActivity(这里命名为ListofWorkers),它也有布局文件。
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);
}
});
}
}
现在这是我的另一项活动&#34; WorkerProfile&#34;我得到了名字,地标和费率,但只有在图像中,我得到了空指针异常,我把它放在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]);
}
}
答案 0 :(得分:1)
这里传递整数作为整数定义的对象,这是一个包装类。
在接收器活动中,您将其检索为基本类型数组( int )。这就是你得到Null指针异常的原因。
请更改以下代码,
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 };
要,
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 };
希望它会有所帮助。
答案 1 :(得分:0)
尝试这样的事情:
Bundle bundel = new Bundle();
bundel.putIntArray("image",images);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);
并在next.class中使用如下:
Integer img[] = getIntent().getExtras().getIntArray("image");
答案 2 :(得分:0)
试试这个..
将图像数组作为IntegerArrayList放入发送意图
i.putIntegerArrayListExtra("image", new ArrayList<Integer>(Arrays.asList(images)))
然后得到像
ArrayList<Integer> images = i.getIntegerArrayListExtra("image");
使用它
imgpro.setImageResource(images.get(position));