Android:在两列中显示信息,而不是一列用于BaseAdapter

时间:2015-10-12 11:30:22

标签: java android android-listview adapter

我正在开发一个Android应用程序,其中我有一个Note对象的java.util.List,我通过扩展BaseAdapter在一个页面上显示。我在屏幕上有大小并排显示其中的两个。

我不知道如何使用BaseAdapter完成此任务。你能帮忙的话,我会很高兴。非常感谢。 :-) 正如您从下面的图片中看到的那样,我有空间可以再显示一列和可点击的(他们现在就可以获取他们的ID):

Notes image

GroupSectionActivity:// Notes检索:

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>

1 个答案:

答案 0 :(得分:1)

为了实现两列,您可以使用GridView代替ListView,并在2文件中将列数设置为xml

并且是也在您的java文件中替换相同内容,并将adapter设置为GridView,您就完成了。