2015-10-12 35 views
0

我在Android應用程序中,我有注意對象的java.util.List的,我正在一個頁面上通過延伸BaseAdapter顯示工作。我在屏幕上顯示了兩個並排顯示的大小。安卓:顯示信息在兩列,而不是一個BaseAdapter

我不知道如何使用BaseAdapter來完成這個任務。你能幫忙的話,我會很高興。非常感謝。 :-) 正如你可以從下面的圖像看,我有空間來顯示一個多列,可點擊(這是他們的權利,讓他們的ID):

Notes image

GroupSectionActivity://注意檢索:

public class GroupSectionActivity extends DrawerLoader { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sectionlayout); 
final ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<HashMap<String, String>>(); 
     for (RestNote restNote : restNoteListFinal) { 
      HashMap<String, String> sectionDisplay = new HashMap<>(); 
      sectionDisplay.put(noteId, String.valueOf(restNote.getMnoticesid())); 
      sectionDisplay.put(noteTag, restNote.getMnotetag()); 
      sectionDisplay.put(noteText,restNote.getMnotetext()); 
      sectionDisplay.put(noteColor,restNote.getMnotecolor()); 
      restSectionArrayList.add(sectionDisplay); 
     } 

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

     SectionLazyAdapter sectionLazyAdapter = new SectionLazyAdapter(this, restSectionArrayList); 

     listView.setAdapter(sectionLazyAdapter); 
} 

SectionLazyAdapter:

public class SectionLazyAdapter extends BaseAdapter{ 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 

    public SectionLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.activity_group_section, null); 

     TextView noteTag = (TextView)vi.findViewById(R.id.noteTag); 
     TextView noteText = (TextView) vi.findViewById(R.id.noteText); 
     ImageView noteImage = (ImageView)vi.findViewById(R.id.noteImage); 
     HashMap<String, String> sectionList = new HashMap<>(); 
     sectionList = data.get(position); 
     noteTag.setText(sectionList.get(GroupSectionActivity.noteTag)); 
     noteText.setText(sectionList.get(GroupSectionActivity.noteText)); 
     String noteColorFromList = sectionList.get(GroupSectionActivity.noteColor); 
     if(noteColorFromList.equals("#1abc9c")){ 
      noteImage.setImageResource(R.drawable.notegreen); 
     } 
     if(noteColorFromList.equals("#3498db")){ 
      noteImage.setImageResource(R.drawable.noteblue); 
     } 
// And other if conditions 
return vi; 
    } 
} 

sectionLayout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <ListView 
      android:id="@+id/seclist" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/sectionAddButton" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add Section" 
      android:id="@+id/sectionAddButton" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/sectionNameTextField" 
      android:layout_above="@+id/seclist" 
      android:layout_toRightOf="@+id/sectionAddButton" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" /> 

    </RelativeLayout> 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:dividerHeight="1dp" /> 
</android.support.v4.widget.DrawerLayout> 

activity_groupSection.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="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/noteImage" 
      android:layout_width="140dp" 
      android:layout_height="200dp" 
      android:scaleType="fitXY" 
      android:padding="5dp"/> 

     <TextView 
      android:id="@+id/noteTag" 
      android:layout_width="90dp" 
      android:layout_height="match_parent" 
      android:visibility="visible" 
      android:gravity="center" 
      android:layout_gravity="center_horizontal|top" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:scrollHorizontally="true" 
      android:layout_marginTop="10dp" /> 

     <TextView 
      android:layout_width="97dp" 
      android:layout_height="160dp" 
      android:id="@+id/noteText" 
      android:layout_gravity="center_horizontal|bottom" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="30dp" /> 
    </FrameLayout> 
</RelativeLayout> 
+0

如何使用'GridView'而不是'ListView'並將'numColumns'設置爲'2' –

+0

只需使用recyclerview即可更好https://developer.android.com/reference/android/support/v7/widget /RecyclerView.html其中提供所有類型如網格,交錯和列表 – Pavan

+0

@SatyenUdeshi:你能告訴我如何修改此代碼使用GridView? –

回答

1

爲了實現兩列可以在xml文件中使用GridView,而不是ListView和設置的列數2

是的,也在你的java文件中替換相同,並將adapter設置爲GridView,你就完成了。

+0

請在有空的時候檢查問題。謝謝。 :-) –

+0

hello @borg對不起,我沒有得到關於字體真棒的問題,你可以解釋一下/ \ –

+0

當然。字體真棒是一種看起來更像圖片的字體集。簡單的二維圖像(保存爲字體)就像一個心臟,雞蛋等。所以,如果你保存字符串「雞蛋」,會有一個與它相關的二維圖像(字體)。所以,對於我的應用程序,我已經保存並在Android中檢索它。我只是想知道如何顯示這些字體真棒字體。這是一個非常受歡迎的圖書館,你可能會發現Android的參考資料,我對此不太瞭解,所以無法理解。 –