无法动态添加视图“child has a parent”android

时间:2017-11-23 19:33:35

标签: java android sql dynamically-generated

大家好我想让这个应用基于现有的数据库和xml文件生成很多视图。我的应用程序正在粉碎,我得到的错误如public class CytatyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cytaty); SqlLiteDataBaseHelper sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(this); LinearLayout ll = (LinearLayout) findViewById(R.id.linear_cytaty); try { if(sqlLiteDataBaseHelper.checkDataBase()){ Log.e("db: ","Data Base Already Exists"); }else { sqlLiteDataBaseHelper.CopyDataBaseFromAsset(); } }catch (Exception e){ e.printStackTrace(); } try { sqlLiteDataBaseHelper.openDataBase(); // after open data base u can read write data base }catch (Exception e){ e.printStackTrace(); } LayoutInflater factory = getLayoutInflater(); RelativeLayout relativeLayout = new RelativeLayout(this); View v = factory.inflate(R.layout.layout_cytaty_text, relativeLayout,false); View v2 = factory.inflate(R.layout.layout_cytaty_image, relativeLayout ,false); Cursor cursor = sqlLiteDataBaseHelper.db.rawQuery("SELECT * FROM Cytaty",null); Context cont = getApplicationContext(); if( cursor.moveToFirst()) { do { // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.TV_cytat); textView.setText(cursor.getString(cursor.getColumnIndex("text")).toString()); ImageView imageView = (ImageView) v2.findViewById(R.id.IV_cytat); Log.v("XD","XDDD"); int id = (int)cont.getResources().getIdentifier(cursor.getString(cursor.getColumnIndex("imagescr")).toString(), "drawable", cont.getPackageName()); imageView.setImageResource(id); relativeLayout.addView(textView); relativeLayout.addView(imageView); ll.addView(relativeLayout, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); } while (cursor.moveToNext()); } }

这是代码: -CytatyActivity.java:

((ViewGroup)imageView.getParent()).removeView(imageView);

}

-layout_cytaty_image.xml:

任何想法该怎么办? 我试图添加......如:

{"success":true,
"alldata": [
    {"name":"a","location":"market","age":12,"hobby":"singing","point":99.83},
    {"name":"b","location":"home","age":23,"hobby":"football","point":99.98},
    {"name":"c","location":"drugstore","age":17,"hobby":"game","point":99.99},
    ........ ,
    {"name":"z","location":"school","age":15,"hobby":"piano","point":100}
    ]
}

但它没有用。我在空白参数上使用remoteView()时遇到错误。

2 个答案:

答案 0 :(得分:0)

你需要这样做:

do {
    // fill in any details dynamically here
    RelativeLayout relativeLayout = new RelativeLayout(this);
    View v = factory.inflate(R.layout.layout_cytaty_text, null, false);
    View v2 = factory.inflate(R.layout.layout_cytaty_image, null, false);

    TextView textView = (TextView) v.findViewById(R.id.TV_cytat);
    textView.setText(cursor.getString(cursor.getColumnIndex("text")).toString());

    ImageView imageView = (ImageView) v2.findViewById(R.id.IV_cytat);
    Log.v("XD","XDDD");
    int id = (int)cont.getResources().getIdentifier(cursor.getString(cursor.getColumnIndex("imagescr")).toString(), "drawable", cont.getPackageName());
    imageView.setImageResource(id);


    relativeLayout.addView(v);
    relativeLayout.addView(v2);

    ll.addView(relativeLayout, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

但如果你创建视图programmaticaly而不是膨胀会更好。像这样:

do {
    TextView textView = new TextView(context);
    //set relative layout params to textView 
    textView.setText(cursor.getString(cursor.getColumnIndex("text")).toString());

    ImageView imageView = new ImageView (context);
    //set relative layout params to imageView 
    int id = (int)cont.getResources().getIdentifier(cursor.getString(cursor.getColumnIndex("imagescr")).toString(), "drawable", cont.getPackageName());
    imageView.setImageResource(id);

    RelativeLayout relativeLayout = new RelativeLayout(this);
    //set linear layout params to relativeLayout 
    relativeLayout.addView(textView);
    relativeLayout.addView(imageView);

    ll.addView(relativeLayout);
}

答案 1 :(得分:0)

感谢回复。我设法以程序方式完成了这项工作,但我仍然遇到了一些错误。这是我的新代码:

public class CytatyActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cytaty);
    SqlLiteDataBaseHelper sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(this);

    LinearLayout ll = (LinearLayout) findViewById(R.id.linear_cytaty);
    try {

        if(sqlLiteDataBaseHelper.checkDataBase()){

            Log.e("db: ","Data Base Already Exists");

        }else {

            sqlLiteDataBaseHelper.CopyDataBaseFromAsset();

        }

    }catch (Exception e){
        e.printStackTrace();
    }
    try {

        sqlLiteDataBaseHelper.openDataBase();
        // after open data  base u can read write data base

    }catch (Exception e){
        e.printStackTrace();
    }
    LayoutInflater factory = getLayoutInflater();

    Cursor cursor = sqlLiteDataBaseHelper.db.rawQuery("SELECT * FROM Cytaty",null);
    Context cont = getApplicationContext();

    if( cursor.moveToFirst()) {


        do {
            TextView textView = new TextView(this);
            //set relative layout params to textView
            textView.setText(cursor.getString(cursor.getColumnIndex("text")).toString());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );

            textView.setPadding(15,15,5,10);
            textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            params.setMargins(87, 20, 10, 0);
            textView.setLayoutParams(params);

            ImageView imageView = new ImageView (this);
            //set relative layout params to imageView
            int id = (int)cont.getResources().getIdentifier(cursor.getString(cursor.getColumnIndex("imagescr")).toString(), "drawable", cont.getPackageName());
            imageView.setImageResource(id);
            LinearLayout.LayoutParams paramv = (LinearLayout.LayoutParams)imageView.getLayoutParams();
            paramv.width = 80;
            paramv.height = 80;
            paramv.setMargins(7,20,0,0);
            imageView.setLayoutParams(paramv);

            RelativeLayout relativeLayout = new RelativeLayout(this);
            //set linear layout params to relativeLayout
            relativeLayout.addView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            relativeLayout.addView(imageView);

            ll.addView(relativeLayout);
        } while (cursor.moveToNext());
    }

}

}

我从第34行得到错误; paramv.width = 80;"并且它在空对象上抛出即时调用方法。