簡體   English   中英

Android ListView 定位 TextView

[英]Android ListView positioning for TextView

我真的不知道從哪里開始問這個問題,所以我將盡我所能描述我的問題......我目前正在使用一個自定義列表適配器來處理一個 ListView,它會膨脹 2 個文本視圖和一個復選框。 一個 textView 是該選項的描述,另一個 textView 保存在我需要使用它來顯示數據時。 當用戶單擊 List 中的某個項目時,它將彈出一個 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);

/** 用於顯示行星對象數組的自定義適配器。 */ 私有 class 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;
        }

不要認為您可以從對話框視圖中設置這些文本視圖。 您必須從自定義列表適配器中設置它們。 像您一樣從對話框中獲取名稱,然后將名稱添加到自定義適配器的數據集中,最后再次設置適配器或 notifyDataSetChanged()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM