类看到 array.lengh 但看不到它的数据
我的代码中有一个奇怪的错误,但我看不到问题所在。我有一个主班,里面还有另一个班。我在主类中声明了一个数组列表,如果我在类的某些部分访问它,我可以获得所有信息,但是当我在内部类中访问它时,它看不到数据。为什么?
内部类是一个用于制作视图列表的BaseAdapter。我从 oncreate 包中将其称为:
public class Main extends ListActivity implements OnClickListener {
//new adapter
private EfficientAdapter adap;
//The variable which makes the error
public static ArrayList<String> data2 = new ArrayList<String>();
protected static Context mContext=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nuevoingr);
data2.clear();
data2.add("just adding 1");
adap = new EfficientAdapter(this);
setListAdapter(adap);
内部类是(问题在它的末尾):
public static class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
this.context = context;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(final int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid
// unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is
// no need
// to reinflate it. We only inflate a new View when the convertView
// supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.pruebas, null);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.pr2);
holder.buttonLine = (ImageButton) convertView.findViewById(R.id.pr1);
convertView.setOnClickListener(new OnClickListener() {
private int pos = position;
@Override
public void onClick(View v) {
Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
holder.buttonLine.setOnClickListener(new OnClickListener() {
private int pos = position;
@Override
public void onClick(View v) {
Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.textLine.setText(String.valueOf(position));
return convertView;
}
static class ViewHolder {
TextView textLine;
ImageButton buttonLine;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//Here it comes the problem. Before editing my program this code was provide to include only a few values from
a String array declared at the same moment of data2 called data. What I do now is to declare an arraylist so
I can include more values and then I refresh the adaptor and here I pass the arraylist to an array to be equal as
it was before.
@Override
public int getCount() {
// TODO Auto-generated method stub
String [] change = (String[]) data2.toArray(new String[data2.size()]);
return change.length;
//return data.length;
}
//Same problem
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
String [] change = (String[]) data2.toArray(new String[data2.size()]);
return change[position];
//return data[position];
}
}
奇怪的是,这个 baseAdapter 用于列表视图,当它更新它的数据时,会有与 arraylist.length 一样的行,但它的数据不包含它,并且单击它时所有行都被检测为相同。
I have a strange error in my code and i can't see the problem. I have a main class where I have an other class inside. I have an arraylist declared in the main class and if I acces to it in somepart of the class i can have all the information but when i acces to it in the inner class it can't see the data. Why?
The inner class is an BaseAdapter for making a viewlist. I called it from oncreate bundle as:
public class Main extends ListActivity implements OnClickListener {
//new adapter
private EfficientAdapter adap;
//The variable which makes the error
public static ArrayList<String> data2 = new ArrayList<String>();
protected static Context mContext=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nuevoingr);
data2.clear();
data2.add("just adding 1");
adap = new EfficientAdapter(this);
setListAdapter(adap);
and the inner class is (the problem is at the end of it):
public static class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
this.context = context;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(final int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid
// unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is
// no need
// to reinflate it. We only inflate a new View when the convertView
// supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.pruebas, null);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.pr2);
holder.buttonLine = (ImageButton) convertView.findViewById(R.id.pr1);
convertView.setOnClickListener(new OnClickListener() {
private int pos = position;
@Override
public void onClick(View v) {
Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
holder.buttonLine.setOnClickListener(new OnClickListener() {
private int pos = position;
@Override
public void onClick(View v) {
Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.textLine.setText(String.valueOf(position));
return convertView;
}
static class ViewHolder {
TextView textLine;
ImageButton buttonLine;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//Here it comes the problem. Before editing my program this code was provide to include only a few values from
a String array declared at the same moment of data2 called data. What I do now is to declare an arraylist so
I can include more values and then I refresh the adaptor and here I pass the arraylist to an array to be equal as
it was before.
@Override
public int getCount() {
// TODO Auto-generated method stub
String [] change = (String[]) data2.toArray(new String[data2.size()]);
return change.length;
//return data.length;
}
//Same problem
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
String [] change = (String[]) data2.toArray(new String[data2.size()]);
return change[position];
//return data[position];
}
}
What is strange is that this baseAdapter is for a listview and when it updates it's data there's as rows as arraylist.length but its data it's not include it and all rows are detected as the same when clicking it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this:
问题解决了。
我将代码更改为以下代码: techdroid.kbeanie.com/2009 /07/custom-listview-for-android.html
Problem solved.
I changed the code to this one: techdroid.kbeanie.com/2009/07/custom-listview-for-android.html