ArrayAdapter要求资源ID为TextView,ListView无法强制转换为TextView

时间:2015-07-25 06:32:21

标签: android android-fragments android-listview textview android-arrayadapter

我创建了两个片段,一个片段使用listview显示项目,一个片段使用textview在单击另一个片段的项目时显示文本。在主要活动中,我使用下面一个的这两个片段。但我有两个错误,即

  

1)java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView   2)java.lang.ClassCastException:android.widget.ListView无法强制转换为android.widget.TextView

MainActivity.java

package perception.vicky.fragments;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements platformsfragment.ListSelectionListener {

public static String[] titles;
public static String[] details;
private detailsfragment detailsreference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get the string arrays with the titles and details
    titles = getResources().getStringArray(R.array.Titles);
    details = getResources().getStringArray(R.array.Quotes);

    // Get a reference to the detailsfragment
    detailsreference = (detailsfragment) getFragmentManager().findFragmentById(R.id.details);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

// Called when the user selects an item in the platformsfragment
@Override
public void onListSelection(int index) {

    if (detailsreference.getShownIndex() != index) {

        // Tell the detailsfragment to show the quote string at position index
        detailsreference.showQuoteAtIndex(index);
    }

}
}

platformsfragment.java

package perception.vicky.fragments;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
* Created by Vicky on 7/23/2015.
*/
public class platformsfragment extends ListFragment {

private ListSelectionListener platformlistener = null;
private View view;

// Callback interface that allows this Fragment to notify the MainActivity when
// user clicks on a List Item
public interface ListSelectionListener {
    void onListSelection(int index);
}

// Called when the user selects an item from the List
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // Indicates the selected item has been checked
    getListView().setItemChecked(position, true);

    // Inform the MainActivity that the item in position pos has been selected
    platformlistener.onListSelection(position);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {

        // Set the ListSelectionListener for communicating with the MainActivity
        platformlistener = (ListSelectionListener) activity;

    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnArticleSelectedListener");
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.platformsfragment, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Set the list choice mode to allow only one selection at a time
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // Set the list adapter for the ListView
    // Discussed in more detail in the user interface classes lesson
    setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles));
}
}

detailsfragment.java

package perception.vicky.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by Vicky on 7/23/2015.
*/
public class detailsfragment extends Fragment {

private TextView detailstext=null;
private int DetailsArrayLen;
private int Indx = -1;
private View view;

public detailsfragment() {
}

public int getShownIndex() {
    return Indx;
}

// Show the Quote string at position newIndex
public void showQuoteAtIndex(int newIndex) {
    if (newIndex < 0 || newIndex >= DetailsArrayLen)
        return;
    Indx = newIndex;
    detailstext.setText(String.valueOf(MainActivity.details[Indx]));
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

// Called to create the content view for this Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout defined in detailsfragment.xml
    // The last parameter is false because the returned view does not need to be attached to the container ViewGroup
    view = inflater.inflate(R.layout.detailsfragment, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.detailsfragment,R.id.textId,MainActivity.details);
    //setListAdapter(adapter);
    detailstext = (TextView) getActivity().findViewById(R.id.textId);
    DetailsArrayLen = MainActivity.details.length;
}

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="perception.vicky.fragments.platformsfragment"
    tools:layout="@layout/platformsfragment"
    android:id="@+id/platforms"
    android:layout_above="@+id/details"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="perception.vicky.fragments.detailsfragment"
    tools:layout="@layout/detailsfragment"
    android:id="@+id/details"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false" />

</RelativeLayout>

platformsfragment.xml

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

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:padding="5dip"
    android:textSize="32sp"/>

</RelativeLayout>

detailsfragment.xml

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

<TextView
    android:layout_width="match_parent"
    android:id="@+id/textId"
    android:layout_height="291dp"
    android:padding="5dip"
    android:textSize="32sp"
    android:layout_alignParentTop="true" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

你错了这句话:

new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles)

如果您查阅文档,则可以看到您将错误的值传递给构造函数。请检查此链接:ArrayAdapter

第二个参数是列表项的布局用户的id,而不是片段的布局。 第三个参数是列表项布局中的TextView的id,应该用于显示每个项目的信息,而不是要填充的列表的id。

答案 1 :(得分:0)

我认为问题在于:

new ArrayAdapter<String>(getActivity(), R.layout.platformsfragment, android.R.id.list, MainActivity.titles)

尝试将此代码替换为:

new ArrayAdapter<String>(getActivity(), R.layout.listitem, android.R.id.text1, MainActivity.titles)

<强> listitem.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/textview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TextView></LinearLayout>