2013-04-30 27 views
0

我在我的代碼中使用了一個簡單的適配器。我從文件路徑填入GridView。以下是我的代碼,它的工作原理。現在我想在我的gridview中添加一個複選框。我怎樣才能做到這一點?我應該如何修改我的代碼?所有的例子,我從畫廊導入圖像,但我需要導入他們的形式文件路徑。如果一個文件是一個目錄,我還需要顯示一個目錄圖標,否則顯示一個圖像。請幫我如何自定義gridview

GridView gridView; 
TextView textView; 
File currentParent; 
File[] currentFiles; 
SimpleAdapter simpleAdapter; 
File root1; 
File root; 

root1 = new File("/data/data/com.myexample.lock/files/"); 

currentParent = root1; 
currentFiles = root1.listFiles(); 

currentFilePath = new String[currentFiles.length]; 
int count = 0; 

for (File f : currentFiles) { 
    currentFilePath[count] = f.getAbsolutePath(); 
    count++; 
} 

gridView = (GridView) findViewById(R.id.grid); 
gridView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     if (currentFiles[position].isDirectory()) { 
      root = new File("/data/data/com.myexample.lock/files/" + FileName(currentFilePath[position]) + "/"); 
      currentFiles = root.listFiles(); 
      inflateListView(currentFiles); 
     } else if (currentFiles[position].isFile()) { 
      openFile(currentFiles[position]); 
     } 
    } 
}); 


private void inflateListView(File[] files) { 
    List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); 

    for (int i = 0; i < files.length; i++) { 
     Map<String, Object> listItem = new HashMap<String, Object>(); 
     if (files[i].isDirectory()) { 
      listItem.put("icon", R.drawable.folder); 
      listItem.put("fileName", files[i].getName()+"("+files[i].list().length+")"); 
     } else { 
      listItem.put("icon", files[i]); 
     } 

     listItems.add(listItem); 
    } 

    simpleAdapter = new SimpleAdapter(this, listItems, R.layout.line,new String[] { "icon", "fileName" }, new int[] { R.id.icon, R.id.file_name }); 
} 

R.layout.line.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

     <TextView 
     android:id="@+id/file_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/icon" 
     android:paddingLeft="5dp" 
     android:textColor="#000000" 
     android:textSize="12dp" /> 

</RelativeLayout> 
+1

你聽說過句子的概念嗎? – 2013-04-30 08:28:33

+0

help me plzzzzz – 2013-04-30 08:40:06

+4

@hayyaanam請在你的問題中儘量避免使用* plzz,im等*的縮寫和縮寫..你不會寫信給你的朋友,而是你尋求其他proffessionals的幫助。用適當的句子使用大寫字母。 – Krishnabhadra 2013-04-30 08:47:07

回答

1

參考鏈接here。按照預期在應用程序中使用複選框定製gridview。

+0

該代碼也是我的哪個解決方案ü給我,但問題是即時通訊使用simpleadapter代碼使用自定義調整器不diffrenciate當前文件是目錄或文件?如果是目錄顯示文件夾圖標,否則顯示圖像我如何在ur代碼中diffrenciate? – 2013-04-30 10:20:51

+0

即時通訊使用gridview烏爾代碼使用列表視圖 – 2013-04-30 10:21:21