在列表视图点击事件上更新 ImageView
Update ImageView on listview click event
我试图让我的 BikeActivity 上的图像在单击自定义适配器中的一行时更新,但我无法找到执行此操作的代码,我尝试过的所有操作都导致程序崩溃。 .
我希望 ImageView iv 在列表项单击时更新并更新数组中该行上的图像,我们将不胜感激!干杯。
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" />
那么这个呢
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();
}
});
首先在您的 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;
}
}
我试图让我的 BikeActivity 上的图像在单击自定义适配器中的一行时更新,但我无法找到执行此操作的代码,我尝试过的所有操作都导致程序崩溃。 .
我希望 ImageView iv 在列表项单击时更新并更新数组中该行上的图像,我们将不胜感激!干杯。
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" />
那么这个呢
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();
}
});
首先在您的 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;
}
}