我对ListView
有疑问。是否可以将每个项目ImageView
取出并按下此A活动开始?
我不知道如何实施OnClickListener
方法。
有人可以告诉我吗?
我认为它属于CustomAdapter类,但不知道是否会失去意图从活动中转移,因为我在另一个不是活动正在运行的类中有适配器。我是否必须将自定义适配器类放在活动为ListView
的类中?
谢谢!
CustomHolder:
public class ObrasAdapter extends ArrayAdapter<Obra> {
private Context context;
private ArrayList<Obra> datos;
public ObrasAdapter(Context context, ArrayList<Obra> datos) {
super(context, R.layout.listview_item, datos);
this.context = context;
this.datos = datos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ObrasHolder holder;
if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);
holder = new ObrasHolder();
holder.foto = (ImageView) item.findViewById(R.id.imgAnimal);
holder.num = (TextView) item.findViewById(R.id.numC);
holder.iden = (TextView) item.findViewById(R.id.idenC);
holder.ubi = (TextView) item.findViewById(R.id.ubiC);
holder.hombres = (TextView) item.findViewById(R.id.homC);
holder.material = (TextView) item.findViewById(R.id.matC);
holder.eq1 = (TextView) item.findViewById(R.id.eq1);
holder.eq2 = (TextView) item.findViewById(R.id.eq2);
holder.eq3 = (TextView) item.findViewById(R.id.eq3);
holder.eq4 = (TextView) item.findViewById(R.id.eq4);
holder.g1 = (TextView) item.findViewById(R.id.g1);
holder.g2 = (TextView) item.findViewById(R.id.g2);
holder.g3 = (TextView) item.findViewById(R.id.g3);
holder.g4 = (TextView) item.findViewById(R.id.g4);
holder.fondo = (RelativeLayout) item.findViewById(R.id.fondobra);
holder.cam = (ImageView) item.findViewById(R.id.cam);
item.setTag(holder);
}
holder = (ObrasHolder) item.getTag();
holder.foto.setImageResource(datos.get(position).getDrawableImageID());
if(Integer.parseInt(datos.get(position).getHom().trim())/2==1)
holder.g1.setTextColor(Color.BLACK);
if(Integer.parseInt(datos.get(position).getHom().trim())/2==2)
{
holder.g1.setTextColor(Color.BLACK);
holder.g2.setTextColor(Color.BLACK);
}
if(Integer.parseInt(datos.get(position).getHom().trim())/2==3)
{
holder.g1.setTextColor(Color.BLACK);
holder.g2.setTextColor(Color.BLACK);
holder.g3.setTextColor(Color.BLACK);
}
if(Integer.parseInt(datos.get(position).getHom().trim())/2==4)
{
holder.g1.setTextColor(Color.BLACK);
holder.g2.setTextColor(Color.BLACK);
holder.g3.setTextColor(Color.BLACK);
holder.g4.setTextColor(Color.BLACK);
}
//Inicializamos los equipos
holder.eq1.setText("1");
holder.eq2.setText("2");
holder.eq3.setText("3");
holder.eq4.setText("4");
return item;
}
}
主要课程:
public class Inventario extends Activity implements OnItemClickListener {
private ArrayList<Obra> obras;
private ListView lvObras;
private ObrasAdapter adapter;
private TextView num, iden,ubi,hombres,material;
private int pulsado = -1;
List<String> equiDisp;
ArrayList<String> marcado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventario);
equiDisp = new ArrayList<String>();
marcado = new ArrayList<String>();
for(int i=1; i<11; i++)
{
equiDisp.add(String.valueOf(i));
}
obras = new ArrayList<Obra>();
rellenarArrayList();
actualizarDisplay();
}
public void actualizarDisplay()
{
adapter = new ObrasAdapter(this, obras);
lvObras = (ListView) findViewById(R.id.lvItems);
lvObras.setAdapter(adapter);
lvObras.setOnItemClickListener(this);
}
private void rellenarArrayList()
{
obras.add(new Obra("08", "San","S","4","P,C,2 ", R.drawable.sa008p,1,0));
...
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,long ID) {
pulsado = position;
final String[] opciones = new String[] { "Asignar Equipo", "Ver ficha","Marcar como rescatado"};
final AlertDialog.Builder dialogo = new AlertDialog.Builder(Inventario.this);
dialogo.setCancelable(true);
dialogo.setTitle("Accion a realizar: ");
dialogo.setItems(opciones, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0)
{
for(int i=0;i<marcado.size();i++)
{
for(int j=0; j<equiDisp.size(); j++)
{
if(marcado.get(i).trim().equals(equiDisp.get(j).trim()))
equiDisp.remove(j);
}
}
final String[] opciones= equiDisp.toArray(new String[equiDisp.size()]);
final boolean [] selected = new boolean[equiDisp.size()];
AlertDialog.Builder selec = new AlertDialog.Builder(Inventario.this);
selec.setTitle("Asigne equipo/equipos de rescate a la obra: ")
.setPositiveButton("Aceptar",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
actualizarDisplay();
}
})
.setMultiChoiceItems(opciones, selected, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialogInterface, int item, boolean b) {
marcado.add(opciones[item]);
obras.get(pulsado).equiA.add(Integer.parseInt(opciones[item]));
}
});
selec.create().show();
}
if (item ==1)
{
Intent passIntent = new Intent();
passIntent.setClass(Inventario.this, VerFicha.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("inv",obras.get(pulsado).getNum());
passIntent.putExtras(bundle);
startActivity(passIntent);
}
if (item ==2)
{
obras.get(pulsado).setEstado(1);
for (int j =0; j<marcado.size();j++)
{
for (int k =0; k<obras.get(pulsado).equiA.size();k++)
{
if(marcado.get(j)== String.valueOf(obras.get(pulsado).equiA.get(k)))
marcado.remove(j);
}
}
actualizarDisplay();
}
}
});
dialogo.create();
dialogo.show();
}
}
答案 0 :(得分:1)
public class YourAdapter extends BaseAdapter{
Context context;
public YourAdapter (Context c) {
context = c;
}
@Override
public int getCount() {
}
@Override
public Object getItem(int position) {
}
@Override
public long getItemId(int position) {
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView == null){
convertView = inflater.inflate(R.layout.layout_adapter_display_project_list, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, AnotherClass.class);
context.startActivity(intent);
}
});
return convertView;
}
static class ViewHolder{
public ImageView img;
}
}
答案 1 :(得分:0)
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
/*LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
label =(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);*/
//do anything
}
return row;
}
}