我失去了一整天试图找出如何使这项工作。
我成功设置了自定义适配器和我的ListView
。
问题是我得到了我点击的项目的正确位置,但该项目始终是列表中的最后一项
更重要的是,我在另一个线程中调用所有这些来执行PostCall。
我尝试了其他的例子,当然也跟随官方的开发者资源,但我无法弄清楚我做错了什么。
protected void onPostExecute(String result) {
try {
ListView serviceList = (ListView)findViewById(android.R.id.list);
String[] menu = result.split("_");
ListaMenuActivity adapter = new ListaMenuActivity(ListaCibo.this, menu);
serviceList.setAdapter(adapter);
serviceList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(ListaCibo.this, String.valueOf(position) + parent.getAdapter().getItem(position), Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error_string, Toast.LENGTH_LONG).show();
}
}
我也尝试使用parent.getItemAtPosition()
,也使用了View,总是一样的结果。
修改 这是我的适配器的代码 public ListaMenuActivity(Activity context,String [] itemname){ super(context,R.layout.menu_custom,itemname);
this.context=context;
this.itemname=itemname;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.menu_custom, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
TextView extratxt = (TextView) rowView.findViewById(R.id.ingredienti);
ImageView image = (ImageView) rowView.findViewById(R.id.vegan);
try{
if(itemname[position]!="Medium Text") {
Log.e("basd", itemname[position]);
String [] elems = itemname[position].split(", ");
txtTitle.setText(elems[0] + ", " + elems[2]);
extratxt.setText(elems[1].replace("-", ", "));
if(!elems[3].equals("1")) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.leaf);
vegan.setImageBitmap(icon);
}
desc_piatto = elems[4];
nome_foto = elems[5];
nomepiatto = elems[0];
}
} catch (Exception e){e.printStackTrace();}
return rowView;
}
@Override
public String getItem(int position) {
return nomepiatto + "," +nome_foto + "," + desc_piatto;
}
答案 0 :(得分:1)
在getView中设置内容并且之后将其置于getItem()内部。
您的代码如何工作(示例)
这是对Adapter的getItem的错误使用。阅读有关ListView如何膨胀视图和适应性生命周期
的内容P.S。使用lowerCamelCase进行变量命名,并使用ViewHolder作为适配器
修改强> 更好,但不是最好的解决方案
class YourAwesomeObject {
String[] elems = null;
public YourAwesomeObject(String inputString) {
elems = inputString.split(", ");
}
public String getTitle() {
return elems[0] + ", " + elems[2];
}
public String getExtraTxt() {
return elems[1].replace("-", ", ");
}
public boolean isShowImage() {
return !elems[3].equals("1");
}
public String getDescPiatto() {
return elems[4];
}
public String getNomeFoto() {
return elems[5];
}
public String getNomePiatto() {
return elems[0];
}
@Override
public String toString() {
return getNomePiatto() + "," +getNomeFoto() + "," + getDescPiatto();
}
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.menu_custom, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
TextView extratxt = (TextView) rowView.findViewById(R.id.ingredienti);
ImageView image = (ImageView) rowView.findViewById(R.id.vegan);
try {
if (itemname[position] != "Medium Text") {
Log.e("basd", itemname[position]);
YourAwesomeObject item = getItem(itemname[position])
txtTitle.setText(item.getTitle());
extratxt.setText(item.getExtraTxt());
if (item.isShowImage()) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.leaf);
vegan.setImageBitmap(icon);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return rowView;
}
@Override
public YourAwesomeObject getItem(int position) {
return new YourAwesomeObject(itemname[position]);
}
答案 1 :(得分:0)
如果您的列表包含完整且正确的数据,并且您的位置正确,那么您可以使用您获得的位置直接从列表中获取值。请尝试以下代码段
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data = menu[position];
Toast.makeText(ListaCibo.this, String.valueOf(position) + data, Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
您应该将getItem
重写为:
@Override
public String getItem(int position) {
String [] elems = itemname[position].split(", ");
return elems[0] + "," +elems[5] + "," + elems[4];
}