尝试将数据从Intent迁移到ListView,但出现了一些错误
Posted
技术标签:
【中文标题】尝试将数据从Intent迁移到ListView,但出现了一些错误【英文标题】:Try to migrate data from Intent to ListView, but there are some error 【发布时间】:2018-10-21 22:44:50 【问题描述】:我已经在这个问题上得到了帮助,但是现在,当我运行应用程序时,这个错误就来了。 2小时后一切都很好,但我没有改变任何东西,那是什么?我尝试从 Firestore 中获取数据,并将其传送到 ListView,然后传送到下一个活动。
错误:
进程:com.example.eugene.lafinalproduction,PID:9741 java.lang.IllegalArgumentException: n
这一行有错误:
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
主要活动:
public class ListViewPlaces extends AppCompatActivity
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference placesRef = rootRef.collection("Places");
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_places);
placesRef.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 places = document.toObject(Places.class);
placesList.add(places);
if (placesList.size() > 0)
int placeCount = placesList.size();
System.out.println(placesList.size());
Random randomGenerator = new Random();
List<Places> randomPlaceList = new ArrayList<>();
for (int i = 0; i <= 3; i++)
String randomIndex = randomGenerator.nextInt(randomPlaceList.size());
randomPlaceList.add(randomIndex);
ListView mListView = (ListView) findViewById(R.id.place_list);
PlacesAdapter placesAdapter = new PlacesAdapter(getApplicationContext(), randomPlaceList);
mListView.setAdapter(placesAdapter);
);
模型类:
public class Places
private String name;
public Places()
public Places(String name)
this.name = name;
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);
【问题讨论】:
更新错误行 我在 Main Activity@Eminem 中更新了答案 是的,现在在我的项目@Eminem 中是错误的 【参考方案1】:int placeCount = placesList.size();
nextInt(int) 产生一个从 0 到 n 的数字。例如,nextInt(3) 可能会产生 0、1 或 2。因此,值 placecount
bound - 上限(不包括)。必须是正面的。 编辑:
for (int i = 1; i <= 3; i++)
if (placeCount >0)
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
另一种方法是:
for (int i = 0; i < 3; i++)
int randomIndex = randomGenerator.nextInt(randomPlaceList.size());
Places item = randomPlaceList.get(randomIndex);
randomPlaceList.add(item);
【讨论】:
以上是关于尝试将数据从Intent迁移到ListView,但出现了一些错误的主要内容,如果未能解决你的问题,请参考以下文章
自定义 ListView 以处理从另一个 Intent 传递的 EditText 和 TextView
将 wpf listview 绑定到数据集......可能......?
我正在尝试使用intent [duplicate]填充listview
如何获取listView位置值并通过intent传递给另一个类