我已经四处寻找解决这个问题好几天了,我在这里看到了几个问题,其中一些已经解决,但没有一个真正帮助我,因为我的情况与其他情况有点不同。
在我的应用程序的一部分中,我将展示在应用程序的另一部分中开始的所有活动的历史记录。为此,我在我的历史活动中使用了一个列表视图。这是活动的代码
历史列表.java
public class HistoryList extends Activity {
Campaign_Db myCamp = new Campaign_Db(HistoryList.this);
Date_calc myDate = new Date_calc(HistoryList.this);
graphCreate mygraph = new graphCreate(HistoryList.this);
ListView historylist;
SimpleCursorAdapter cursorAdapter;
Cursor cursor = null;
String picadd;
ImageView pic;
ArrayList<Integer> array_cap = new ArrayList<Integer>();
Integer[] arr_cap;
int passing_id;
String passing_date,passing_startdate;
GraphicalView newgraph;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.historylist);
historylist = (ListView) findViewById(R.id.historylistview);
myCamp.open();
cursor = myCamp.gethistory();
if(cursor !=null && cursor.moveToFirst()){
int rowenddate = cursor.getColumnIndex(Campaign_Db.KEY_ROWID);
int rowstartdate = cursor.getColumnIndex(Campaign_Db.KEY_START);
String camp_id = cursor.getString(rowenddate);
passing_id = Integer.valueOf(camp_id);
passing_date = cursor.getString(rowstartdate);
}
getdata();
newgraph = mygraph.startgraph(arr_cap,this);
HistoryAdapter histAdap = new HistoryAdapter(HistoryList.this,cursor,R.layout.historyrow,arr_cap, newgraph);
historylist.setAdapter(histAdap);
}
private void getdata() {
String dt = myDate.Calc(passing_date);
int d = Integer.valueOf(dt);
try {
array_cap = myCamp.historygarphs(d,passing_id,passing_date);
} catch (ParseException e) {
e.printStackTrace();
}
arr_cap = array_cap.toArray(new Integer[array_cap.size()]);
}
}
在该活动中,我将列表视图设置为自定义适配器,并传递要使用的光标以及要使用的 AChartEngine 图。图形和游标数据都已经过测试和记录,因此它们没有问题。这是自定义适配器的代码
HistoryAdapter.java
public class HistoryAdapter extends CursorAdapter {
private Context context;
private int layout;
private Integer[] arr_cap;
String picadd;
GraphicalView newgraph;
@SuppressWarnings("deprecation")
public HistoryAdapter(Context context, Cursor c, int layout, Integer[] arr, GraphicalView grp) {
super(context, c);
this.arr_cap= arr;
this.context = context;
this.layout = layout;
this.newgraph = grp;
}
Campaign_Db myCamp = new Campaign_Db(context);
Date_calc myDate = new Date_calc(context);
@Override
public void bindView(View view, Context context, Cursor cursor) {
int rowaddress = cursor.getColumnIndex(Campaign_Db.KEY_PIC);
int rowenddate = cursor.getColumnIndex(Campaign_Db.KEY_ROWID);
int rowstartdate = cursor.getColumnIndex(Campaign_Db.KEY_START);
int rowcampname = cursor.getColumnIndex(Campaign_Db.KEY_NAME);
ImageView pic = (ImageView) view.findViewById(R.id.history_picture);
TextView enddate = (TextView) view.findViewById(R.id.history_enddate);
TextView startdate = (TextView) view.findViewById(R.id.history_startdate);
TextView campaign_name = (TextView) view.findViewById(R.id.history_campaignname);
LinearLayout lay = (LinearLayout) view.findViewById(R.id.historychart);
lay.addView(newgraph, new LayoutParams(220, 80));
if (cursor != null && cursor.moveToFirst()){
picadd = cursor.getString(rowaddress);
enddate.setText(cursor.getString(rowenddate));
startdate.setText(cursor.getString(rowstartdate));
campaign_name.setText(cursor.getString(rowcampname));
}
File imgFile = new File(picadd);
if(imgFile.exists()){
Bitmap bmp = BitmapFactory.decodeFile(picadd);
Bitmap reducedbmp = Bitmap.createScaledBitmap(bmp, 100, 100, true);
Bitmap rightsidebmp = getrightside(reducedbmp);
Bitmap finalbmp = getRoundedCornerBitmap(rightsidebmp, 50);
pic.setImageBitmap(finalbmp);
}
}
@Override
public View newView(Context context, Cursor cursor,ViewGroup parent ) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout,null,false);
bindView(v, context, cursor);
return v;
}
public Bitmap getrightside(Bitmap bit){
Bitmap bmp = bit;
File f = new File(picadd);
ExifInterface exif = null;
try {
exif = new ExifInterface(f.getPath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap correctBmp = Bitmap.createBitmap(bit , 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
return correctBmp;
}
public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
当我尝试将 achartengine 图添加到我的相对布局时,就会出现问题,这种方式我已经在另一个活动中完成并且它已经工作了。
当这行被注释时,其余代码运行完美。
lay.addView(newgraph, new LayoutParams(220, 80));
从我一直在阅读我得到的错误中
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
是当我尝试使用一个孩子并在它已经有另一个父母时给它另一个父母时引起的,但是在这种情况下,子新图刚刚在自定义适配器类中初始化,并且当使用 getparent() 时,它返回 null .
如果有人能帮助解决这个问题,那就太好了,在此先感谢您。