我想知道如何将新项添加到listView已经由数据填充的listView中?
在 Edit_Staff_Benefit_ListView 中,有一个添加button
,listView
(从SQLite获取的值)。
点击添加按钮后,它会转到 Edit_Staff ,最后将值返回 Edit_Staff_Benefit_ListView 。
下面的图片显示 Edit_Staff_Benefit_ListView 。如您所见,它有一个列表,从SQLite中检索。
Edit_Staff_Benefit_ListView
public class Edit_Staff_Benefit_ListView extends Fragment {
PicCustomBaseAdapter obj;
ArrayList<ImageAndText> images=new ArrayList<ImageAndText>();
ListView listViewEdit;
StaffAPI sqlcon;
private SimpleCursorAdapter dataAdapter;
public static final int PROJECT_REQUEST_CODE = 1;
public static final int CAMERA_REQUEST_CODE = 2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_details = inflater.inflate(R.layout.edit_staff_list, container, false);
listViewEdit = (ListView)edit_details. findViewById(R.id.listViewEditStaff);
add=(Button)edit_details.findViewById(R.id.button4);
dbHelper = new MyDatabaseHelper(getActivity());
sqlcon = new StaffAPI(getActivity());
Bundle bundle = this.getArguments();
if (getArguments() != null) {
ID = bundle.getLong("ID");
BuildEditStaffList(ID);
}
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
mClickedPosition = -1;
Intent intent = new Intent(getActivity(), Edit_Staff.class);
//Log.e("ST","S");
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
return edit_details;
}
public void BuildEditStaffList(long id) // retrieve data from SQLite
{
sqlcon.open();
Cursor cursor1=sqlcon.readName(id);
String[] columns=new String[]{
MyDatabaseHelper.Image,MyDatabaseHelper.Claim_Type,MyDatabaseHelper.Amount,MyDatabaseHelper.Description};
int[] to=new int[]
{
R.id.image, R.id.type,R.id.amount,R.id.description};
dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_staff,
cursor1,
columns,
to,
0);
dataAdapter.setViewBinder(new MyViewBinder());
listViewEdit.setAdapter(dataAdapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity EditStaff and populate to ListView
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
ReceiveType = data.getStringExtra("type");
ReceiveDescription = data.getStringExtra("description");
ReceiveAmount = data.getStringExtra("amount");
ReceiveImage = data.getParcelableExtra("photo");
if (mClickedPosition == -1) { // if icon clicked
if (obj != null) {
obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit);
listViewEdit.setAdapter(obj);
obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
}
}
Edit_Staff
save.setOnClickListener(new View.OnClickListener() { // return value to Edit_Staff_Benefit_ListView
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
Amount = amount.getText().toString();
Description = description.getText().toString();
claimType = type.getSelectedItem().toString();
returnIntent.putExtra("type", claimType);
returnIntent.putExtra("description", Description);
returnIntent.putExtra("amount", Amount);
returnIntent.putExtra("photo", photo);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
PicCustomBaseAdapter
public class PicCustomBaseAdapter extends BaseAdapter {
private static ArrayList<ImageAndText> imgAndText;
FrameLayout footerLayout;
private LayoutInflater mInflater;
ListView listview;
public PicCustomBaseAdapter(Context context, ArrayList<ImageAndText> img,ListView listview) {
imgAndText = img;
this.listview=listview;
mInflater = LayoutInflater.from(context);
}
public void addNewItem1(String type,String amount, Bitmap imageFromClaims,String description)
{
ImageAndText image = new ImageAndText();
image.setType(" Claims Type : " + type);
image.setAmount(" Amount : " + amount);
image.setImage(imageFromClaims);
image.setDescription(" Description : " + description);
imgAndText.add(image);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_image_and_text, null);
holder = new ViewHolder();
holder.txtType= (TextView) convertView.findViewById(R.id.ListType);
holder.txtDescription = (TextView) convertView.findViewById(R.id.ListDescription);
holder.txtAmount = (TextView) convertView.findViewById(R.id.ListAmount);
holder.picture=(ImageView)convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtType.setText(imgAndText.get(position).getType());
holder.txtDescription.setText(imgAndText.get(position).getDescription());
holder.txtAmount.setText(imgAndText.get(position).getAmount());
holder.picture.setImageBitmap(imgAndText.get(position).getImage());
return convertView;
}
static class ViewHolder {
TextView txtType;
TextView txtDescription;
TextView txtAmount;
ImageView picture;
}
}
现在的问题是当我点击Edit_Staff中的保存按钮时 返回 Edit_Staff_Benefit_ListView ,列表应该是 添加。但我仍然得到相同的名单。这里有什么问题?
答案 0 :(得分:0)
因为你创建自定义适配器的新对象并将其设置为listview,如果没有创建对象但是它已经没有向它添加数据的适配器
首先在Activity
的onCreate()中创建自定义适配器obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit);
listViewEdit.setAdapter(obj);
然后更改onActivityResult()
if (obj != null) {
obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit);
listViewEdit.setAdapter(obj);
obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
}
到
obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
或
if (obj != null) {
obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit);
listViewEdit.setAdapter(obj);
obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
} else {
obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
}
将适配器更改为
public class PicCustomBaseAdapter extends BaseAdapter {
private static ArrayList<ImageAndText> imgAndText;
FrameLayout footerLayout;
private LayoutInflater mInflater;
ListView listview;
boolean mFirstTime = true;
public PicCustomBaseAdapter(Context context, ArrayList<ImageAndText> img,ListView listview) {
imgAndText = img;
this.listview=listview;
mInflater = LayoutInflater.from(context);
}
public void addNewItem1(String type,String amount, Bitmap imageFromClaims,String description)
{
if(mFirstTime){
imgAndText.clear();
mFirstTime = false;
}
ImageAndText image = new ImageAndText();
image.setType(" Claims Type : " + type);
image.setAmount(" Amount : " + amount);
image.setImage(imageFromClaims);
image.setDescription(" Description : " + description);
imgAndText.add(image);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_image_and_text, null);
holder = new ViewHolder();
holder.txtType= (TextView) convertView.findViewById(R.id.ListType);
holder.txtDescription = (TextView) convertView.findViewById(R.id.ListDescription);
holder.txtAmount = (TextView) convertView.findViewById(R.id.ListAmount);
holder.picture=(ImageView)convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtType.setText(imgAndText.get(position).getType());
holder.txtDescription.setText(imgAndText.get(position).getDescription());
holder.txtAmount.setText(imgAndText.get(position).getAmount());
holder.picture.setImageBitmap(imgAndText.get(position).getImage());
return convertView;
}
static class ViewHolder {
TextView txtType;
TextView txtDescription;
TextView txtAmount;
ImageView picture;
}
}
答案 1 :(得分:0)
您可以使用adapterNotifyChanged从listiview添加新项目。
images.add(NewAdapterItemObject);
和
obj.notifyDataSetChanged();
完成了。