单击按钮后如何显示ArrayList(Json响应)

时间:2017-03-19 08:11:09

标签: android parsing arraylist android-arrayadapter asynctaskloader

我是Android的新手。我开始开发我的应用程序,我从网站得到响应,我想将此响应主体“Json”显示为ArrayList。我创建了一个AsyncTaskLoader + ArrayAdapter。

当我点击按钮时,我得到响应“200”,但没有显示

我试图找几天的答案,但我不能。我很沮丧。任何人都可以帮助我吗?:)

    earthQuakeAdapter = new EarthQuakeAdapter(this, new ArrayList<EarthQuakeData>());





    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Get a reference to the LoaderManager, in order to interact with loaders.
            LoaderManager loaderManager = getLoaderManager();

            loaderManager.restartLoader(EARTHQUAKE_LOADER_ID, null, mCallbacks);



        }
    });



}


@Override
public Loader<List<EarthQuakeData>> onCreateLoader(int i, Bundle bundle) {

    String pobierzTekst = editText.getText().toString();


    return new EarthQuakeLoader(this,pobierzTekst);
}

@Override
public void onLoadFinished(Loader<List<EarthQuakeData>> loader, List<EarthQuakeData> earthQuakeDatas) {
    earthQuakeAdapter.clear();

    if (earthQuakeDatas != null && !earthQuakeDatas.isEmpty()) {
        earthQuakeAdapter.addAll(earthQuakeDatas);

    }
}

@Override
public void onLoaderReset(Loader<List<EarthQuakeData>>  loader) {

    }

}

这是我的ArrayAdapterClass:

public class EarthQuakeAdapter extends ArrayAdapter <EarthQuakeData> {


public EarthQuakeAdapter(Context context, List<EarthQuakeData> earthQuakeDatas) {
    super(context, 0, earthQuakeDatas);

}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listViewItem = convertView;
    if(listViewItem==null){
        listViewItem = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    EarthQuakeData earthQuakeData = getItem(position);

    TextView typeView = (TextView) listViewItem.findViewById(R.id.type);
    typeView.setText(earthQuakeData.getType());


    return listViewItem;








    }
}

AsyncLoaderClass:

public class EarthQuakeLoader extends AsyncTaskLoader<List<EarthQuakeData>> {

public static final String LOG_TAG = MainActivity.class.getName();

private String mUrl;


public EarthQuakeLoader(Context context, String url) {
    super(context);
    this.mUrl = url;
}

@Override
protected void onStartLoading() {
    Log.e(LOG_TAG,"OnStartLoading");
    forceLoad();
}


@Override
public List<EarthQuakeData> loadInBackground() {
    Log.e(LOG_TAG,"LOADINBACKGROUN>>>>>>>>>>>>>>>>>>>");

    if(mUrl==null){
        return null;
    }
   List<EarthQuakeData> fetchRespons = null;


         fetchRespons = EarthQuakeJson.fetchRespons(mUrl);


    return fetchRespons;
}

}

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.bunzlau.eathqueaks.MainActivity">

<EditText
    android:hint="Max magnitude"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/magnitude_text"/>

<Button
    android:text="send"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/send"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview">

    </ListView>



</LinearLayout>

list_item.xml

<LinearLayout 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:layout_height="wrap_content"
    android:id="@+id/type"/>

0 个答案:

没有答案