2014-01-09 5 views
0

내 목록보기를 사용자 정의하여 이미지 아이콘과 텍스트보기를 추가하려고합니다. 배열 목록을 XML 파일로 변환해야합니다. 내부 스토리지에 저장된 xml 파일에이 어레이 목록 (FTP 서버에 저장된 파일 이름 포함)을 쓰도록 도와주세요.XML 파일에 arraylist 쓰기

+0

왜 XML로 변환해야합니까? 캐싱 목적입니까? – ayorhan

+0

내가 배열 목록을 사용자 지정 목록보기에 직접 바인딩 할 수 없기 때문에 XML 파일로 변환하여 arraylist >을 얻을 수 있습니다. – Nishad

답변

0

XML 파일에 arraylist를 쓸 필요가 없습니다. 하나의 어댑터 클래스를 생성하고 생성자에 arraylist로 컨텍스트를 보유하십시오.

그런 다음 어댑터 클래스를 초기화 할 때 활동에 arraylist 데이터를 전달하고 getView()를 통해 사용자 정의 xml보기의 모든 데이터를 추가하십시오.

편집

어댑터 클래스는 같은 것을해야한다 (사용자 정의 레이아웃이 뷰는 하나의 텍스트 뷰와 하나 개의 체크 박스 즉,이 가정) :

public class YourAdapter extends BaseAdapter { 
private ArrayList<HashMap<String, String>> mArrayListData= null; 
private Context mContext; 

//Define your constructor in which you pass your arraylist data with usable activity context 
public StatusAdapter(Context mContext, ArrayList<HashMap<String, String>> mArrayListData) { 
this.mContext = mContext; 
this.mArrayListData= mArrayListData; 
} 

@Override 
public int getCount() { 
    return data.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

private class MyViewHolder { 
    TextView fillData; 
    CheckBox statusCheck; 

    MyViewHolder(View v) { 
     statusCheck = (CheckBox) v.findViewById(R.id.checkit); 
     fillData = (TextView) v.findViewById(R.id.textViewFillData); 
     } 
    } 

    //Do whatever you want to do with your custom layout here 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    final MyViewHolder holder; 
    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.yourcustomlayout, null); 
     holder = new MyViewHolder(view); 
     view.setTag(holder); 
    } else { 

     holder = (MyViewHolder) view.getTag(); 
    } 

      HashMap<String, String> value = new HashMap<String, String>(); 
    value = data.get(position); 
      holder.fillData.setText(value.get("Your_HashMap_Key"); 
      holder.statusCheck.setText(value.get("Your_HashMap_Key"); 
    holder.statusCheck.setTextColor(Color.CYAN); 

      return view; 
     } 
    } 

하고 활동에

는 youradapter의 클래스를 초기화를 컨텍스트와 arraylist 데이터를 확인한 다음 어댑터 객체를 listadapter로 사용하십시오.

YourAdapter mAdapter = new YourAdapter(YourActivity.this, your_arraylist); 
your_list.setAdapter(mAdapter); 

참고 :이 코드는 대략 적으므로 어떤 경우라도 낙타 사례 오류는 피하십시오. 감사합니다.

+0

제안 해 주셔서 감사합니다. 그러나 안드로이드 프로그래밍에 익숙한 샘플 코드를 제공해 주실 수 있습니까? – Nishad

+0

내 수정 된 답변보기 – Ranjit