我尝试将我在ListView
中选择的数据项传递给下一个Activity
。但我所能做的就是第二个活动是空的或nullpoinexception
错误。我是使用RecycleView
完成的,但是ListView
存在一些问题。
主要活动:
public class ListViewPlaces extends AppCompatActivity {
FirebaseFirestore mFirestore = FirebaseFirestore.getInstance();
CollectionReference placeRef = mFirestore.collection("Places");
Query query;
ListView mListView;
String name;
Places places;
ArrayList<Places> randomPlaceList;
PlaceAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_places);
mListView = findViewById(R.id.place_list);
randomPlaceList = new ArrayList<>();
Boolean l_check1 = getIntent().getExtras().getBoolean("1");
Boolean l_check2 = getIntent().getExtras().getBoolean("2");
if (l_check2){
query = placeRef.whereEqualTo("colour", "red");
} else if (l_check1){
query = placeRef.whereEqualTo("size", "1");
} else {
query = placeRef.whereEqualTo("size", "2");
}
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places place = document.toObject(Places.class);
placesList.add(place);
}
int placeCount = placesList.size();
Random randomGenerator = new Random();
ArrayList<Places> randomPlaceList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
}
ListView mListView = (ListView) findViewById(R.id.place_list);
mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
mListView.setAdapter(mAdapter);
}
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Places places = mAdapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), Card_activity.class);
intent.putExtra("name", places.getName());
startActivity(intent);
}
});
}
}
我的模特课:
public class Places {
private String image, name;
public Places() {
}
public Places(String image, String name) {
this.image = image;
this.name = name;
}
public String getImage() { return image; }
public String getName() { return name; }
}
自定义适配器:
class PlaceAdapter extends ArrayAdapter<Places> {
private ArrayList<Places> dataSet;
Context mContext;
private static class ViewHolder {
TextView name_text;
ImageView image_text;
}
public PlaceAdapter(ArrayList<Places> dataSet, Context context ) {
super(context, R.layout.item_list, dataSet);
this.dataSet = dataSet;
this.mContext = context;
}
@Override
public View getView(int position, View converView, ViewGroup parent) {
Places places = getItem(position);
ViewHolder viewHolder;
final View result;
if (converView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
converView = inflater.inflate(R.layout.item_list, parent, false);
viewHolder.name_text = (TextView) converView.findViewById(R.id.text_image_id);
viewHolder.image_text = (ImageView) converView.findViewById(R.id.image_id);
result = converView;
converView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) converView.getTag();
result = converView;
}
viewHolder.name_text.setText(places.getName());
Glide.with(getContext()).load(places.getImage()).into(viewHolder.image_text);
return converView;
第二个活动,我传递数据:
public class Card_activity extends AppCompatActivity implements View.OnClickListener{
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.massive_places);
mTextView = findViewById(R.id.head_name);
String name = getIntent().getStringExtras("name")
mTextView.setText(name);
}
}
已编辑:错误
java.lang.NullPointerException:尝试调用虚方法 &#39; java.lang.Object java.util.ArrayList.get(int)&#39;在null对象上 参考
在这一行:
Places places = randomPlaceList.get(position);
ERROR:
过程:com.example.eugene.lafinalproduction,PID:8352 java.lang.IllegalArgumentException:n&lt; = 0:0 在java.util.Random.nextInt(Random.java:182) 在com.example.eugene.lafinalproduction.ListViewPlaces $ 1.onComplete(ListViewPlaces.java:63) 在com.google.android.gms.tasks.zzj.run(未知来源) 在android.os.Handler.handleCallback(Handler.java:815) 在android.os.Handler.dispatchMessage(Handler.java:104) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5763) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
这一行的问题:
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
答案 0 :(得分:1)
使用randomPlaceList
onCreate
方法初始化ListViewPlaces
。
randomPlaceList = new ArrayList<>();
目前,您正在以这种randomPlaceList
初始化this.randomPlaceList = randomPlaceList;
,这是错误的,因为您分配了randomPlaceList
的相同副本,类似 a = a 。
并删除位于ArrayList<Places> randomPlaceList = new ArrayList<>();
query.get().addOnCompleteListener();
getIntent().getExtras()
会返回{<1}},其值为键值对(您可以将键值对的多个值添加到a捆绑)但你没有传递Bundle
,你只传递了一个键值对。因此,最好在Bundle
。{/ p>中致电getStringExtra("name")
Card_activity
而不是
String name = getIntent().getStringExtra("name");
答案 1 :(得分:1)
错误表示您的randomPlaceList
为空。
由于我无法看到您执行的应用程序或逻辑以获取错误,因此我只是根据我所看到的内容进行操作。
首先,如果您需要获取点击的项目,则应通过调用getItem(position)
的{{1}}方法来 。您可以像下面这样轻松实现它:
adapter
现在,您可以通过@Override
public Places getItem(int position) {
if(dataSet != null && position < dataSet.size()){
return dataSet.get(position);
}
return null;
}
来执行以下呼叫:
activity
请在初始化时添加对Places places = mAdapter.getItem(position);
的引用,方法是在活动变量中添加adapter
并使用
PlaceAdapter mAdapter;
快速阅读代码,我对此也没有多大意义:
mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
mListView.setAdapter(mAdapter);
但也许你删掉了一些代码,但我更愿意向你报告,以防万一:)
错误在这里,它是关于变量的参考:
this.randomPlaceList = randomPlaceList;
您应该将其替换为:
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places place = document.toObject(Places.class);
placesList.add(place);
}
int placeCount = placesList.size();
Random randomGenerator = new Random();
// In the line below, you specify a new ArrayList that is LOCAL, you don't refer to the Activity's object
ArrayList<Places> randomPlaceList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
}
ListView mListView = (ListView) findViewById(R.id.place_list);
PlaceAdapter placeAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
mListView.setAdapter(placeAdapter);
}
});
否则活动的列表将始终为null,因为您从不初始化
希望这有帮助
答案 2 :(得分:0)
在您的第二项活动中,请更改此内容:
String name = getIntent().getExtras().getString("name");
要:
String name = getIntent().getStringExtra("name");
答案 3 :(得分:0)
您的代码中存在多个问题。但是,我们只关注你的问题。 你应该使用
intent.getStringExtra
从先前的活动中获取字符串数据,而不是使用
intent.getExtras().getString("name");
getStringExtra和getExtras是不同的。