2012-02-22 5 views
0

ff와 비슷한 프로젝트를 만들어야합니다. 사진. (siily 그림을 사용하는 것에 대한 나의 사과, 나는 즉시 이용할 수있는 것과 함께해야한다).
enter image description here왼쪽의 ImageView가있는 다중 라인 ListView


나는 내가 '동적'의 ListView를 만드는 가장 좋은 방법이었다 무슨 짓을했는지 언급하지 않았다하지만 난 현재 일이 무엇을 만들 다행 것. 다음과 같이 어쨌든 내 구현은 다음과 같습니다

public class MultiLineListViewActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ArrayList<SearchResults> searchResults = getSearchResults(); 

    ListView listView = (ListView) findViewById(R.id.listView); 
    listView.setAdapter(new MyCustomBaseAdapter(this, searchResults)); 
} 

private ArrayList<SearchResults> getSearchResults() { 
    ArrayList<SearchResults> results = new ArrayList<SearchResults>(); 

    SearchResults sr1 = new SearchResults(); 
    sr1.setName("Name 1"); 
    sr1.setPhone("12345"); 
    SearchResults.setIcon(R.drawable.pic_one); 
    results.add(sr1); 

    sr1 = new SearchResults(); 
    sr1.setName("Name 2"); 
    sr1.setPhone("123456"); 
    SearchResults.setIcon(R.drawable.pic_two); 
    results.add(sr1); 

    ... 

    return results; 
} 
} 


공용 클래스의 SearchResult {

private String name; 
private String phone; 
public static ArrayList<Integer> iconsList = new ArrayList<Integer>(); 

public void setName(String name) { 
    this.name = name; 
} 

public void setPhone(String phone) { 
    this.phone = phone; 
} 

public static void setIcon(int i) { 
    iconsList.add(i); 
} 

public String getName() { 
    return this.name; 
} 

public String getPhone() { 
    return this.phone; 
} 
} 


public class MyCustomBaseAdapter extends BaseAdapter { 

private ArrayList<SearchResults> searchArrayList; 
private LayoutInflater layoutInflater; 
private Context context; 

public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) { 
    this.searchArrayList = results; 
    //this.context = context; 
    layoutInflater = LayoutInflater.from(context); 
} 

@Override 
public int getCount() { 
    return searchArrayList.size(); 
} 

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

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    ImageView imageView; 

    if(convertView == null) { 
     convertView = layoutInflater.inflate(R.layout.custom_row_view, null); 

     holder = new ViewHolder(); 
     holder.txtName = (TextView) convertView.findViewById(R.id.name); 
     holder.txtPhone = (TextView) convertView.findViewById(R.id.phone); 

     imageView = new ImageView(context); 

     convertView.setTag(holder); 
    } 
    else { 
     //imageView = (ImageView) convertView; 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    imageView = new ImageView(context); 
    imageView.setImageResource(SearchResults.iconsList.get(position)); 

    holder.txtName.setText(searchArrayList.get(position).getName()); 
    holder.txtPhone.setText(searchArrayList.get(position).getPhone()); 

    return convertView; 
} 

class ViewHolder { 
    TextView txtName; 
    TextView txtPhone; 
} 


위의 코드는 이름을 표시에서 성공을Phone ListView에있는 전화, 그것은 'Force Close'를주는 이미지를 추가 한 것입니다.

주석 처리 된 라인을 메모하십시오. imageView = (ImageView) convertView; 댓글을 삭제하면 앱이 다운되는 점에서 특정 라인까지 줄였습니다.

답변

3

참고 : StackOverflow 질문에서 강제 종료 할 때마다 전체 스택 추적을 게시하십시오.

실제로 새 것을 만들 필요가 없을 때 항상 새로운 ImageView을 생성합니다. TextView과 같이 사용자 정의 항목 레이아웃에 정의해야합니다 (이 경우 게시해야합니다). 동일한 방법으로 findViewById을 사용할 수 있습니다.

+0

귀하의 제안이 현장에있었습니다! 예를 들어, layout.custom_row_view.xml에 ImageView를 추가하고 getView() 내에서 imageView의 모든 선언과 인스턴스를 제거했다면 중첩 된 LinearLayouts (RelativeLayout도 마찬가지 임)를 사용하여 예제와 유사한 뷰를 정렬했습니다. ! 그것은 고의로 지금 고의로 일하고있다, 감사합니다! –

3

카 부코는 두 가지 모두에 적합합니다. (업데이트 : 아래 그의 주석을 무시해서는 안됩니다.) 귀하의 layout/custom_row_view이 LinearLayout 인 것으로 추측됩니다. 이는 주석 처리 된 선이 LinearLayout을 ImageView로 강요하려고 시도한다는 것을 의미합니다. 그래야 예외가 발생합니다. 또한, 귀하의 ViewHolder 클래스는 불필요, 그냥 ... 그래서처럼 getView()

// Assuming that layout/custom_row_view is a LinearLayout... 
getView(yadda) { 
    LinearLayout row; 
    ImageView imageView; 
    TextView txtName, txtPhone; 

    if (convertView == null) { 
     row = // inflate a new one 
    } else { 
     row = // get the old one 
    } 

    // fetch the views 
    imageView = row.findViewById(...); 
    txtName = row.findViewById(...); 
    txtPhone = row.findViewById(...); 

    // fill the views 
    imageView.setImageResource(...); 
    txtName.setContent(...); 
    txtPhone.setContent(...); 

    return row; 
} 

챠오을 변수로하는 TextViews을 정의합니다.

+1

'ViewHolder' 클래스는 실제로 유효한 성능 향상입니다. 'findViewById'는 항상 싸지는 않지만 때로는 그 처리를 저장하기 위해 조금 더 많은 메모리를 먹어 치울만한 가치가 있습니다. 예제는 [this] (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html)를 참조하십시오. – kabuko