我已经读过这个令人困惑的材料," listview滚动丢失的内容"这让我感到沮丧。所以我决定在这里问。这就是我需要的:
(TextItems) (TextValue)
Item1 0
Item2 0
Item3 0
// after input custom edittext dialog, It must be:
Item1 3
Item2 8
Item3 5
但是,在我向下滚动屏幕后(屏幕上没有显示3个项目)然后再次向上滚动,这些项目值又回到0.这是我的Main.java:
String[] items = {"Item1","Item2","Item3","Item4",..,"Item30"};
final ListView listView = (ListView)findViewById(R.id.listItems);
ArrayAdapter<String> adapter = new LVAdapter(this, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);
LayoutInflater inflater = Main.this.getLayoutInflater();
View v = inflater.inflate(R.layout.input_dialog, null);
alert.setView(v);
final EditText userInput = (EditText)v.findViewById(R.id.editInput);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView)view.findViewById(R.id.textValue); // for giving value on each Items
tv.setText(userInput.getText());
}
});
AlertDialog al = alert.create();
al.show();
}
});
然后,它是我的LVAdapter.java
public class LVAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] val;
public LVAdapter(Context context, String[] val) {
super(context, R.layout.detail_item, val);
this.context = context;
this.val = val;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.detail_item, parent, false);
TextView tv = (TextView)v.findViewById(R.id.textItems); //for storing Item1, Item2, .., etc.
tv.setText(val[position]);
return v;
}
main.xml中
<ListView
android:id="@+id/listItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants" >
</ListView>
及其main_items.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textItems"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentLeft="true"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- I need to set this textView based on users input -->
<TextView
android:id="@+id/textValue"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentRight="true"
android:text="0"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我在这里。堆叠如何实现&#34;它叫什么&#34;为了保持我的价值,即使我在屏幕上滚动。提前谢谢。
答案 0 :(得分:1)
ListView正在回收其子视图,并且不会自动保存您的输入。这意味着你必须自己做肮脏的工作。首先保存输入然后检索它并将其显示在正确的位置。 堆栈上有一些答案你可以查看,但无论如何这里是我使用的代码:
public class ArrayListAdapter extends BaseAdapter{
ArrayList<Product> productlist= null;
private Activity context=null;
public ArrayListAdapter(Activity context, ArrayList<Product> objects) {
this.productlist= objects;
this.context=context;
}
static class ViewHolder{
TextView textvw;
EditText edittx;
int ref;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(productlist!=null&&productlist.size()>0)
return productlist.size();
return 1;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return productlist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView==null){
holder= new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView=inflater.inflate(R.layout.list_item, null);
holder.textvw =(TextView)convertView.findViewById(R.id.name);
holder.edittx =(EditText)convertView.findViewById(R.id.quantity);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
holder.ref=position;
holder.textvw.setText(productlist.get(position).name);
holder.edittx.setText(productlist.get(position).quantity);
holder.edittx.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
productlist.get(holder.ref).quantity= s.toString();
productlist.get(holder.ref).quantity);
productlist.get(holder.ref+1).quantity );
}
});
return convertView;
}
}
答案 1 :(得分:1)
您需要将输入的值设置为传递给适配器的列表或数组。
像这样:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);
LayoutInflater inflater = Main.this.getLayoutInflater();
View v = inflater.inflate(R.layout.input_dialog, null);
alert.setView(v);
final EditText userInput = (EditText)v.findViewById(R.id.editInput);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Don't set text view item here
// Here you need to set value to your array or list which you passed to adapter for listview
items[position] = userInput.getText().toString();
adapter.notifyDataSetChanged(); // Use this for reflects the entered value
}
});
AlertDialog al = alert.create();
al.show();
}
});
答案 2 :(得分:0)
错误在你的getView方法中。你需要像这样修改你的getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.detail_item, parent, false);
holder. tv = (TextView)v.findViewById(R.id.textItems);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder. tv.setText(val[position]);
return v;
}
还添加持有者类
class ViewHolder {
TextView tv;
}
答案 3 :(得分:0)
创建视图的Hashmap并在其中添加listview项视图,如果您的项已经存在于hashmap中则滚动,然后从hashmap中检索该项。这不会导致您的内容丢失。
HashMap<Integer, View> hashMap = new HashMap<Integer, View>();
public View getView(final int position, View view, ViewGroup parent)
{
final ViewHolder holder;
if (hashMap.containsKey(position)) {
return hashMap.get(position);
}
holder = new ViewHolder();
view = inflater.inflate(R.layout.detail_item, null);
// set tag and put views in hashmap
view.setTag(holder);
hashMap.put(position, view);
return view;
}
希望这会奏效......谢谢