ListView和按钮

时间:2014-02-22 18:13:56

标签: android listview button

我设法创建了我的ListView,其中包含大约10个项目,但这只不过是一个列表。我想添加一些功能,但我不知道如何。我需要在每个项目旁边添加按钮,这些按钮将被称为“查看更多”,然后将显示有关所单击项目的更多信息。任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

在提出问题之前,在您的情况下技术上是 请求 ,请访问此页面并熟悉人们提问的方式here

链接:https://stackoverflow.com/questions/how-to-ask


我为你制作了一个简单的应用程序,它有一个包含五个项目的列表,每个项目都有一个名为view more的按钮。当用户触摸该按钮时,描述将弹出警告对话框。

对于布局,我们有一个简单的主要布局,只有listview,仅此而已。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView_MainItems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


因为我们希望自定义列表,所以列表项目必须有一个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtItemTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnViewMore"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btnViewMore"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:text="@string/nullText"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnViewMore"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/ReadMore" />

</RelativeLayout>

现在是类的编码部分:

我们的主要活动类有一个简单的功能,用我定义的五个项目填充列表:

public class MainActivity extends Activity {

    private ListView lstMainItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstMainItems = (ListView) findViewById(R.id.listView_MainItems);
        fillList();
    }

    // This functions fills the list
    private void fillList() {
        ArrayList<ListProperty> ItemHolder = new ArrayList<ListProperty>();

        ListProperty item0 = new ListProperty();
        ListProperty item1 = new ListProperty();
        ListProperty item2 = new ListProperty();
        ListProperty item3 = new ListProperty();
        ListProperty item4 = new ListProperty();

        item0.setTitle("Item 1");
        item0.setDesc("(1) some description places here...");

        item1.setTitle("Item 2");
        item1.setDesc("(2) some description places here...");

        item2.setTitle("Item 3");
        item2.setDesc("(3) some description places here...");

        item3.setTitle("Item 4");
        item3.setDesc("(4) some description places here...");

        item4.setTitle("Item 5");
        item4.setDesc("(5) some description places here...");

        ItemHolder.add(item0);
        ItemHolder.add(item1);
        ItemHolder.add(item2);
        ItemHolder.add(item3);
        ItemHolder.add(item4);

        MyListAdapter adapter = new MyListAdapter(MainActivity.this,
                R.layout.list_main, ItemHolder);
        lstMainItems.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

为了制作我们的自定义列表功能,我们需要两个基本类:

  1. 一个属性类,负责设置/获取标题和描述。
  2. 一个ArrayAdapter类,旨在将用户界面元素分配给我们的自定义布局文件并处理按钮的单击事件:“查看更多”
  3. Property类代码:

    public class ListProperty {
    
        // Getting and Setting Item Title
        public void setTitle(String itemTitle) {
            this.itemTitle = itemTitle;
        }
    
        // Getting Item Title
        public String getTitle() {
            return itemTitle;
        }
    
        // Setting Item Description
        public void setDesc(String itemDesc) {
            this.itemDesc = itemDesc;
        }
    
        // Getting Item Description
        public String getDesc() {
            return itemDesc;
        }
    
        // Defining Variables
        private String itemTitle;
        private String itemDesc;
    }
    

    ArrayAdapter类代码:

    public class MyListAdapter extends ArrayAdapter<ListProperty> {
    
        private static List<ListProperty> Items;
        private Activity context;
        private LayoutInflater l_inflater;
    
        public MyListAdapter(Activity context, int textViewResourceId,
                List<ListProperty> Items) {
            super(context, R.id.listView_MainItems, textViewResourceId, Items);
            this.context = context;
            this.Items = Items;
        }
    
        private class ViewHolder {
            TextView txtTitle;
            Button btnViewMore;
        }
    
        public ListProperty getItem(int position) {
            return Items.get(position);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            LayoutInflater inflater = context.getLayoutInflater();
    
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_main, null);
                holder = new ViewHolder();
    
                holder.txtTitle = (TextView) convertView
                        .findViewById(R.id.txtItemTitle);
                holder.btnViewMore = (Button) convertView
                        .findViewById(R.id.btnViewMore);
    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            final ListProperty property = (ListProperty) getItem(position);
            holder.txtTitle.setText(property.getTitle());
    
            holder.btnViewMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder msgBuilder = new Builder(context);
                    msgBuilder.setTitle(property.getTitle());
                    msgBuilder.setCancelable(false);
                    msgBuilder.setMessage(property.getDesc());
                    msgBuilder.setPositiveButton("Return", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    AlertDialog alertRING = msgBuilder.create();
                    alertRING.show();
                }
            });
    
            return convertView;
        }
    }
    

    我希望你得到你想要的东西。