我已经制作了listViewAdapter,它从三个EditText中获取数据并在其上显示,但问题是,如果我点击添加按钮,那么第一个条目将由新条目覆盖。我想要添加数据在列表。
这是我的android代码:
public class List extends AppCompatActivity {
private EditText etItem;
private EditText etQty;
private EditText etRate;
public static String item;
public static String qty;
public static String rate;
private ArrayList<HashMap> list;
public listviewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
etItem=(EditText)findViewById(R.id.etItem);
etQty=(EditText)findViewById(R.id.etQty);
etRate=(EditText)findViewById(R.id.etRate);
item=etItem.getText().toString();
qty=etQty.getText().toString();
rate=etRate.getText().toString();
Button bAdd=(Button)findViewById(R.id.bAdd);
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListView lview = (ListView) findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
adapter.notifyDataSetChanged();
etItem.setText("");
etQty.setText("");
etRate.setText("");
}
});
}
private void populateList() {
list = new ArrayList<HashMap>();
item=etItem.getText().toString();
qty=etQty.getText().toString();
rate=etRate.getText().toString();
HashMap temp = new HashMap();
temp.put(FIRST_COLUMN,"");
temp.put(SECOND_COLUMN, item);
temp.put(THIRD_COLUMN, qty);
temp.put(FOURTH_COLUMN, rate);
list.add(temp);
}
答案 0 :(得分:0)
您必须使所有可重用参数必须全局。全局生成ListView对象和Adapter Class对象以及List数据,不要使用新构造函数重新赋值。在onCreate方法上添加以下代码并从重新赋值中删除。
ListView lview;
listviewAdapter adapter;
onCreate方法中的
lview = (ListView) findViewById(R.id.listview);
list = new ArrayList<HashMap>();
adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
现在在populateList()方法中添加三个参数,如
populateList(String item,String qty,String rate)
现在使用
从Button onClick事件添加数据item=etItem.getText().toString();
qty=etQty.getText().toString();
rate=etRate.getText().toString();
populateList(item,qty,rate);
并调用notifyDataSetChange来刷新适配器和ListView,在populateList方法的最后一行添加它
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
将hashmap打包到列表(适配器)是一个不可见的:)
1)创建简单的pojo
https://en.wikipedia.org/wiki/Plain_Old_Java_Object
public class Item {
private String _itemName;
private int _qty;
private int _rate;
public Item(String itemName, int qty, int rate) {
_itemName = itemName;
_qty = qty;
_rate = rate;
}
// create getter/setter if need
public String getName() { return _itemName;}
// if you want create setter
// then keep in mind your
// item _itemName variable could not be marked as final
public void setName(String name) { _itemName = name;}
}
2)创建适配器扩展,例如BaseAdapter类 https://developer.android.com/reference/android/widget/BaseAdapter.html
public class MyAdapter extends BaseAdapter {
private List<item> _myList;
public MyAdapter(Context context, List<Item> myList) {
// context is useful to use for create an inflater and getView method
_context = context;
// check on list here
// instantiate new if need (if you plane to use method on list)
// to avoid null pointer exceptions
_myList = myList;
}
// implement rest methods methods for base adapter
// getView() , id etc
// implement methods to add delete clear (on list)
public void addItem(Item item) {
// do some chcks on list is not empty etc
_myList.add(item);
// notify adapter observers to update their states
notifyDatasetChanged();
}
}
3)使用带列表视图的适配器:
// create list to hold items
List<Items> listItems = new ArrayList<>()
// create item 1
Item item1 = new Item(String,int,int);
// add item 1
listItems.add(item1);
Item item2 = new Item(String,int,int);
listItems.add(item2)
// create adapter
MyAdapter myAdapter = new MyAdapter(Context, listItems)
//set adapter
listView.setAdapter(myAdapter);
// later you can re-user adapter
Adapter adapter = listView.getAdapter()
if(adapter !=- null && MyAdapter.class.isAssingableFromClass(adapter.getClass) {
// do cast
MyAdapter myAdapter = MyAdapter(adapter);
// add new item
myAdapter.addItem( new Item (...););
}
完整示例:
https://github.com/c3ph3us/examples/blob/master/VaultListAdapter.java
其他示例和信息:
如果你只是加载一个简单的对象列表,那么ArrayAdapter比BaseAdapter更受欢迎 - cricket_007
您的意思是使用还是延长?
ArrayAdapter:
由任意对象数组支持的具体BaseAdapter。 默认情况下,此类要求提供的资源ID引用单个TextView 。如果要使用更复杂的布局,请使用也带有字段ID的构造函数。该字段id应引用较大布局资源中的TextView。
我认为没有任何意义需要在这里利用更多资源
在我看来,使用更复杂的类/库不会让你学习语言 - 你应该使用低级方法来获得新技能
答案 2 :(得分:-1)
我希望这会对你有所帮助:
在onCreate()
中执行此操作 cross = (ImageButton) findViewById(R.id.cross);
lview = (ListView) findViewById(R.id.listview);
lview.setItemsCanFocus(false);
clicks();
}
然后添加此方法:
private void populateList() {
HashMap temp = new HashMap();
list = new ArrayList<HashMap>();
item = etItem.getText().toString();
qty = etQty.getText().toString();
rate = etRate.getText().toString();
temp.put(FIRST_COLUMN, "1");
temp.put(SECOND_COLUMN, item);
temp.put(THIRD_COLUMN, qty);
temp.put(FOURTH_COLUMN, rate);
list.add(temp);
}
private void populateList1() {
HashMap temp1 = new HashMap();
i++;
sno = Integer.toString(i);
item = etItem.getText().toString();
qty = etQty.getText().toString();
rate = etRate.getText().toString();
temp1.put(FIRST_COLUMN, sno);
temp1.put(SECOND_COLUMN, item);
temp1.put(THIRD_COLUMN, qty);
temp1.put(FOURTH_COLUMN, rate);
list.add(temp1);
adapter.notifyDataSetChanged();
}
public void clicks() {
Button bAdd = (Button) findViewById(R.id.bAdd);
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (click == true) {
populateList();
adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
etItem.setText("");
etQty.setText("");
etRate.setText("");
click = false;
} else if (click == false) {
populateList1();
etItem.setText("");
etQty.setText("");
etRate.setText("");
}
}
});
}
希望我能帮助你:)。