java - 安卓 : Displaying information in two columns instead of one for BaseAdapter

标签 java android android-listview adapter

我正在开发一个 Android 应用程序,其中有一个 java.util.List of Note 对象,我通过扩展 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>

最佳答案

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

是的,也可以在您的java文件中替换相同的内容,并将您的适配器设置为GridView,然后就完成了。

关于java - 安卓 : Displaying information in two columns instead of one for BaseAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33080017/

相关文章:

java - 使用 http 传输通过代理将消息推送到 Activity mq

JavaFX 3d 和工具栏

android - 使用 parse.com 制作聊天应用程序并选择收件人

android - 如何在 Android Listview 中显示单个项目?

java - 正确答案进度条

java - 在 Android Studio 中将信息保存到数据时出现问题

java - 如何更改外部类?

android - 如何通过单击 ListView 的图像按钮来启动 Activity ?

android - java.io.IOException : BufferedInputStream is closed 异常

java - double 混淆?