2014-02-17 150 views
-3

的Android的ListView我想要做一個自定義項的ListView。一般項目是具有兩個文本視圖的全寬圖像,另一圖像像圖標。與自定義項目

喜歡的東西:

enter image description here

什麼是實現這一目標的最佳方式是什麼?

感謝

+4

在來這裏之前,你甚至嘗試過搜索嗎?這個 –

+0

必須至少有一打教程,但我想試試,但是我想要。 sry –

回答

1

你應該通過擴展BaseAdapterArrayAdapter等聲明自己的適配器ListView有你可以寫自己的實現getView()方法負責每個項目在你的ListView的看法。

getView()方法,你可以只是從你的XML與佈局膨脹視圖,並在需要的數據填充它。

0

您可以通過創建可ListView的getView方法的項目,相同的佈局內充氣將適用於所有的人都爲@nikis太說,自己的自定義佈局實現這一目標。

下面是一個容易理解和您的需求,工作的例子。 :)

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

+0

請至少包括有關文章的基本知識,以便文章消失時仍然有答案。 – PearsonArtPhoto

+0

謝謝atul o holic –

+0

至少給他一些基本的信息,而不僅僅是給一個教程的鏈接。 – mike20132013

0

在這裏你去。這至少會瀏覽你在一定程度上。我在組中有一個圖像,三個文字瀏覽。在你的情況下,有一個圖像視圖和兩個文本視圖,這是唯一的區別。

這裏是我是如何實現它:

步驟1:在MainActivity,我做了這是有5件事情,一個ImageView的4個textviews自定義適配器。

下面的代碼:

public class ListViewExample extends Activity { 

    ArrayList<String> QuestionForSliderMenu = new ArrayList<String>(); 

    ArrayList<String> NAME = new ArrayList<String>(); 
    ArrayList<String> STATE = new ArrayList<String>(); 
    ArrayList<String> LATITUDE = new ArrayList<String>(); 
    ArrayList<String> LONGITUDE = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.listview_layout); 

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

     MyAdapter adapter = new MyAdapter(this, QuestionForSliderMenu); 

     listView.setAdapter(adapter); 

     try { 
      yourjson(); 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

    } 

    //Just for example. You can do anything to get in your array list. 
    public void yourjson() { 

     NAME = new ArrayList<String>(); 
     STATE = new ArrayList<String>(); 
     LATITUDE = new ArrayList<String>(); 
     LONGITUDE = new ArrayList<String>(); 
     try { 

      JSONObject json = new JSONObject(loadJSONFromAsset()); 

      JSONArray array = json.getJSONArray("cities"); 
      QuestionForSliderMenu = new ArrayList<String>(); 
      Log.d("Cities: ", array.toString()); 
      for (int my = 0; my <= array.length(); my++) { 

       JSONObject c = array.getJSONObject(my); 

       String name = c.getString("name"); 
       String state = c.getString("state"); 
       String latitude = c.getString("latitude"); 
       String longitude = c.getString("longitude"); 

        NAME.add(name); 
        STATE.add(state); 
        LATITUDE.add(latitude); 
        LONGITUDE.add(longitude); 

      } 

     } catch (JSONException e) { 

      e.printStackTrace(); 

     } 

    } 

下面是我使用的自定義適配器。

第二步:

private class MyAdapter extends BaseAdapter { 

     private int lastPosition = -1; 
     private Context context; 
     private ArrayList<ModelClass> name; 
     private ArrayList<ModelClass> state; 
     private ArrayList<ModelClass> latitude; 
     private ArrayList<ModelClass> longitude; 
     private ArrayList<ModelClass> MainItems; 
     private ArrayList<String> mainList; 

     public MyAdapter(Context context, ArrayList<ModelClass> name, 
       ArrayList<ModelClass> state, ArrayList<ModelClass> latitude, 
       ArrayList<ModelClass> longitude) { 
      super(); 
      this.context = context; 
      this.name = name; 
      this.state = state; 
      this.latitude = latitude; 
      this.longitude = longitude; 
     } 

     public MyAdapter(Context applicationContext, 
       ArrayList<String> questionForSliderMenu1) { 

      super(); 

      this.mainList = questionForSliderMenu1; 

     } 

     @Override 
     public int getCount() { 

      return STATE.size(); 
     } 

     @Override 
     public Object getItem(int position) { 

      return position; 
     } 

     @Override 
     public long getItemId(int position) { 

      return position; 
     } 

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

      if (convertView == null) { 

       LayoutInflater inflater = (LayoutInflater) getApplicationContext() 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.cities_row, null); 
      } 

      ImageView img = (ImageView)convertView.findViewById(R.id.customimageView1); 
      TextView name = (TextView) convertView.findViewById(R.id.customtextView1); 
      TextView state = (TextView) convertView.findViewById(R.id.customtextView2); 
      TextView latitude = (TextView) convertView 
        .findViewById(R.id.Latitude); 
      TextView longitude = (TextView) convertView 
        .findViewById(R.id.Longitude); 

      try { 
       img.setBackgroundResource(R.drawable.ic_launcher); 
       name.setText(NAME.get(position)); 
       state.setText(STATE.get(position)); 
       latitude.setText(LATITUDE.get(position)); 
       longitude.setText(LONGITUDE.get(position)); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return convertView; 
     } 

    } 

這是檢索從資產JSON的方法:這是你的情況是可選的。但現在,我必須將這個用於上述方法。

public String loadJSONFromAsset() { 

     String json = null; 

     try { 

      InputStream is = getApplicationContext().getAssets().open(
        "yourjson.json"); 

      int size = is.available(); 

      byte[] buffer = new byte[size]; 

      is.read(buffer); 

      is.close(); 

      json = new String(buffer, "UTF-8"); 

     } catch (IOException ex) { 

      ex.printStackTrace(); 

      return null; 
     } 
     return json; 

    } 

} 

最後兩個XML,

一:你的ListView佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/customlistView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

二:你的物品列表視圖,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/customimageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher"/> 

    <TextView 
     android:id="@+id/Name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Cities" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/State" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="States" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/Latitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Latitude" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/Longitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Longitude" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

我希望這可以幫助你回答你的問題... :)

如果你喜歡我的回答,並認爲它有幫助,請接受它..:)

+0

謝謝邁克... –

+0

我是畢業生我能夠幫助.. :) – mike20132013

+0

是這個答案有幫助,雖然 – mike20132013