2014-02-19 38 views
-1

我只是問自己是否有一種方法讓ListView沒有任何文本。我只找到了帶有圖像+文本的ListView,而這正是我不想要的。我只想添加很多像列表中的圖像,並且不想用ImageView製作圖像,因爲這會導致我的應用程序崩潰。另外,我不希望我的應用程序滯後。謝謝!ListView僅限圖像

這是我在activity_main.xml中

<ListView 
android:id="@+id/listview" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 

/>

了,我發現從另一個職位,實際上幫助了我,但它不顯示所有圖像

public class MainActivity extends Activity { 


// Array of integers points to images stored in /res/drawable-hdpi/ 
int[] vehs = new int[]{ 
    R.drawable.veh1, 
    R.drawable.veh2, 
    R.drawable.veh3, 
    R.drawable.veh4, 
    R.drawable.veh5, 
    R.drawable.veh6, 
    R.drawable.veh7, 
    R.drawable.veh8, 
    R.drawable.veh9, 
    R.drawable.veh10, 
    R.drawable.veh11, 
    R.drawable.veh12, 
    R.drawable.veh13, 
    R.drawable.veh14, 
    R.drawable.veh15, 
    R.drawable.veh16, 
    R.drawable.veh17, 
    R.drawable.veh18, 
    R.drawable.veh19, 
    R.drawable.veh20, 
    R.drawable.veh21, 
    R.drawable.veh22, 
    R.drawable.veh23, 
    R.drawable.veh24, 
    R.drawable.veh25, 
    R.drawable.veh26, 
    R.drawable.veh27, 
    R.drawable.veh28, 
    R.drawable.veh29, 
    R.drawable.veh30, 
    R.drawable.veh31, 
    R.drawable.veh32, 
    R.drawable.veh33, 
    R.drawable.veh34, 
    R.drawable.veh35, 
    R.drawable.veh36, 
}; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Each row in the list stores country name, currency and flag 
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

    for(int i=0;i<10;i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("vehs", Integer.toString(vehs[i])); 
     aList.add(hm); 
    } 

    // Keys used in Hashmap 
    String[] from = { "vehs","txt","cur" }; 

    // Ids of views in listview_layout 
    int[] to = { R.id.vehs,R.id.txt,R.id.cur}; 

    // Instantiating an adapter to store each items 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

    // Getting a reference to listview of main.xml layout file 
    ListView listView = (ListView) findViewById(R.id.listview); 

    // Setting the adapter to the listView 
    listView.setAdapter(adapter); 
} 

}

沒有編輯所有的代碼,所以你可能會發現一些令人困惑的東西xD

+0

你可以發佈您的代碼? –

+0

一些代碼會幫助我理解你的觀點:) –

+0

@mohammedmomn我其實沒有代碼,希望你能告訴我一個基本的地方,我可以添加圖像。 –

回答

1

方式ListView顯示數據是通過使用適配器。適配器將您的數據,插入到自定義視圖中,然後將其添加到列表中。

要創建一個快速圖像ListView,你想要做的第一件事是將Picasso添加到您的項目。這個庫會自動下載和緩存你的圖片,處理回收,等等。

你想要做的下一件事是寫你的項目視圖。如果你想要一個只圖像列表,認爲可能是簡單的:

<!-- res/layout/list_item.xml --> 

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

接下來,您要創建您的適配器。它接受圖像URL的List<String>作爲輸入,構建項目並將它們插入到ListView

public class ImageListAdapter extends ArrayAdapter<String> { 
    List<String> items; 

    public ImageListAdapter(Context context, int resource, List<String> items) { 
     super(context, resource, items); 
     this.items = items; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.list_item, null); 
     } 

     Picasso.with(getContext()) 
       .load(items.get(position)) 
       .into((ImageView) convertView); 

     return convertView; 
    } 
} 

,如果你想添加更多的選項,如圖像佔位符,轉換,多閱讀Picasso library documentation

最後,使用該適配器上的ListView,將它添加到您的活動的onCreate方法:

List<String> images = new ArrayList<String>(); 
images.add("http://hometowncolumbia.files.wordpress.com/2007/12/lol-cats-dont-look-behind-cat.jpg"); 
images.add("http://i232.photobucket.com/albums/ee245/topswing/cat-lol.jpg"); 

listView = (ListView)findViewById(R.id.myListView); 
listView.setAdapter(new ImageListAdapter(this, R.layout.list_item, images)) 

在實際應用中,雖然,你可能會想從您的服務器加載圖像列表。你需要使用AsyncTask

+0

我只需要顯示很多圖像。這看起來很難理解。我對這個android開發很感興趣,這種事情讓我感到困惑。我在可繪製的文件夾中獲得了一堆圖像,我想用ListView顯示它們。除非有其他更好的顯示方式。 –

0

不要爲難,解決方法很簡單中的經典3個步驟

第1步:
在XML做一個RelativeLayout。圖像移動到所需的位置(總是中心,但你可以選擇,如果左或右)和更改layout_heightwrap_content

第2步:
創建的ListView自定義適配器,如果你這樣做,你會能夠把所有任何佈局作爲項目

public class ImagesAdapter extends ArrayAdapter<String> { 

    private Activity mContext; 
    private ArrayList<Bitmap> mImages; 

    public ImagesAdapter(Activity context, ArrayList<String> listOfValues, ArrayList<Bitmap> images) { 
     //The listOfValues is used when you make item click to get value 
     //Each image must to have a text value 
     super(context, R.layout.yourlayout, listOfValues); 
     mContext = context; 
     mImages = images; 
    } 

    @Override 
    public void getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = mContext.getLayoutInflater(); 
     View returnView = inflater.inflate(R.layout.yourlayout, null); 
     ImageView imageView = (ImageView) returnView.findViewById(R.id.yourimageid); 
     imageView.setImageBitmap(mImages.get(position)); 
     return returnView; 
    } 

} 

步驟3:
在代碼中實現

ImagesAdapter adapter = new ImagesAdapter(this, myList, myImages); 
mylistView.setAdapter(adapter); 
myListView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     //volov = Values Of ListOfValues 
     String volov = (String) myListView.getItemPosition(position); 
     Log.i(TAG, "listOfValues is useful " + volov); 
    } 

});