这是要回答的区域,但我可以弄清楚如何将其作为评论发布到此处的其他评论。
所以,我遇到了同样的问题。我认为那是因为如果我没有这个 ID,因为我正在尝试动态使用 all。
我已经尝试调用我的构造函数 put (context, 0, list-of-Objects)
还有其他解决方案可以尝试吗?
06-13 15:48:18.273: E/AndroidRuntime(14326): FATAL EXCEPTION: main
06-13 15:48:18.273: E/AndroidRuntime(14326): android.content.res.Resources$NotFoundException: String resource ID #0xc
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.content.res.Resources.getText(Resources.java:230)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.TextView.setText(TextView.java:3640)
06-13 15:48:18.273: E/AndroidRuntime(14326): at com.example.frame.graphics.ContextArrayAdapter.getView(ContextArrayAdapter.java:68)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.ListView.onMeasure(ListView.java:1158)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-13 15:48:18.273: E/AndroidRuntime(14326): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.View.measure(View.java:15513)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.os.Handler.handleCallback(Handler.java:725)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.os.Looper.loop(Looper.java:137)
06-13 15:48:18.273: E/AndroidRuntime(14326): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-13 15:48:18.273: E/AndroidRuntime(14326): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:48:18.273: E/AndroidRuntime(14326): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 15:48:18.273: E/AndroidRuntime(14326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-13 15:48:18.273: E/AndroidRuntime(14326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-13 15:48:18.273: E/AndroidRuntime(14326): at dalvik.system.NativeStart.main(Native Method)
编辑1:所以,这是我的代码:
package com.example.frame.graphics;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class ContextArrayAdapter extends ArrayAdapter<contextitem> {
Activity context;
// int layoutResourceId;
List<contextitem> objects;
public ContextArrayAdapter(Context context, int textViewResourceId,
List<contextitem> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = (Activity) context;
// this.layoutResourceId = textViewResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout row = (RelativeLayout) convertView;
ContextHolder holder = null;
if (row == null) {
holder = new ContextHolder();
holder.name = new TextView(context);
holder.counter = new TextView(context);
RelativeLayout.LayoutParams paramsName = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsName.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams paramsCount = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsName.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
row = new RelativeLayout(context);
row.addView(holder.name, paramsName);
row.addView(holder.counter, paramsCount);
row.setTag(holder);
parent.addView(row, position);
} else {
holder = (ContextHolder) row.getTag();
}
contextitem contextItem = objects.get(position);
holder.name.setText(contextItem.getName());
int count = contextItem.getCount();
if (count > -1) {
holder.counter.setText(contextItem.getCount());
} else {
holder.counter.setText("");
}
return row;
}
static class ContextHolder {
TextView name;
TextView counter;
}
}
编辑2:解决方案(最后)
问题是我们需要那里的线性布局。无论是否使用 XML。
package com.example.frame.graphics;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import org.apache.cordova.api.LOG;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import com.example.frame.MainActivity;
public class ContextArrayAdapter extends ArrayAdapter<contextitem> {
Activity context;
ArrayList<contextitem> list;
static private final String TAG = "ContextArrayAdapter.java";
public ContextArrayAdapter(Context context) {
super(context, android.R.layout.list_content);
this.context = (Activity) context;
this.list = new ArrayList<contextitem>();
}
@Override
public void add(contextitem object) {
// TODO Auto-generated method stub
// super.add(object);
if (MainActivity.debug) {
LOG.d(TAG, "add Called");
}
list.add(object);
}
@Override
public void addAll(Collection<? extends contextitem> collection) {
// TODO Auto-generated method stub
// super.addAll(collection);
// super.add(object);
if (MainActivity.debug) {
LOG.d(TAG, "addAll Called");
}
list.addAll(collection);
}
@Override
public void remove(contextitem object) {
// TODO Auto-generated method stub
// super.remove(object);
if (MainActivity.debug) {
LOG.d(TAG, "remove Called");
}
list.remove(object);
}
public ArrayList<contextitem> getList() {
return list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
// return super.getCount();
if (MainActivity.debug) {
LOG.d(TAG, "getCount Called");
}
return list.size();
}
@Override
public contextitem getItem(int position) {
// TODO Auto-generated method stub
// return super.getItem(position);
if (MainActivity.debug) {
LOG.d(TAG, "getItem Called");
}
return list.get(position);
}
@Override
public int getPosition(contextitem item) {
// TODO Auto-generated method stub
// return super.getPosition(item);
if (MainActivity.debug) {
LOG.d(TAG, "getPosition Called");
}
return list.indexOf(item);
}
@Override
public void sort(Comparator<? super contextitem> comparator) {
// TODO Auto-generated method stub
// super.sort(comparator);
if (MainActivity.debug) {
LOG.d(TAG, "sort Called");
}
Collections.sort(list, comparator);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (MainActivity.debug) {
LOG.d(TAG, "getView Called");
}
LinearLayout row = (LinearLayout) convertView;
ContextHolder holder = null;
if (row == null) {
holder = new ContextHolder();
holder.name = new TextView(context);
holder.counter = new TextView(context);
RelativeLayout.LayoutParams paramsName = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsName.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams paramsCount = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsCount.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
RelativeLayout innerLayout = new RelativeLayout(context);
innerLayout.addView(holder.name, paramsName);
innerLayout.addView(holder.counter, paramsCount);
row = new LinearLayout(context);
row.addView(innerLayout);
row.setTag(holder);
// LayoutInflater inflater = (LayoutInflater)
// context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// row = (LinearLayout)
// inflater.inflate(com.example.frame.R.layout.context_row, parent,
// false);
// holder = new ContextHolder();
// holder.name = (TextView) row.findViewById(R.id.context_row_name);
// holder.counter = (TextView)
// row.findViewById(R.id.context_row_count);
// row.setTag(holder);
} else {
holder = (ContextHolder) row.getTag();
}
contextitem contextItem = this.getItem(position);
holder.name.setText(contextItem.getName());
int count = contextItem.getCount();
if (count > -1) {
holder.counter.setText(Integer.toString(contextItem.getCount()));
} else {
holder.counter.setText("");
}
return row;
}
class ContextHolder {
TextView name;
TextView counter;
}
}