我刚刚开始学习Android。我不确定我的问题是否正确。我的目的是当用户按下按钮时将项目添加到listview。每个单项(行)有3个输入。目前我的代码在按下按钮时添加项目(行)。但它创建新数组并删除所有用户输入。我也几乎不认识Java(OOP技能非常少)。你能回答我做错了吗?
活动:
package com.example.gereltod.test7;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
public class SellActivity extends Activity {
ArrayList<Items> items = new ArrayList<Items>();
ItemsAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell);
adapter = new ItemsAdapter(this, items);
listView = (ListView) findViewById(R.id.listView_main);
listView.setAdapter(adapter);
}
public void add_item(View view) {
Log.i("clicked add item", "yes");
items.add(new Items("", "", ""));
adapter.notifyDataSetChanged();
}
}
布局
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_main"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/btn_add_item" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add_item"
android:id="@+id/btn_add_item"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="add_item" />
项目适配器
package com.example.gereltod.test7;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ItemsAdapter extends ArrayAdapter<Items> {
private static class ViewHolder {
TextView name;
TextView qty;
TextView price;
}
public ItemsAdapter(Context context, ArrayList<Items> items) {
super(context, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Items items = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.edittxt_name);
viewHolder.qty = (TextView) convertView.findViewById(R.id.edittxt_qty);
viewHolder.price = (TextView) convertView.findViewById(R.id.edittxt_price);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(items.name);
viewHolder.qty.setText(items.qty);
viewHolder.price.setText(items.price);
return convertView;
}
}
列出商品
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<EditText
android:id="@+id/edittxt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@drawable/input_shape"
android:hint="@string/hint_item_name"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:id="@+id/edittxt_qty"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.2"
android:background="@drawable/input_shape"
android:gravity="right"
android:hint="@string/hint_item_qty"
android:inputType="number"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:id="@+id/edittxt_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.3"
android:background="@drawable/input_shape"
android:gravity="end"
android:hint="@string/hint_item_price"
android:inputType="number"
android:padding="5dp"
android:singleLine="true" />
</TableRow>
</TableLayout>
编辑:由于我的英语很糟糕,我无法准确描述最新情况。所以我添加了截图。请参见此处:http://i.imgur.com/Yzw2s7D.jpg
答案 0 :(得分:1)
您每次创建新列表并将项目添加到列表中,然后将单个列表的适配器设置为listview。
在创建活动时设置适配器,然后将项添加到适配器并调用notifyDataSetChanged()方法。以下是活动代码:
public class SellActivity extends Activity {
ArrayList<Items> items = new ArrayList<Items>();
private ItemsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell);
ListView listView = (ListView) findViewById(R.id.listView_main);
adapter = new ItemsAdapter(this);
listView.setAdapter(adapter);
}
public void add_item(View view) {
Log.i("clicked add item", "yes");
adapter.add(new Items("", "", ""));
adapter.notifyDataSetChanged();
}
}
答案 1 :(得分:0)
在add_item方法中,您不应该创建新的适配器和对listView的新引用。在onCreate()方法中移动这些行
ItemsAdapter adapter = new ItemsAdapter(this, items);
ListView listView = (ListView) findViewById(R.id.listView_main);
listView.setAdapter(adapter);
add_item应该只添加item和notifyDataSetChanged()。
答案 2 :(得分:0)
我不确定但是试试这个
package com.example.gereltod.test7;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class SellActivity extends Activity {
ArrayList<Items> items = new ArrayList<Items>();
ItemsAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell);
adapter = new ItemsAdapter(this, items);
listView = (ListView) findViewById(R.id.listView_main);
listView.setAdapter(adapter);
}
public void add_item(View view) {
Log.i("clicked add item", "yes");
items.add(new Items("", "", ""));
adapter.notifyDataSetChanged();
}
}
我觉得这应该有用......
答案 3 :(得分:0)
您必须更改此方法。首先将适配器初始化为
onCreate() {
adapter = new ItemsAdapter(this, items);
listView = (ListView) findViewById(R.id.listView_main);
listView.setAdapter(adapter);
}
ItemsAdapter adapter;
ListView listView;
public void add_item(View view) {
Log.i("clicked add item", "yes");
items.add(new Items("", "", ""));
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 4 :(得分:0)
我认为你没有设置从输入到arraylist的信息,所以它里面没有任何信息。