【问题标题】:Button not showing in the custom listview of android按钮未显示在android的自定义列表视图中
【发布时间】:2015-05-21 04:11:02
【问题描述】:

我已经使用 ArrayAdapter 创建了自定义 Listview。下面是我的代码

adapter = new ArrayAdapter<Item>(this,
                android.R.layout.activity_list_item, android.R.id.text1,
                fileList) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // creates view
                View view = super.getView(position, convertView, parent);
                TextView textView = (TextView) view
                        .findViewById(android.R.id.text1);

                // put the image on the text view
                textView.setCompoundDrawablesWithIntrinsicBounds(
                        fileList[position].icon, 0, 0, 0);

                // add margin between image and text (support various screen
                // densities)
                int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                textView.setCompoundDrawablePadding(dp5);

                Button b1 = (Button)findViewById(android.R.id.button1);

                return view;
            }
        };

在上面的代码中按钮没有显示。如何显示带有 textview 和 image 的按钮。

谢谢

【问题讨论】:

  • 最好使用自定义适配器。

标签: android android-arrayadapter


【解决方案1】:

this 更改为get activity()getBaseContext

adapter = new ArrayAdapter<Item>(getActivity(),
                android.R.layout.activity_list_item, android.R.id.text1,
                fileList)

【讨论】:

  • 这之间没有任何联系。
【解决方案2】:

这里的问题是您使用 android 的 textview ""android.R.id.text1"" 并在 Arraylist 适配器中设置,因此您只能获得 Textview,因为您在适配器的 getview() 方法中返回视图

为按钮创建自定义视图。

创建xml文件cstom.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

创建您的自定义适配器类

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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyCustomAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private ArrayList<String> list;
    public MyCustomAdapter(Context context, ArrayList<String> list) {
        inflater=LayoutInflater.from(context);
        this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // creates view

        View view = inflater.inflate(R.layout.cstom, null);
        Button b1 = (Button) view.findViewById(android.R.id.button1);
        TextView textView = (TextView) view.findViewById(android.R.id.textView1);
        return view;

    }
}

从您的活动中设置

MyCustomAdapter adapter=new MyCustomAdapter(context,list);
listview.setAdapter(adapter);

【讨论】:

    【解决方案3】:

    您的代码已经是一个自定义适配器,但最好为此创建一个单独的类。无论如何,对于一个完善的代码,缺少holder。看看它是如何做到的:

    adapter = new ArrayAdapter<Item>(this,
                    android.R.layout.activity_list_item, android.R.id.text1,
                    fileList) {
    
                private ViewHolder mHolder;
    
                private static class ViewHolder {
    
                    TextView textView;
                    Button b1;
    
                }
    
                @Override
                public View getView(int position, View view, ViewGroup parent) {
    
                    if (view == null) {
    
                        mHolder = new ViewHolder();
    
                        LayoutInflater _layoutInflater = (LayoutInflater) mContext.getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
    
                        view = _layoutInflater.inflate(R.layout.custom, null);
    
                        mHolder.textView = (TextView) view.findViewById(R.id.text1);
                        mHolder.b1 = (Button) view.findViewById(R.id.button1);
    
                        view.setTag(mHolder);
    
                    }
                    else{
    
                        mHolder = (ViewHolder) view.getTag();
    
                    }
    
                    // put the image on the text view
                    mHolder.textView.setCompoundDrawablesWithIntrinsicBounds(
                            fileList[position].icon, 0, 0, 0);
    
                    // add margin between image and text (support various screen
                    // densities)
                    int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                    mHolder.textView.setCompoundDrawablePadding(dp5);
    
                    mHolder.b1.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
    
                        }
    
                    });
    
                    return view;
                }
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2013-03-10
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多