【问题标题】:Android ListView positioning for TextView用于 TextView 的 Android ListView 定位
【发布时间】:2011-07-15 22:29:05
【问题描述】:

我真的不知道从哪里开始问这个问题,所以我会尽力描述我的问题......我目前正在使用一个自定义列表适配器来处理一个 ListView,它会膨胀 2 个文本视图和一个复选框。一个 textView 是该选项的描述,另一个 textView 保存在我需要使用它来显示数据时。当用户单击列表中的项目时,它将弹出一个editText 对话框。我得到它来保存文本并显示它,但我遇到的问题是,当它显示数据时,它不在正确的 ListView 项目上......例如:http://www.vbsteven.be/blog/wp-content/uploads/2009/07/screenshotcalllog.png 假设我想在 listView 的第一个槽中显示我从对话框中获得的信息。它随机显示在第 3 个第 2 个或第 4 个位置。这似乎是一个如此简单的问题,但我已经挣扎了一段时间......

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/rowTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="70dp"
    android:padding="12dp"
    android:textSize="16sp" >
  </TextView>

  <TextView android:id="@+id/alarm_name_text" 
   android:layout_height="wrap_content" 
   android:text="" 
   android:layout_toRightOf="@+id/rowTextView" 
   android:layout_width="fill_parent">
   </TextView>

  <CheckBox android:id="@+id/CheckBox01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp"
    android:layout_alignParentRight="true" android:layout_marginRight="6sp"
    android:focusable="false">
  </CheckBox>

</RelativeLayout>

活动:

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_TEXT_ENTRY:

            LayoutInflater mInflater = LayoutInflater.from(this);
            final View textEntryView = mInflater.inflate(
                    R.layout.alert_text_entry, null);
            /*final View textEntryView1 = mInflater.inflate(R.layout.row, null);*/
            final EditText savedText = ((EditText)textEntryView.findViewById(R.id.password_edit));
            final TextView rowSavedText = ((TextView)findViewById(R.id.alarm_name_text));
            return new AlertDialog.Builder(AlarmList.this)
                    .setIcon(R.drawable.alert_icon)
                    .setTitle("Enter your name")
                    .setView(textEntryView)
                    .setPositiveButton("accept",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    String temp = savedText.getText().toString();
                                    rowSavedText.setText(temp);
                                    rowSavedText.setVisibility(0);
                                }
                            })
                    .setNegativeButton("Cancel", new OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create();
        }
        return null;
    }

如果有人想查看我的代码的任何其他部分以寻求帮助,请告诉我。提前致谢!

这是我的列表适配器和其中的数据:

planets = new Planet[] { new Planet("Turn On Alarm"),
                    new Planet("Alarm Name"), new Planet("Time"),
                    new Planet("Sound"), new Planet("Vibrate"),
                    new Planet("Repeat"), new Planet("Volume Control"),
                    new Planet("Second Alarm") };
        }
        ArrayList<Planet> planetList = new ArrayList<Planet>();
        planetList.addAll(Arrays.asList(planets));


listAdapter = new PlanetArrayAdapter(this, planetList);

/** 用于显示行星对象数组的自定义适配器。 */ 私有类 PlanetArrayAdapter 扩展 ArrayAdapter {

    private LayoutInflater inflater;
    public PlanetArrayAdapter(Context context, List<Planet> planetList) {
        super(context, R.layout.row, R.id.rowTextView, planetList);

        // Cache the LayoutInflate to avoid asking for a new one each time.
        inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Planet to display
        Planet planet = (Planet) this.getItem(position);


        // The child views in each row.
        CheckBox checkBox;
        TextView textView;
        Spinner mySpinner;


// Create a new row view
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.row, null);

                // Find the child views.
                textView = (TextView) convertView
                        .findViewById(R.id.rowTextView);
                checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
                // Optimization: Tag the row with it's child views, so we don't
                // have to
                // call findViewById() later when we reuse the row.
                convertView.setTag(new PlanetViewHolder(textView, checkBox));

                // If CheckBox is toggled, update the planet it is tagged with.
                checkBox.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Planet planet = (Planet) cb.getTag();
                        planet.setChecked(cb.isChecked());
                    }
                });
            }
            // Reuse existing row view
            else {
                // Because we use a ViewHolder, we avoid having to call
                // findViewById().
                PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
                        .getTag();
                checkBox = viewHolder.getCheckBox();
                textView = viewHolder.getTextView();
            }

            // Tag the CheckBox with the Planet it is displaying, so that we can
            // access the planet in onClick() when the CheckBox is toggled.
            checkBox.setTag(planet);

            // Display planet data
            checkBox.setChecked(planet.isChecked());
            textView.setText(planet.getName());

            return convertView;
        }

【问题讨论】:

    标签: android listview textview position dialog


    【解决方案1】:

    不要认为你可以从对话框视图中设置那些文本视图。您必须从自定义列表适配器中设置它们。像你一样从对话框中获取名称,然后将名称添加到自定义适配器的数据集中,最后再次设置适配器或 notifyDataSetChanged()。

    【讨论】:

    • 我不太明白你的意思。如果我如何从 dialogView 中获取名称,然后在范围之外使用它?另外,我应该在 getView() 方法中设置我的 TextViews 吗?当我添加数据集时,我是否将其传入?或者只是定义一个数据。抱歉,我对 android 很陌生,我们的大学没有老师。
    • 从对话框中获取文本,然后将其添加到您的 ArrayList 或您用于适配器的任何结构中,当您添加它时,调用 YourCursor.notifyDataSetChanged(),然后光标将重新填充列表视图和将对话框中的数据放在您的列表视图中。不要忘记调用 YourCursor.notifyDataSetInvalidated();这将导致列表视图重绘自身,包括新数据。我敢打赌这就是你想要做的。
    • 有什么方法可以聊天吗?您所说的实际上是有道理的,我只是不确定如何添加该字符串。
    • 我的意思是,文本被存储并放入 ListView 视图之一的 textView 中。只是不是正确的视图...
    • Listview 不止一个视图。它就像一堆视图。当您从对话框中调用 rowSavedText.setText(temp) 时,它会将其设置在适配器当时正在绘制/设置的任何视图上。这就是为什么你必须对 arraylist/adapter 进行更改,然后让它重新填充 listview。你能发布你的自定义列表适配器吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多