Android 将数组从活动传递到自定义适配器类
在 MainAcitivty.java 中,我用项目填充 Arraylist。我的自定义适配器类中需要这些项目,因为我需要它们来设置文本视图中某些行的颜色。
我有一个动态数组:storage [],其大小在过程中设置:
storage = new int[db];
该数组具有以下项目,例如:35,56,67
数组列表“myArr3”具有相同的项目,但元素类型是字符串。
这是 myArr3 arraylist 的定义:
ArrayList<String> myArr3 = new ArrayList<String>();
我正在寻找一种解决方案来管理另一个类中的这些项目,无论它是整数类型数组还是字符串类型数组列表。
在 MyCustomBaseAdapter2.java 中,我设置了适配器的布局:
package com.bfarago.nevnap;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyCustomBaseAdapter2 extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter2(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public MyCustomBaseAdapter2(OnClickListener onClickListener,
ArrayList<SearchResults> searchResults) {
// TODO Auto-generated constructor stub
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row2, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.left);
holder.txtCityState = (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
}
}
In MainAcitivty.java i populate an Arraylist with items. I need these items in my customadapter class because i need them to set the colour of certain rows in a textview.
I have a dynamic array: storage [], whose size is set during the process:
storage = new int[db];
This array has the following items e.g.: 35,56,67
The arraylist "myArr3" has the same items but type of the elements are string.
This is the definition of the myArr3 arraylist:
ArrayList<String> myArr3 = new ArrayList<String>();
I am looking for a solution to manages these items in the other class whether it is an integer type array or a string type arraylist.
In MyCustomBaseAdapter2.java i set the layout of the Adapter:
package com.bfarago.nevnap;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyCustomBaseAdapter2 extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter2(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public MyCustomBaseAdapter2(OnClickListener onClickListener,
ArrayList<SearchResults> searchResults) {
// TODO Auto-generated constructor stub
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row2, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.left);
holder.txtCityState = (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将整数数组更改为字符串
ArrayList
,然后在Adapter
中使用ArrayList
您可以将整数数组更改为字符串数组,就像现在
将
stringstorage
更改为您的ArrayList
现在根据您的需要使用
list
。希望这会对您有所帮助。
You can change the integer array to a string
ArrayList
and then use theArrayList
in yourAdapter
You can change your integer array to string array to like this
Now Change the
stringstorage
to yourArrayList
Now use the
list
as your need.Hope this will help you.
解决方案是使用意图将数组从一个活动传递到另一个活动。
答案可以在这个中找到
The solution is to use intents to pass an array from one activity to another activity.
The answer can be found in this