实际上当我触摸列表视图时我有一个列表视图当我选中复选框时我会弹出一个弹出窗口,然后按OK然后关闭对话框,当我再次触摸列表视图然后检查行position将改变背景颜色并存储到数据库中。实际上问题是当我再次选择不同的项目位置并检查并按下确定并再次触摸列表视图而不是进入检查位置进入数据库时先前检查的位置也与当前检查的位置一起进入数据库。所以我需要在列表视图看到已检查的位置之前获得先前的位置和清除位置。所以它只进入当前位置。
我的活动
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
//listView1.setTag(position);
list1 = (ListView) dialog.findViewById(R.id.list1);
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button) dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
return true;
}
});
代码简化:
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
list1=(ListView )dialog.findViewById(R.id.list1);
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this,newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button)dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Connection con1 = DbHandler.dbConnection();
try{
PreparedStatement stmt1 = con1
.prepareStatement("Select Line_No, ItemName,DeleteFlag from PendingOrders_Dtl where Inv_No=? ");
stmt1.setString(1,invNo);
ResultSet rsSetup1 = stmt1.executeQuery();
if (rsSetup1.next()) {
ItemsBean bean1 = new ItemsBean();
bean1.setLinenum(rsSetup1.getInt("Line_No"));
bean1.setProdnum(rsSetup1.getInt("ItemName"));
bean1.setDeleteFlag(rsSetup1.getInt("DeleteFlag"));
disablelist.add(bean1);
}
CustomAdapter adapter1 = new CustomAdapter(PendingOrdersActitvity.this, itemsList1);
for(int i=0;i<itemsList1.size();i++)
{
for(int j=0;j<disablelist.size();j++)
{
if(itemsList1.get(i).getProdnum()==disablelist.get(j).getProdnum())
{
itemsList1.get(i).setSelection(true);
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
答案 0 :(得分:0)
尝试这样的事情
int previousPosition;
public int getPreviousPosition() {
return previousPosition;
}
public void setPreviousPosition(int previousPosition) {
this.previousPosition = previousPosition;
}
并在列表视图中选择项目选择的侦听器
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int prePos= getPreviousPosition();
// do what ever you want to do with this prePos
View viewchild= parent.getChildAt(getPreviousPosition());
CheckBox prevCheckBox=(CheckBox)viewchild.findViewById(R.id.Checkbox_sort_list);
prevCheckBox.setChecked(false);
//at ending of list view write this
setPreviousPosition(position);
}
});
答案 1 :(得分:0)
我认为您已经在itemList1中保留了已检查的位置,因此在将其复制到newListitems2之后,您需要清除newListitems2中的已检查位置。 试试这个:
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
//listView1.setTag(position);
list1 = (ListView) dialog.findViewById(R.id.list1);
// Uncheck all items of newListitems2
for(int i=0; i<newListitems2.size(); i++){
ItemsBean tmpItem = newListitems2.get(i);
tmpItem.setChecked(false);
newListitems2.remove(i);
newListitems2.add(i, tmpItem);
}
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button) dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
return true;
}
});
对于另一个问题:
方法1:
如果弹出对话框中的列表与主列表相同,则可以尝试:当对话框关闭时,从弹出对话框中获取列表并将其复制到主列表。
在Adapter类中添加方法以从adapter2获取列表:
public ArrayList<ItemsBean> getItemList(){
return newListitems;
}
点击确定按钮后,覆盖主列表并关闭对话框:
itemsList1 = adapter2.getItemList();
adapter1 = new ItemBumpingAdapter(PendingOrdersActitvity.this, itemList1);
adapter1.notifyDataSetChanged();
dialog.dismiss();
方法2:
向adapter1添加方法以检查项目:
public void checkItemsAccordingDatabase(){
for(int i=0; i<newListItems; i++){
ItemsBean tmpItem = newListItems.get(i);
// query tmpItem in database.
// if tmpItem exits, newListItems.get(i).setChecked(true).
}
notifyDataSetChanged();
}
点击确定按钮后,更新主列表并关闭对话框:
adapter1.checkItemsAccordingDatabase();
dialog.dismiss();