2014-12-14 3 views
1

안녕하세요 저는 "Person"유형의 각 객체를 일식 안드로이드의 Gridview에 "Person"유형의 각 객체를 삽입해야하는 프로젝트에서 작업하고 있습니다. 나의 의도는 Person의 이름과 함께 그림을 보여주는 것이다 (Instagram과 비슷하다). 지금까지 시도한 것은 있지만 지금까지는 이해하기 힘든 것처럼 보이지만 작동하지 않는 것 같습니다.안드로이드에서 GridView에 ArrayList 객체 추가하기

yall에는 더 좋은 해결책이 있습니까?

ArrayList<Person> dbPersons = dop.getPersons(dop); //This is where I populate my list 
int[] imageId = { 
      R.drawable.image1, 
      R.drawable.image2, 
      R.drawable.image3 
    }; 

      gridView = (GridView)findViewById(R.id.grid); 
      gridView.setAdapter(new function.CustomGrid(this, dbPersons, imageId)); 
      gridView.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // TODO Auto-generated method stub 

       } 

      }); 

내 사람의 클래스는 일반적으로 포함

private String FName; 
private String LName; 
private String Biography; 

어쨌든 나는 깨끗하고 더 나은 대안을 찾고 있어요 때문에 내 잘못된 코드이 게시물을 도배하고 싶지 않았다. 그리드 뷰 항목의 제목과 그림 아래에 이름을 표시하고 나머지는 arraylist에 대해 동일하게 처리하기 만하면됩니다. 당신이 나 좀 도와 주 시겠어요들 :

+0

여기를 확인하십시오. 'http://www.javacodegeeks.com/2013/08/android-custom-grid-view-example-with-image-and-text.html' –

+0

@Jedil 귀하의 링크가 죽었다고 생각합니다. 그러나 Benildus, 상황에 따라 작성된 코드를 원하십니까?> – Elltz

+0

@Elltz 친구가 아니라 내 상황과 비슷한 샘플로 충분합니다. 나는 그것을 통해 내 방식대로 일할 수 있습니다. 객체를 포함하는 arrayList가 gridview에 채워지는 샘플을 원합니다. – Pjayness

답변

3

난 그냥 설정 어댑터를 잊지 어댑터

public class Benildus_Adapter extends ArrayAdapter<Person> { 

ArrayList<Person> list; // your person arraylist 
Context context; // the activity context 
int resource; // this will be your xml file 

public Benildus_Adapter(Context context, int resource,ArrayList<Person> objects) { 
    super(context, resource, objects); 
    // TODO Auto-generated constructor stub 
    this.list = objects; 
    this.context = context; 
    this.resource = resource; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    if(list.size() == 0){ 
     return 0; 
    }else{ 
     return list.size(); 
    } 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View child = convertView; 
    RecordHolder holder; 
    LayoutInflater inflater = ((Activity) context).getLayoutInflater(); // inflating your xml layout 

    if (child == null) {    
     child = inflater.inflate(resource, parent, false); 
     holder = new RecordHolder(); 
     holder.fname = (TextView) child.findViewById(R.id.fname); // fname is the reference to a textview 
     holder.lname = (TextView) child.findViewById(R.id.lname); // in your xml layout file 
     holder.bio =(TextView) child.findViewById(R.id.bio); // you are inflating.etc 
     child.setTag(holder); 
    }else{ 
     holder = (RecordHolder) child.getTag(); 
    } 

    final Person user = list.get(position); // you can remove the final modifieer. 

    holder.fname.setText(user.getFName());  
    holder.lname.setText(user.getLName()); 
    holder.bio.setText(user.getBiography()); 
    holder.image.setImageBitmap(user.getImage()); // if you use string then you download the image using 
    // the string as url and set it to your imageview.. 
    return child; 
} 

static class RecordHolder { 
    TextView fname,lname,bio; 
    ImageView image;  
} 


@Override 
public void notifyDataSetChanged() { // you can remove this.. 
    // TODO Auto-generated method stub  
    if(getCount() == 0){ 
     //show layout or something that notifies that no list is in.. 
    }else{ 
     // this is to make sure that you can call notifyDataSetChanged in any place and any thread 
     new Handler(getContext().getMainLooper()).post(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       Benildus_Adapter.super.notifyDataSetChanged(); 
      } 
     }); 
    } 
} 

} 

당신의 사람 클래스

public class Person { 

private String FName; 
private String LName; 
private String Biography; 
private Bitmap image; // add the image too to your class, you can store the url of the image 
// or save it using bitmap.. if you store the url then = String image; the load the image 
// in the getview method.. any way you choose.. 
public String getFName() { 
    return FName; 
} 
public void setFName(String fName) { 
    FName = fName; 
} 
public String getLName() { 
    return LName; 
} 
public void setLName(String lName) { 
    LName = lName; 
} 
public String getBiography() { 
    return Biography; 
} 
public void setBiography(String biography) { 
    Biography = biography; 
} 
public Bitmap getImage() { 
    return image; 
} 
public void setImage(Bitmap image) { 
    this.image = image; 
} 

} 

편집 ..

gridView = (GridView)findViewById(R.id.grid); 
Benildus_Adapter bA = new Benildus_Adapter(this, R.layout.myxml,dbPersons); 
gridView.setAdapter(bA); 

도움이 되시길 바랍니다.

관련 문제