与ListVIew对话中的问题

时间:2011-05-27 13:05:48

标签: android listview dialog

以下是我使用ListView进行Dialog的代码,在这里我还维护状态,当用户点击列表时,背景将填充绿色,如下图所示

但我遇到的问题是

1>我想增加行高。怎么办?

2>并且我还希望将图像放在所有行中这可能吗?

Here is my image in which i want to put image beside all list items

这是我所有这些东西的代码。

 package com.android.listselector;

 import java.util.ArrayList;
 import java.util.List;

 import android.app.Activity;
 import android.app.Dialog;
 import android.content.Context;
 import android.graphics.Color;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.AdapterView.OnItemClickListener;

  public class ListSelector extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
private Context mContext = ListSelector.this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.selected_example);
    String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula",
            "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat",
            "placerat", "ante", "porttitor", "sodales", "pellentesque",
            "augue", "purus" };
    // populate the model - a simple a list
    list = new ArrayList<String>();
    for (int i = 0; i < items.length; i++) {
        list.add(items[i]);

    }
    // create our SelectedAdapter
    selectedAdapter = new SelectedAdapter(this, 0, list);
    selectedAdapter.setNotifyOnChange(true);

    Dialog dialog = new Dialog(mContext);
    dialog.setContentView(R.layout.selected_example);
    dialog.setTitle("Custom Dialog");
    ListView listview = (ListView) dialog.findViewById(R.id.listExample);

    listview.setAdapter(selectedAdapter);

    dialog.show();

    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long id) {
            // user clicked a list item, make it "selected"
            selectedAdapter.setSelectedPosition(position);
        }
    });
}

// move up event handler

// move down event handler

// Move selected item "up" in the ViewList.
private void moveUp() {
    int selectedPos = selectedAdapter.getSelectedPosition();
    if (selectedPos > 0) {
        String str = list.remove(selectedPos);
        list.add(selectedPos - 1, str);
        // set selected position in the adapter
        selectedAdapter.setSelectedPosition(selectedPos - 1);
    }
}

// Move selected item "down" in the ViewList.
private void moveDown() {
    int selectedPos = selectedAdapter.getSelectedPosition();
    if (selectedPos < list.size() - 1) {
        String str = list.remove(selectedPos);
        list.add(selectedPos + 1, str);
        // set selected position in the adapter
        selectedAdapter.setSelectedPosition(selectedPos + 1);
    }
}

public class SelectedAdapter extends ArrayAdapter<String> {

    // used to keep selected position in ListView
    private int selectedPos = -1; // init value for not-selected

    public SelectedAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
    }

    public void setSelectedPosition(int pos) {
        selectedPos = pos;
        // inform the view of this change
        notifyDataSetChanged();
    }

    public int getSelectedPosition() {
        return selectedPos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        // only inflate the view if it's null
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.selected_row, null);
        }

        // get text view
        TextView label = (TextView) v.findViewById(R.id.txtExample);

        // change the row color based on selected state
        if (selectedPos == position) {
            label.setBackgroundColor(Color.GREEN);
        } else {
            label.setBackgroundColor(Color.WHITE);
        }

        label.setText(this.getItem(position).toString());
        /*
         * // to use something other than .toString() MyClass myobj =
         * (MyClass)this.getItem(position);
         * label.setText(myobj.myReturnsString());
         */
        return (v);
    }
}

}

以下是我的代码中使用的布局

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


    <ListView
android:id="@+id/listExample"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#CCCCCC"
    android:choiceMode="singleChoice"
     />

     <LinearLayout
     android:orientation="horizontal"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     >
     <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:id="@+id/btnMoveUp"
     />

     <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:id="@+id/btnMoveDown"
     />

     </LinearLayout>

这是另一个

   <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
    </TextView>

6 个答案:

答案 0 :(得分:2)

使用以下代码来满足您的期望。图像视图位于文本视图的下方。如果你想看到taxt视图右侧的图像视图,而不是在linearlayout中使用android:orientation =“vertical”,请使用android:orientation =“horizo​​ntal”

selected_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
    </TextView>
    <ImageView
        android:id="@+id/accountIcon"
        android:layout_width="wrap_content"![enter image description here][1]
        android:layout_height="wrap_content"
        android:background="@drawable/btn_public_message_focus"
        android:layout_marginRight="6dip" />
        </LinearLayout>

如果我的回复对您有用,那就不要投票。

由于 迪帕克

enter image description here

答案 1 :(得分:0)

  1. 您应该自定义列表视图以增加行高。
  2. 只能在对话框中使用自定义列表视图放置图像。

    check this

答案 2 :(得分:0)

如果要使用不同的设置制作ListView,则必须为该列表视图创建自定义ListView和CustomAdapter。

http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html

http://www.androidpeople.com/android-custom-listview-tutorial-part-2

答案 3 :(得分:0)

您必须为此添加单独的布局才能在单元格中添加图像

答案 4 :(得分:0)

将此布局用于列表行.....

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

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:id="@+id/txtExample" android:textSize="18sp" android:textColor="#000000"
    android:background="#FF0000">
        </TextView>


      <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/icon"
 />

    </LinearLayout>

答案 5 :(得分:0)

是的,我在这里得到所有答案的解决方案

首先我必须通过此

修改selected_row xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" android:layout_height="100dip">
 <TextView android:layout_width="fill_parent"
    android:layout_height="40px" android:id="@+id/txtExample"
    android:textSize="18sp" android:textColor="#000000"
    android:gravity="center_vertical"
     />
<ImageView android:layout_width="wrap_content"
    android:layout_height="40px" android:id="@+id/imgbeside"
    android:scaleType="center" android:layout_centerVertical="true"
    android:layout_alignParentRight="true" android:src="@drawable/selector"
    android:layout_marginRight="10dip" />

并且在我的java文件中,我将以下内容放在此行下面

  TextView label = (TextView) v.findViewById(R.id.txtExample);
  ImageView mImageview = (ImageView) v.findViewById(R.id.imgbeside); 

这对我有用。谢谢所有的回应。