我有一个列表视图,其中包含带文本视图和隐藏图像视图的适配器类,其想法是,当用户长按一个项目时,在选择该选项后,将触发onCreateContextMenu事件以供用户单击该控件菜单,此行应该显示隐藏的图像,我尝试过,但是该图像仅出现在最后一行。 我的适配器类
public class Adaptergeneric extends BaseAdapter {
TextView txtDescart;
TextView txtrenglon;
TextView txtdespacho;
ImageView ImgStock;
Context context;
JSONArray oJS;
LayoutInflater inflater;
String song2;
public Adaptergeneric(Context context,JSONArray pJS) {
this.context = context;
this.oJS = pJS;
}
@Override
public int getCount() {
return oJS.length();
}
@Override
public Object getItem(int i) {
try {
return oJS.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_generic_pedidos, viewGroup, false);
txtrenglon = (TextView) itemView.findViewById(R.id.renglon);
txtDescart = (TextView) itemView.findViewById(R.id.descart);
txtdespacho = (TextView) itemView.findViewById(R.id.destino);
ImgStock = (ImageView) itemView.findViewById(R.id.imgEstado);
try {
txtDescart.setText(oJS.getJSONObject(i).getString("tareas"));
txtrenglon.setText(oJS.getJSONObject(i).getString("renglon"));
txtdespacho.setText(oJS.getJSONObject(i).getString("destino"));
return view;
} catch (JSONException e) {
e.printStackTrace();
}
return itemView;
}
public void function() {
ImgStock.setVisibility(View.VISIBLE);
}
}
还有我的Java类
protected String doInBackground(String... strings) {
final String url = "http://danbijann.freeiz.com/consulta.json";
HttpHandler sh = new HttpHandler();
JSONObject jsonStr = sh.makeServiceCall(url);
try {
contacts = jsonStr.getJSONArray("PEDIDOS");
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
pedidosAdap = new Adaptergeneric(Operaciones.this, contacts);
listaPedidos.setAdapter(pedidosAdap);
registerForContextMenu(listaPedidos);
}
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflate = getMenuInflater();
if (v.getId() == R.id.listaPedidos) {
inflate.inflate(R.menu.estado, menu);
}
}
public boolean onContextItemSelected(final MenuItem item) {
final Context context = this;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.mnu_estado:
pedidosAdap.function();
return super.onContextItemSelected(item);
}
return false;
}
那是我的结果 enter image description here