在我的应用程序中,我想更改用户单击的列表视图中的imageview。开始我通过适配器类传递列表视图中的图像,但点击项目我想更改图像视图中的图像在listview中,我如何在特定位置更改图像。??
MainActivity
public class MainActivity extends Activity {
ListView listView;
public static int pos = 0;
String[] Prayers = new String[] { "REFLETINDO A IMAGEM DE CRISTO",
"PROVIDÊNCIA PARA TODOS OS CASOS", "A CRUZ REVELA O AMOR DE DEUS",
"REFLETINDO O AMOR DE CRISTO", "CRISTO CRUCIFICADO POR NÓS",
"AMOR IMENSURÁVEL", "DÁDIVA DO AMOR DE DEUS",
"NÃO PARA CONDENAR, MAS PARA SALVAR",
"O CENTRO DE MINHA ESPERANÇA",
};
String[] Dias = new String[] { "Dia1", "Dia2", "Dia3", "Dia4", "Dia5",
"Dia6", "Dia7", "Dia8", "Dia9", "Dia10", "Dia11", "Dia12",
"Dia13", "Dia14", "Dia15", "Dia16", "Dia17", "Dia18",
};
LinearLayout lay1 ,lay2;
int i=0;
int img ;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
img = R.drawable.praying_hands_normal ;
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < 50; i++) {
RowItem item = new RowItem( Dias[i], Prayers[i],img);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.listview);
BaseClass adapter = new BaseClass(this, rowItems);
listView.setAdapter(adapter);
//listView.setOnItemClickListener(this);
// get the position of the clicked item
View t = null;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
pos = position;
//Toast.makeText(getApplication(), String.valueOf(pos), Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this ,OpenActivity.class);
startActivity(i);
}
});
}
Adapterclass
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.txtDias = (TextView) convertView.findViewById(R.id.dias);
holder.txtPrayers = (TextView) convertView
.findViewById(R.id.prayers);
holder.imageView = (ImageView) convertView
.findViewById(R.id.symbol);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtPrayers.setText(rowItem.getDesc());
holder.txtDias.setText(rowItem.getTitle());
holder.imageView.setImageResource(R.drawable.praying_hands_normal);
if(pos == position && OpenActivity.a + OpenActivity.b +
OpenActivity.c == 1 || OpenActivity.a + OpenActivity.b +
OpenActivity.c == 2)
{
holder.imageView.setImageResource(R.drawable.praying_hands_bookmark);
}
else if(pos == position && OpenActivity.a + OpenActivity.b +
OpenActivity.c == 3 )
{
holder.imageView.setImageResource(R.drawable.praying_hands_selected);
}
return convertView;
}
答案 0 :(得分:0)
onItemClick()方法中的参数具有“视图视图”,其中afaik是您按下的项目。 view.findViewById()关注该行。
试试这个。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Add following to your listener
ImageView image = (ImageView) view.findViewById( R.id.item_image );
image.setBackground( getResources().getDrawable( R.drawable.new_item_image ));
// Or
Drawable img = ( Drawable ) getResources().getDrawable( R.drawable.new_item_image ));
image.setBackground( img );
}
});
编辑:在您从意图返回时保持项目更改,请尝试此
在MainActivity中,添加一个View,Context&amp; ImageView的,
public class MainActivity extends Activity {
public View row;
public Context ctx;
public ImageView newImage;
// your code
在onItemClickListener中添加此内容,
row = view;
ctx = getBaseContext();
newImage = ( ImageView ) getResources().getDrawable( R.drawable.new_item_image ));
现在添加一个名为“setImage”的新方法(在MainActivity类中),
public void setImage( View row ) {
ImageView item_image = ( ImageView ) row.findViewById( R.id.item_image );
item_image.setBackground( newImage );
}
当您离开新活动时,请先调用它,
MainActivity.setImage( MainActivity.row );
告诉我这是否有效,我自己没试过。
答案 1 :(得分:0)
RowItem
仍然保留旧的ImageId,这就是为什么上一张图片看得见,只需在项目点击时修改RowItem
的imageId:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RowItem rowItem = rowItems.get(position);
rowItem.setImageId(R.drawable.new_item_image);
Intent i = new Intent(MainActivity.this ,OpenActivity.class);
startActivity(i);
}
--------------- 3小时后更新---------------
将BaseClass
保存在onCreate()方法之外,通知onResume()更改的数据集:
public class MainActivity extends Activity {
private BaseClass adapter;
protected void onCreate(Bundle savedInstanceState) {
...
adapter = new BaseClass(this, rowItems);
...
}
@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
}
顺便说一下,你也应该发布BaseClass
代码。