2017-05-08 77 views
0

標籤設置爲一排我想設置一個標籤的行(或至少一些行中,最好行雖然)。然後當我點擊該行時,我可以得到標籤。我認爲這就是我必須做的事情,但無論我點擊什麼,標籤總是爲空。如何在自定義適配器的Android工作室的Java

我的代碼示例顯示了我在標籤設定爲一個TextView的嘗試,因爲我不知道如何將其設置爲行。

這是我customAdapter類

int counter = 0; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 

    View customView = layoutInflater.inflate(R.layout.my_row, parent, false); 

    ViewHolder holder = new ViewHolder(); 

    holder.name = (TextView)customView.findViewById(R.id.nameTextView); 
    holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView); 

    holder.name.setText(allNames.get(position)); 
    holder.picture.setImageResource(allPictures.get(position)); 

    //This does not work ---> holder.name.setTag(counter); 

    counter ++; 
    return customView; 
} 

我getView代碼這是我在不同的類

listVIew.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) { 

      Toast.makeText(getApplicationContext(), "tag is " + view.getTag(), Toast.LENGTH_SHORT).show(); 

     } 
    }); 

任何幫助,將不勝感激onClickListener。謝謝。

回答

1

試試這個,

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 

     View customView = layoutInflater.inflate(R.layout.my_row, parent, false); 

     ViewHolder holder = new ViewHolder(); 

     holder.name = (TextView)customView.findViewById(R.id.nameTextView); 
     holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView); 

     holder.name.setText(allNames.get(position)); 
     holder.picture.setImageResource(allPictures.get(position)); 

     //This does not work ---> holder.name.setTag(counter); 

     customView.setTag(Integer.valueOf(counter)); 

     counter ++; 
     return customView; 
    } 
+0

工程。看着這個,我意識到我之前用精確的代碼嘗試過,但我不小心使用了「convertView」而不是「customView」。謝謝。 – Z10987654321X

1

試試這個:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 

    if(convertView == null) { 

     // inflate the layout 
     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
     convertView = layoutInflater.inflate(R.layout.my_row, parent, false); 

     // set up the ViewHolder 
     holder = new ViewHolder(); 

     holder.name = (TextView) convertView.findViewById(R.id.nameTextView); 
     holder.picture = (ImageView) convertView.findViewById(R.id.pictureImageView); 

     // store the holder with the view. 
     convertView.setTag(holder); 

    } else { 
     // we've just avoided calling findViewById() on resource everytime 
     // just use the viewHolder 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.name.setText(allNames.get(position)); 
    holder.picture.setImageResource(allPictures.get(position)); 
    holder.name.setTag(counter); 

    counter++; 

    return convertView; 
} 
+0

此方法有效,但如果我從此頁面切換回去並返回(但不使用Intent返回),則會出現錯誤。由於Krish的答案有效,所以沒有必要修復我遇到的錯誤。儘管謝謝你的幫助。 – Z10987654321X

相關問題