Android - 将对象添加到另一个活动的列表视图中

时间:2018-04-06 09:03:04

标签: android listview parcelable

我有自己的适配器自定义列表视图,我可以在同一个活动中将对象添加到此列表中,但我想要实现的是从另一个活动添加对象的可能性。在另一个活动中,有两个编辑文本框,一个负责主文本,第二个用于描述,两个单选按钮用于确定图像。在底部是添加按钮,应该将输入的数据添加到列表视图。

我尝试用Parcelable做,但我不确切知道如何实现它。下面是我的ListView类:

public class Activity_4 extends AppCompatActivity {

private ListView listView;
private myAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_4);

    listView = (ListView)findViewById(R.id.listView4);
    ArrayList<Object> objectList = new ArrayList<>();

    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));
    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));
    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));
    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));
    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));
    objectList.add(new Object(R.drawable.row_img, "After Earth" , "2013"));

    mAdapter = new myAdapter(this,objectList);
    listView.setAdapter(mAdapter);

}

public class Object implements Parcelable {
    int imageID;
    String mainText;
    String description;

    public Object (int imageID, String mainText, String description) {
        this.imageID = imageID;
        this.mainText = mainText;
        this.description = description;
    }

    public Object() {

    }

    public int getImageID() {
        return imageID;
    }
    public void setImageID(int imageID) {
        this.imageID = imageID;
    }
    public String getMainText () {
        return  mainText;
    }
    public void setMainText() {
        this.mainText = mainText;
    }
    public  String getDescription() {
        return  description;
    }
    public void setDescription() {
        this.description = description;
    }

    public Object(Parcel in) {
        this.imageID = in.readInt();
        this.mainText = in.readString();
        this.description = in.readString();

    }


    public final Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() {
        @Override
        public Object createFromParcel(Parcel in) {
            return new Object(in);
        }

        @Override
        public Object[] newArray(int size) {
            return new Object[size];
        }
    };
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(imageID);
        dest.writeString(mainText);
        dest.writeString(description);
    }

}

public class myAdapter extends ArrayAdapter<Object> {

    private Context mContext;
    private List<Object> objectList = new ArrayList<>();

    public myAdapter(@NonNull Context context, ArrayList<Object> list) {
        super(context,0,list);
        mContext = context;
        objectList = list;
    }

    @NonNull
    @Override
    public View getView (int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);

        Object currentObject = objectList.get(position);

        ImageView image = (ImageView) listItem.findViewById(R.id.row_image);
        image.setImageResource(currentObject.getImageID());

        TextView mainTxt = (TextView) listItem.findViewById(R.id.row_tv1);
        mainTxt.setText(currentObject.getMainText());

        TextView description = (TextView) listItem.findViewById(R.id.row_tv2);
        description.setText(currentObject.getDescription());

        return listItem;
    }

}

任何人都可以帮忙解决这个问题吗?

由于

3 个答案:

答案 0 :(得分:1)

您可以使用Activity其他startActivityForResult()并在data中通过bundle,点击其他Activity的添加按钮,以便当前活动添加数据到它的列表。

致电其他Activity添加

startActivityForResult(intent,reqCode);

结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == reqCode) {
           Object object = new Object();
           object.setImageID(data.getIntExtra("image_id));
           object.setMainText(data.getStringExtra("main_text");
           object.setDescription(data.getStringExtra("desc");
           objectlist.add(object);
           mAdapter.notifyDataSetChanged();
       }
}

其他活动

public void onAddClick(){
  Intent intent = new Intent();
  intent.putIntExtra("image_id",*<value>*);
  intent.putStringExtra("main_text",*<value>*);
  intent.putStringExtra("desc",*<value>*);
  setResult(intent);
  finish();
}

答案 1 :(得分:0)

一个很好的起点可能是使用startActivityForResult启动EditText活动,并通过调用setResult添加您想要的所有内容后,通过结果将更新传递到适配器数据。如需进一步参考,请参阅Getting a Result from an Activity。祝你好运!

答案 2 :(得分:0)

从第一个活动开始第二个活动

DO

Intent mIntent=new Intent(this,YourSecondActivity.class);
startActivityForResult(mIntent,SOME_STATIC_INT)

在另一项活动中 点击AddButton

Intent mIntent=new Intent(this,YourFirstActivity.class);
mIntent.putExtra("DATA",Object)
setResult(SOME_STATIC_INT,mIntent)

现在进入第一个活动 覆盖onActivity结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == SOME_STATIC_INT) {
           Object object = data.getParcelableExtra("DATA")

           objectlist.add(object);
           mAdapter.notifyDataSetChanged();
       }
}