在listview点击事件上更新ImageView

时间:2015-04-13 18:05:48

标签: android arrays listview imageview custom-adapter

我正在尝试在我的BikeActivity上获取一个图像,以便在自定义适配器中单击一行时进行更新,但是我无法找到代码来执行此操作,并且我尝试的所有内容都导致程序崩溃。

我希望ImageView iv在listitem上更新点击并使用阵列中该行上的图像进行更新,任何帮助表示赞赏!欢呼声。

    public class BikeActivity extends Activity {

    ListView lv;
    Context context;
    TextView tv;
    ImageView iv;
    ArrayList prgmName;
    public static int [] prgmImages={R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6};
    public static String[] bikes = new String[]{
        "Liteville 301,  Enduro",
                "Liteville 601,  Downhill",
                "Liteville 901,  Downhill",
                "Liteville Marathon, XC",
                "Liteville Werksmachine, Freeride",
                "Liteville AllMountain"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bike);

        context=this;

        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, bikes,prgmImages));
        iv=(ImageView) findViewById(R.id.imageView2);
        iv.setImageResource(R.drawable.images1);
        tv=(TextView) findViewById(R.id.textinfo);
        tv.setText(String.valueOf("BLAHBLAHTESTESTLDKSJHF:"));






    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bike, menu);
        return true;
    }

}

自定义适配器的代码:

    public class CustomAdapter extends BaseAdapter{
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(BikeActivity bikeActivity, String[] bikes, int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=bikes;
    context=bikeActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {

    return result.length;
}

@Override
public Object getItem(int position) {
    return position;
}

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

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.list_item, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);


    rowView.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {


         Toast.makeText(context, ""+result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;

}



}

list_item.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" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="TextView" />

activity_bike.xml:

<LinearLayout 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:orientation="vertical"
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=".BikeActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textStyle="bold"
    android:text="Liteville Bikes:" />

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="295dp" >
</ListView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_gravity="center_horizontal"
    android:contentDescription="@string/bike" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textinfo" />

2 个答案:

答案 0 :(得分:1)

那么这个

   rowView.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {
     iv.setImageResource(imageId[position]);
     //set position as final
     Toast.makeText(context, ""+result[position], Toast.LENGTH_LONG).show();
    }
});

答案 1 :(得分:0)

首先在CustomAdapter中初始化iv

 ImageView iv=(ImageView) findViewById(R.id.imageView2);  

然后试试这个:

 rowView.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
 iv.setImageResource(imageId[position]);
 //set position as final
 Toast.makeText(context, ""+result[position], Toast.LENGTH_LONG).show();
}
});

喜欢这个

public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(BikeActivity bikeActivity, String[] bikes, int[]    prgmImages) {
// TODO Auto-generated constructor stub
result=bikes;
context=bikeActivity;
imageId=prgmImages;
ImageView iv=(ImageView) findViewById(R.id.imageView2); 
}
@Override
public int getCount() {

return result.length;
}

@Override
public Object getItem(int position) {
return position;
}

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

public class Holder
{
TextView tv;
ImageView img;
}
@Override
 public View getView(final int position, View convertView, ViewGroup parent)
  {       final Holder holder=new Holder();
View rowView=convertview;
inflater = ( LayoutInflater )context.
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, null);

holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);


rowView.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {

    iv.setImageResource(imageId[position]);
     Toast.makeText(context, ""+result[position], Toast.LENGTH_LONG).show();
    }
});

return rowView;

}

}