2015-06-19 51 views
0

非常新的java和android,但我認爲我需要一個佈局inflater,我試圖實現它,但失敗了,我抹去了代碼。Android適配器傳遞JSON對象數據?

我可以把對象插入到一個字符串數組,並使用ArrayAdapter循環佈局分配字符串到textView

所以ArrayAdapter是有限

ListAdapter dribbbleFeedAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.feed_card, R.id.info_text, titleArr); 

我所用的上下文中僅通過,佈局,textView和字符串數組。

我想要做的是將圖像和其他數據傳遞到該佈局的視圖。所以我的問題是如何構建適配器,以便我可以將對象的值分配給視圖ID?

public class MainActivity extends AppCompatActivity { 

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

    // GET Json and use adapter ListView 
    Ion.with(this) 
      .load("http://api.dribbble.com/shots/everyone") 
      .asJsonObject() 
      .setCallback(new FutureCallback<JsonObject>() { 
       @Override 
       public void onCompleted(Exception e, JsonObject result) { 

       // SET Titles Array 
       List<String> titles = new ArrayList<String>(); 

       // BUILD THE ARRAY 
       JsonArray shots = result.getAsJsonArray("shots"); 
       for (int i = 0; i < shots.size(); i++) { 
        String title = shots.get(i).getAsJsonObject().get("title").getAsString(); 
        titles.add(title); 
       } 
       String[] titleArr = new String[titles.size()]; 
       titleArr = titles.toArray(titleArr); 

       // TODO: Put more data into the feed_card 
       //dribbbleImage.setImageResource(R.drawable.icon_1); 
       ListAdapter dribbbleFeedAdapter = new ArrayAdapter<String>(MainActivity.this, 
         R.layout.feed_card, R.id.info_text, titleArr); 
       ListView dribbbleDataListView = (ListView) findViewById(R.id.dribbbleFeedListView); 
       dribbbleDataListView.setAdapter(dribbbleFeedAdapter); 
       } 
      }); 

    } 

} 

回答

1

你應該編寫自己的Adapter擴展BaseAdapter。

就像下面的代碼:

public class TestAdapter extends BaseAdapter { 

    public TestAdapter(Context context, List<Data> dataList) { 
     this.dataList = dataList; 
     this.context = context; 
    } 

    private List<Data> dataList; 
    private Context context; 

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

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

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

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

     ViewHolder holder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(layout.your_list_item_layout, parent, false); 
      holder = new View(); 
      holder.tv_title = convertView.findViewById(R.id.title); 
      holder.iv_data = convertView.findViewById(R.id.data); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder)convertView.getTag(); 
     } 

     Data data = dataList.get(position); 
     holder.tv_title.setText(data.getTitle()); 

     return convertView; 
    } 

    private class ViewHolder { 
     TextView tv_title; 
     ImageView iv_data; 
    } 
} 
+0

好,謝謝,這聽起來是正確的,但我仍然試圖找出我怎麼能使用對象來填充視圖,因此一些例子或提示將是巨大的。 –

+0

你可以看看我的答案,我已經編輯它。 –

相關問題