【问题标题】:Android - wizard type activity with adapter backed dataAndroid - 具有适配器支持数据的向导类型活动
【发布时间】:2012-05-07 14:04:51
【问题描述】:

我正在尝试找出解决以下 Android 问题的最佳方法。

我有来自内容提供者的问题对象数组形式的数据。 Question 对象有一些文本和一个 id。每个问题有 4 个答案,这些答案将在 QuestionResponse 对象中发送回其余服务器。

我喜欢在视图中一次显示一个问题(用户阅读问题,选择答案,然后点击“下一个”,这会移动到新屏幕上的下一个问题)但我需要这样做与 ListView 在注册内容观察者方面的方式相同(因为问题可能会在服务器上发生变化,这反过来会在内容提供者中更新它们。)存储在内容提供者中的数据的更改应该会更新屏幕上的问题当它们发生时。

我猜我需要的是 SimpleCursorAdaptor 之类的东西,但我需要的视图不是列表视图,而是光标指向的行之一的全屏表示。我试过制作自己的适配器,但我对下一步要去哪里有点卡住了。这是我的适配器....

public class QuestionAdapter implements Adapter {


public ArrayList<Question> questions = new ArrayList<Question>();
private Context context ;


public QuestionAdapter(ArrayList<Question> questions, Context context)
{
    this.questions.clear();
    this.questions.addAll(questions);
    this.context = context;
}

public void updateQuestions(ArrayList<Question> questions)
{
    questions.clear();
    questions.addAll(questions);
}

@Override
public int getCount() {
    return questions.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return questions.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return Long.parseLong(questions.get(arg0).get_id());
}

@Override
public int getItemViewType(int arg0) {
    // TODO Auto-generated method stub
    return IGNORE_ITEM_VIEW_TYPE;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View questionView = inflater.inflate(R.layout.question, parent);
    return questionView;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return questions.isEmpty();
}

@Override
public void registerDataSetObserver(DataSetObserver arg0) {
    // TODO Auto-generated method stub

}

@Override
public void unregisterDataSetObserver(DataSetObserver arg0) {
    // TODO Auto-generated method stub

}
}

这是我的观点 question.xml。

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

<TextView
    android:id="@+id/questionText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_weight="0.27"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ToggleButton
            android:id="@+id/passButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="Pass"
            android:textOff="Pass" />

        <ToggleButton
            android:id="@+id/advisoryButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="Advisory"
            android:textOff="Advisory" />

        <ToggleButton
            android:id="@+id/defectButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="Defect" 
            android:textOff="Defect"/>

        <ToggleButton
            android:id="@+id/naButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="N/A"
            android:textOff="N/A" />

    </TableRow>

</TableLayout>

<Button
    android:id="@+id/next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="Next..." />

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-contentprovider


    【解决方案1】:

    以下是对可行方法的有根据的猜测。

    我不确定是否还有其他易于绑定到列表视图的视图,因此我将尝试修改您的示例以使用列表视图。首先,如果您将 Listview 本身设为 fill_parent 并在 listview Fill_parent 中设置子项,您基本上会得到一个只能显示一个子项的列表视图。然后您可以尝试以下方法:

    改变 getCount

    我相信getCount(...) 用于确定应该显示多少行数据。通常,我们希望将此设置为我们正在使用的 ArrayList 的计数/大小。在您的情况下,我认为您希望将其设置为 1,因为您一次只希望显示一个问题。

    为问题创建跟踪器

    您需要为当前正在处理的问题创建一个跟踪器,以便知道要显示哪个问题。为此,您需要一个简单的 int questionTracker 变量,您可以在他们每次回答/通过/跳过问题时递增。

    修改 getView(...) 以显示正确的问题

    在 getView(...) 调用中,您需要使用正确的问题填充屏幕,这将基于您的跟踪器。像这样的

    @Override public View getView(int position, View convertView, ViewGroup parent) 
    {     
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
        View questionView = inflater.inflate(R.layout.question, parent);     
    
        //Get the next question
        Question currentQuestion = questions.get(questionTracker);
    
        //Fill the fields in the inflated layout with the data from the Question object
        TextView questionField = questionView.findViewById(R.id.questionText)
        questionField.setText(Question.question);
    
        return questionView; 
    } 
    

    强制改变问题

    棘手的部分是在您点击下一步/通过/回答问题后强制重新绘制视图。您也许可以手动调用 getView(...),虽然我从未这样做过,所以我不确定。

    【讨论】:

    • 我不确定我是否理解 ListView.getCount() 的更改。它并没有真正确定应该显示多少行......??它只是告诉你适配器有多少行你说设置为 1???
    • 我相信 getCount(...) 指示数据集中有多少项目,并顺便向适配器指示需要绘制多少行/项目。我相信如果您将 getCount(...) 结果设置为 1,它只会尝试绘制一行,因为它认为数据集只有一个值/行/项。我必须自己测试一下。我很可能是错的,我只是认为适配器在内部利用 getCount(...) 来确定调用 getView(...) 的次数
    • 是的,你是对的。我扩展了 SimpleAdapter(我正在使用的),然后覆盖 GetCount 返回 1。就像一个魅力。现在由于某种原因,我必须弄清楚为什么我的行没有填满全屏。
    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 2021-09-26
    • 2021-06-28
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2015-01-21
    相关资源
    最近更新 更多