2013-08-27 55 views
1

我有一個自定義列表視圖與3 textview和5 imageview(明星)。前兩個textview是可見的,第三個是不可見的。我從服務器獲取數據並將其放入listview中,無論多少。onClick上的ListView和ListItem行爲

我想

如果我對任何明星單擊(1或2或3或4或5)我想從TextView的是隱藏得ID。並點擊它後明星imageview更改其圖像。假設我點擊第三顆星,那麼3顆星將被評級,其他評級將被降級。

我做什麼

現在我能夠從服務器獲取數據,並把它放到列表視圖但假設我再單擊最後一個項目的明星在改變,而不是點擊項目自身形象的任何項目的明星。並且它從隱藏的textview中獲取最後的id。

這是我的代碼。

public class MyArrayAdapter extends ArrayAdapter<Student> { 
StudentWrapper StudentWrapper = null; 
Context context; 
int layoutResourceId; 
Student student; 
ArrayList<Student> students = new ArrayList<Student>(); 

public MyArrayAdapter(Context context, int layoutResourceId,ArrayList<Student> studs) { 
    super(context, layoutResourceId, studs); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.students = studs; 
} 

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



    if (item == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     item = inflater.inflate(layoutResourceId, parent, false); 
     StudentWrapper = new StudentWrapper(); 
     StudentWrapper.menuname = (TextView) item.findViewById(R.id.title); 
     StudentWrapper.hotelname = (TextView) item.findViewById(R.id.artist); 
     StudentWrapper.menuid = (TextView) item.findViewById(R.id.tvhotelid); 
     StudentWrapper.firstStar = (ImageView)item.findViewById(R.id.hotelListFirstStar); 
     StudentWrapper.secondStar = (ImageView)item.findViewById(R.id.hotelListSecondStar); 
     StudentWrapper.thirdStar = (ImageView)item.findViewById(R.id.hotelListThirdStar); 
     StudentWrapper.fourthStar = (ImageView)item.findViewById(R.id.hotelListFourthStar); 
     StudentWrapper.fifthStar = (ImageView)item.findViewById(R.id.hotelListFifthStar); 
     StudentWrapper.firstStar.setFocusable(false); 
     StudentWrapper.secondStar.setFocusable(false); 
     StudentWrapper.thirdStar.setFocusable(false); 
     StudentWrapper.fourthStar.setFocusable(false); 
     StudentWrapper.fifthStar.setFocusable(false); 
     item.setTag(StudentWrapper); 
    } else { 
     StudentWrapper = (StudentWrapper) item.getTag(); 
    } 

    student = students.get(position); 
    StudentWrapper.menuname.setText(student.getMenuName()); 
    StudentWrapper.hotelname.setText(student.gethotenName()); 
    StudentWrapper.menuid.setText(String.valueOf(student.getMenuId())); 

    StudentWrapper.fifthStar.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "Edit", Toast.LENGTH_LONG).show(); 
      StudentWrapper.firstStar.setImageResource(R.drawable.rated); 
      StudentWrapper.secondStar.setImageResource(R.drawable.rated); 
      StudentWrapper.thirdStar.setImageResource(R.drawable.rated); 
      StudentWrapper.fourthStar.setImageResource(R.drawable.rated); 
      StudentWrapper.fifthStar.setImageResource(R.drawable.rated); 
     } 
    }); 

    StudentWrapper.secondStar.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      StudentWrapper.firstStar.setImageResource(R.drawable.rated); 
      StudentWrapper.secondStar.setImageResource(R.drawable.rated); 
      StudentWrapper.thirdStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.fourthStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.fifthStar.setImageResource(R.drawable.unrated); 
      Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
    StudentWrapper.firstStar.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      StudentWrapper.firstStar.setImageResource(R.drawable.rated); 
      StudentWrapper.secondStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.thirdStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.fourthStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.fifthStar.setImageResource(R.drawable.unrated); 
      Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
    StudentWrapper.fourthStar.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
       StudentWrapper.firstStar.setImageResource(R.drawable.rated); 
      StudentWrapper.secondStar.setImageResource(R.drawable.rated); 
      StudentWrapper.thirdStar.setImageResource(R.drawable.rated); 
      StudentWrapper.fourthStar.setImageResource(R.drawable.rated); 
      StudentWrapper.fifthStar.setImageResource(R.drawable.unrated); 
      Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
    StudentWrapper.thirdStar.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
        StudentWrapper.firstStar.setImageResource(R.drawable.rated); 
      StudentWrapper.secondStar.setImageResource(R.drawable.rated); 
      StudentWrapper.thirdStar.setImageResource(R.drawable.rated); 
      StudentWrapper.fourthStar.setImageResource(R.drawable.unrated); 
      StudentWrapper.fifthStar.setImageResource(R.drawable.unrated); 
      Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return item; 

} 

static class StudentWrapper { 
    TextView menuid; 
    TextView menuname; 
    TextView hotelname; 
    ImageView firstStar; 
    ImageView secondStar; 
    ImageView thirdStar; 
    ImageView fourthStar; 
    ImageView fifthStar; 
} 

}

如果我點擊任何項目,然後它反映了在最後一個項目的任何明星。我想更改該項目的imageview,並想從該項目的textview中獲取文本。
請給我任何暗示或引用

回答

1

的child.Because我這樣做的活動,其中listview存在,不在customadapter中。

adapterListview = new ListArrayAdapter(this, items); 
    this.listview.setAdapter(adapterListview); 

    listview.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      // global variable 
      idItem = (int) id; 

      switch (idItem) { 

      case 0: 
       // set your text and image for this item 
      break; 

      case 1: 
       // set your text and image for this item 

      break; 

      case 2: 
       // set your text and image for this item 

      break; 

      case 3: 
       // set your text and image for this item 

      break; 
     } 
     } 
    }); 

ID = 0,在第一星,ID = 1時的第二星形等等...

0

可能是烏拉圭回合的ListView正在觸摸事件,並傳送到每個這一切的開始都強調