ImageButton出错

时间:2016-07-20 04:15:35

标签: android

我是错误的,不知道如何修复它们

MainActivity

setListAdapter(new ConteudoActivity(ctx, R.layout.suport_activity,
                option_icon, option_text));

ConteudoActivity

ImageButtonib = (ImageButton)convertView.findViewById(R.id.image_adicionar);
ib.setImageDrawable(mButton.getDrawable(position));

活动数组

package com.kolla.projeto;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ConteudoActivity extends ArrayAdapter < String > {

  private LayoutInflater mInflater;
  private ImageButton mButton;
  private String[] mStrings;
  private TypedArray mIcons;

  private int mViewResourceId;

  public ConteudoActivity(Context ctx, int viewResourceId,
    String[] strings, TypedArray icons, TypedArray ibutton) {
    super(ctx, viewResourceId, strings);

    mInflater = (LayoutInflater) ctx.getSystemService(
      Context.LAYOUT_INFLATER_SERVICE);

    mStrings = strings;
    mIcons = icons;
    mButton = ibutton;
    mViewResourceId = viewResourceId;
  }


  @
  Override
  public int getCount() {
    return mStrings.length;
  }

  @
  Override
  public String getItem(int position) {
    return mStrings[position];
  }

  @
  Override
  public long getItemId(int position) {
    return 0;
  }

  @
  Override
  public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(mViewResourceId, null);


    ImageView iv = (ImageView) convertView.findViewById(R.id.option_icon);
    iv.setImageDrawable(mIcons.getDrawable(position));

    ImageButton ib = (ImageButton) convertView.findViewById(R.id.image_adicionar);
    ib.setImageDrawable(ImageButton.getDrawable(position));


    TextView tv = (TextView) convertView.findViewById(R.id.option_text);
    tv.setText(mStrings[position]);

    return convertView;
  }
}

MainActivity

package com.kolla.projeto;


import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;

public class MainActivity extends ListActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context ctx = getApplicationContext();
        Resources res = ctx.getResources();

        String[] option_icon = res.getStringArray(R.array.Logo);
        TypedArray option_text = res.obtainTypedArray(R.array.NomeProd);

        setListAdapter(new ConteudoActivity(ctx, R.layout.suport_activity,
                option_icon, option_text));
    }
}

的ListView

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.kolla.projeto.MainActivity">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>


</RelativeLayout>

XML

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


    <ImageView
        android:id="@+id/option_icon"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <TextView
        android:id="@+id/option_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textSize="16dp"
        android:layout_weight="6">
    </TextView>

    <ImageView
        android:id="@+id/image_adicionar"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个,

image.setImageDrawable(getResources().getDrawable(R.drawable.drawable_name));