我使用两个ListViews
(List1有数据,List2为空)。
用户可以输入名称作为输入,如果List1包含名称并且按下了添加Button
,则标记应减少1并且ListViews
都应更新。
示例:
如果List1有10个标记并且输入了名称,则List1的标记应设为9,List2的标记应设为1。
如果list1具有相同的名称,则应更新具有第一个位置名称的索引。现在两个值都使用相同的名称更新。
如果我们这样做10次,则应删除List1名称,并将List2的标记设置为10。
示例2:
假设List1的标记设置为8,List2标记设置为2.如果按下子按钮,则应添加两个列表。此时,应删除List2,List1的总标记应为10。
以下是我使用过的逻辑,但它无法正常更新多个具有相同名称的行。
HomeAct类
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class HomeAct extends Activity {
List<DocItem> docDet1 = new ArrayList<DocItem>();
List<DocItem> docDet2 = new ArrayList<DocItem>();
ListView lv1, lv2;
EditText editText;
Button btn1, btn2;
DocDetAdapter adapter1, adapter2;
int n=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
lv1 = (ListView)findViewById(R.id.lv_det1);
lv2 = (ListView)findViewById(R.id.lv_det2);
editText = (EditText)findViewById(R.id.editText1);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
adapter1 = new DocDetAdapter(1);
adapter2 = new DocDetAdapter(2);
docDet1.add(new DocItem("1", "john", 20));
docDet1.add(new DocItem("2", "john", 20));
docDet1.add(new DocItem("3", "shaun", 60));
docDet1.add(new DocItem("4", "joy", 10));
lv1.setAdapter(adapter1);
lv2.setAdapter(adapter2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DocItem changeItem = null;
for (int i = 0; i < docDet1.size(); i++) {
DocItem docItem = docDet1.get(i);
if (docItem.name.equals(editText.getText().toString())) {
changeItem = docDet1.get(i);
changeItem.marks = changeItem.marks - 1;
if (findDocItem(editText.getText().toString()) != null) {
DocItem docI = findDocItem(editText.getText()
.toString());
docI.marks = docI.marks + 1;
} else {
docDet2.add(new DocItem(changeItem.docNo,
changeItem.name, 1));
}
}
if (docItem.marks == 0) {
docDet1.remove(i);
}
}
adapter2.notifyDataSetChanged();
adapter1.notifyDataSetChanged();
editText.setText("");
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int changedmarks=0;
DocItem item = null;
for (int i = 0; i < docDet2.size(); i++) {
DocItem docItem = docDet2.get(i);
item = docDet2.get(i);
if (docItem.name.equals(editText.getText().toString())) {
changedmarks =docDet2.get(i).marks;
docDet2.remove(i);
}
}
if(findDocItem2(editText.getText().toString())!=null )
{
DocItem docitem = findDocItem2(editText.getText().toString());
docitem.marks = docitem.marks+ changedmarks;
}
else{
System.out
.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- else loop ");
docDet1.add(0,item);
}
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
editText.setText("");
}
});
}
private class DocDetAdapter extends BaseAdapter {
int mode; // 1 or 2
public DocDetAdapter(int mode) {
this.mode = mode;
}
@Override
public int getCount() {
if (mode == 1)
return docDet1.size();
else
return docDet2.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater li = getLayoutInflater();
if (convertView == null)
convertView = li.inflate(R.layout.row_tray_det, null);
TextView tvItemName = (TextView) convertView
.findViewById(R.id.tv_item_name);
TextView tvRack = (TextView) convertView.findViewById(R.id.tv_rack);
TextView tvQty = (TextView) convertView.findViewById(R.id.tv_qty);
DocItem invItem;
if (mode == 1)
invItem = docDet1.get(position);
else
invItem = docDet2.get(position);
tvItemName.setText(invItem.docNo);
tvRack.setText(invItem.name);
tvQty.setText(invItem.marks + "");
return convertView;
}
}
}
DocItem类
public class DocItem {
public String docNo, name;
public Integer marks;
public DocItem(String docNo, String name, Integer marks) {
super();
this.docNo = docNo;
this.name = name;
this.marks = marks;
}
}
home_act.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:text="Add" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:text="Sub" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 1"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<ListView
android:id="@+id/lv_det1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 2"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<ListView
android:id="@+id/lv_det2"
android:layout_width="fill_parent"
android:layout_height="250dp" />
</LinearLayout>
row_tray_det
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:id="@+id/v_doc_seperator"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/blue"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/row_bg_transparent_white"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_rack"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Sl no"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_qty"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:paddingRight="3dp"
android:text="Marks"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>