如何在已填充列表的 listView 中添加列表?
Posted
技术标签:
【中文标题】如何在已填充列表的 listView 中添加列表?【英文标题】:How do I add a list in listView where the list has been populated? 【发布时间】:2016-03-24 08:06:42 【问题描述】:我想知道如何在 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,列表应该是 添加。但我仍然得到相同的列表。这里有什么问题?
【问题讨论】:
在更改列表数据后使用'notifyDataSetChanged()'。 @Srikanth 不在 baseAdapter 内? 【参考方案1】:因为您正在创建自定义适配器的新对象并将其设置为列表视图,如果未创建对象但如果它已经创建则不向其适配器添加数据
首先在Activity的onCreate()中创建Custom Adapter
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;
【讨论】:
在onCreateView中,我输入listViewEdit = (ListView)edit_details. findViewById(R.id.listViewEditStaff); obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit);
,在onActivityResult中,我改为if (mClickedPosition == -1) // if icon clicked if (obj != null) obj = new PicCustomBaseAdapter(getActivity(),images,listViewEdit); listViewEdit.setAdapter(obj); obj.addNewItem1(ReceiveType, ReceiveAmount, ReceiveImage, ReceiveDescription);
当我点击保存按钮时,第一个列表将被最新列表覆盖。我现在可以添加一个新列表,但我不希望第一个列表(从 SQLite 检索到)被覆盖。
你的意思是在初始化时添加的listview的所有记录都应该在保存后删除。
不,不应删除所有记录。保存后,它应该创建新列表并添加到上一个列表下方
正是你想删除所有的sqllite,然后想创建一个新的,并想在之前的记录中添加新的记录..right【参考方案2】:
您可以使用 adapterNotifyChanged 从 listiview 添加新项目。
images.add(NewAdapterItemObject);
和
obj.notifyDataSetChanged();
完成了。
【讨论】:
以上是关于如何在已填充列表的 listView 中添加列表?的主要内容,如果未能解决你的问题,请参考以下文章