保持textview内容在listview中滚动 [英] Keep textview content in listview scrolling

查看:57
本文介绍了保持textview内容在listview中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到了这种令人困惑的材料,列表视图滚动丢失了内容",这让我感到沮丧.所以我决定在这里问.这是我需要的:

I have read about this confusing material, "listview scrolling lost content" and It made me frustrated. So I decide to ask here. Here is what I need:

(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:

But, after I scroll down my screen(3 items isnot displayed on screen) then sroll up again, those item values is back to 0. Here is my 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

Then, its my 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

And Its 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>

我在这里.堆叠如何实现它叫什么"以保持我的价值,即使滚动屏幕也是如此.预先感谢.

Here I am. Stack in how to implement "what is it called" for keeping my value, even I srolling my screen. Thanks in advance.

推荐答案

您需要将输入的值设置为传递给适配器的列表或数组.

You need to set entered value to list or array you passed to your 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) {
                       // 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();
            }
        });

这篇关于保持textview内容在listview中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆