2014-10-02 2 views
0

내 채팅 페이지에 대한 맞춤 어댑터를 만들었습니다. listViewfirstUser을 표시하고 오른쪽에 표시 메시지를 표시하고 secondUser은 표시 메시지의 왼쪽에 상자를 엽니 다. 페이지가 가득차면 scroll 페이지. 메시지의 위치를 ​​바꾼다 ?? 내 코드는 거대하지만 내 Custom Adapter입니다 :위치 변경 ListView의 대화 메시지?

public class AdapterConversation extends ArrayAdapter<StructConversation>{ 
    public static String co; 
    public AdapterConversation(ArrayList<StructConversation> array){ 
     super(G.context, MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation , array); 
     MClear(); 
    } 
    private static class ViewHolder{ 
     public TextView NameTitle; 
     public TextView Conver; 
     public TextView dateTime; 

     public ViewHolder(View view){ 
      NameTitle = (TextView) view.findViewById(R.id.NameTitle); 
      Conver = (TextView) view.findViewById(R.id.Conver); 
      dateTime = (TextView) view.findViewById(R.id.dateTime); 
     } 
     public void fill(ArrayAdapter<StructConversation> adapter,final StructConversation item, int position){ 
      NameTitle.setText(item.NameTitle); 
      Conver.setText(item.Coversations); 
      dateTime.setText(item.dateTime); 
     } 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     StructConversation item = getItem(position); 
     if(convertView == null){ 
      convertView = G.inflater.inflate(MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation, parent,false); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.fill(this, item, position); 
     return convertView; 
    } 

    public static String MClear() { 
     SharedPreferences sharedPrefs = G.context.getSharedPreferences("CONVER", 0); 
     co = (sharedPrefs.getString("PSA", "not")); 
     sharedPrefs.edit().remove("PSA").commit(); 
     return co; 
    } 

} 
+0

죄송 홀더를 만들 때

private static class ViewHolder{ public TextView NameTitle; public TextView Conver; public TextView dateTime; } 

한 번만 멤버를 설정, 하지만 당신의 목적을 이해할 수 없었습니다. 정확히 무엇을하고 싶습니까? –

답변

1

당신의 viewHolder은 다음과 같아야합니다

if(convertView == null){ 
     convertView = G.inflater.inflate(MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation, parent,false); 
     holder = new ViewHolder(convertView); 
     // Use findViewById HERE!! 
     convertView.setTag(holder); 
    } 
+0

설명해주십시오 : // findViewById 사용! –

+0

viewHolder 패턴은 필수는 아니며 findViewById()가 성능이 느리기 때문에 너무 자주 사용하지 않으려는 경우에 사용됩니다. viewHolder 패턴은 매끄러운 목록 뷰 스크롤을 얻는 데 사용됩니다. 그러나이 패턴을 사용하고 findViewById를 사용하는 것은 이치에 맞지 않습니다! 아래 코드를 이동하여 viewHolder를 초기화하는 동안 findViewById를 한 번만 사용하십시오. 아무 방법도없이보기 소유자를 깨끗하게 둡니다. – jmhostalet

+0

@ jmhostalet. 감사 . –