2012-12-10 43 views
0

我想创建一个数组适配器来设置内容从文本数组到适配器,但我无法从活动中设置适配器。我不知道我应该通过对资源的诠释Android资源无法解析为变量

PlacesListAdapter.java

public class PlacesListAdapter extends ArrayAdapter<Place> { 
    public Context context; 
    private List<Place> places; 

    public PlacesListAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    public PlacesListAdapter(Context context, int resource, List<Place> places) { 
     super(context, resource, places); 
     this.context = context; 
     this.places = places; 
     // imageLoader = new ImageLoader(context.getApplicationContext()); 

    } 

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

     ViewHolder holder; 
     View view = convertView; 
     Place p = places.get(position); 

     if (convertView == null) { 
      LayoutInflater viewInflater; 
      viewInflater = LayoutInflater.from(getContext()); 
      view = viewInflater.inflate(R.layout.item_place, null); 

      holder = new ViewHolder(); 
      holder.placeTitle = (TextView) view.findViewById(R.id.place_title); 
      holder.placeDistance = (TextView) view 
        .findViewById(R.id.place_distance); 

      view.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.placeTitle.setText(p.getPlaceTitle()); 
     holder.placeDistance.setText("200"); 
     holder.placeCategoryIcon.setImageResource(R.drawable.marker); 

     return view; 
    } 

    static class ViewHolder { 
     TextView placeId; 
     TextView placeTitle; 
     TextView placeDistance; 
     ImageView placeCategoryIcon; 
    } 

} 

Place.java

public class Place { 

    String placeId = "", placeTitle = "", placeDistance = "", 
      placeCategoryIcon = ""; 

    public Place(String placeId, String placeTitle, String placeDistance, 
      String placeCategoryIcon) { 
     this.placeId = placeId; 
     this.placeTitle = placeTitle; 
     this.placeDistance = placeDistance; 
     this.placeCategoryIcon = placeCategoryIcon; 
    } 

    public String getPlaceId() { 
     return placeId; 
    } 

    public void setPlaceId(String placeId) { 
     this.placeId = placeId; 
    } 

    public String getPlaceTitle() { 
     return placeTitle; 
    } 

    public void setPlaceTitle(String placeTitle) { 
     this.placeTitle = placeTitle; 
    } 

    public String getPlaceDistance() { 
     return placeDistance; 
    } 

    public void setPlaceDistance(String placeDistance) { 
     this.placeDistance = placeDistance; 
    } 

    public String getPlaceCategoryIcon() { 
     return placeCategoryIcon; 
    } 

    public void setPlaceCategoryIcon(String placeCategoryIcon) { 
     this.placeCategoryIcon = placeCategoryIcon; 
    } 

} 

现在MainActiivty我试图设置适配器,以便它填充阵列中的列表

public class MainActivity extends SherlockFragmentActivity implements 
     SearchView.OnQueryTextListener { 

    private final String[] places = new String[] { "Mysore", "Bangalore", "Mangalore", 
      "Wayanad", "Bandipur National Park", "Chickmaglur", 
      "Bandipura", "Coorg", "Kodaikanal", "Hampi", 
      "Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram", 
      "Talakadu", "Savana Durga" }; 

    public SearchView mSearchView; 
    private TextView mStatusView; 

    private Menu mainMenu = null; 

    PlacesListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Log.i("Nomad", "onCreate"); 

     ListView listView = (ListView) findViewById(R.id.place_list); 


     adapter = new PlacesListAdapter(this, resource, places); 

    } 
} 

我不知道该怎么在adapter = new PlacesListAdapter(this, resource, places);

回答

1

resources设置初始化resource变量:

// it doesn't matter what values you assing to the resource variable because you build 
// the row layout yourself in the getView method of the adapter 
int resource = android.R.layout.simple_list_item_1; 
adapter = new PlacesListAdapter(this, resource, places); 

编辑:

List<Place> thePlaces = new ArrayList<Place>(); 
for (int i = 0; i < places.length; i++) { 
    Place pl = new Place("NO_ID", places[i], "NO_DISTANCE", "NO_CATEGORYICON"); 
    thePlaces.add(pl); 
} 
int resource = android.R.layout.simple_list_item_1; 
adapter = new PlacesListAdapter(this, resource, thePlaces); 
+0

我试过,但现在我得到这个错误的构造PlacesListAdapter(MainActivity,INT的String [])是未定义 –

+0

@HarshaMV你的适配器的建设者预计数据没有或列表'Places',你正在试图传递一个String数组。将该字符串数组转换为“Places”对象列表,并将该列表传递给适配器。 – Luksprog

+0

你能告诉我一些资源教我如何创建对象。 –

1

通排布局ID,而不是默认的Android布局如果您通过扩展ArrayAdapter来创建自定义适配器:

adapter = new PlacesListAdapter(this,R.layout.item_place, places); 

代替

adapter = new PlacesListAdapter(this, resource, places);