在Java类中进行类函数数据传输

时间:2016-05-29 10:37:07

标签: java android android-activity

我想访问SeriesSearch课程中的标题,流派和其他字符串。我使用了filmveri()函数,它返回ArrayList。我尝试在onCreate()方法中找到但失败了。

public class SeriesActivity extends AppCompatActivity implements Serializable{
    TextView  seriesname, seriesYear, seriesGenre;
    ImageView seriesPoster;

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

        seriesname = (TextView) findViewById(R.id.icerik1);
        seriesYear = (TextView) findViewById(R.id.icerik2);
        seriesGenre=(TextView) findViewById(R.id.icerik3);
        seriesPoster=(ImageView)findViewById(R.id.seriesPoster);

        Intent intent = getIntent();
        String arama =intent.getStringExtra("arama");

        //String aranan =arama.getText().toString();

        SeriesSearch filmdata;
        List<String> listem = filmdata.filmveri();

        new SeriesSearch().execute(arama);

        Log.e("GELENURL:", arama);
    }

    public class SeriesSearch extends AsyncTask<String, String, String>
    {
        String title, genre, year, resim;

        @Override

        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... voids) {
            String seriesUrl = "http://www.omdbapi.com/?t="+voids[0]+"&y=&plot=short&r=json";
            Log.e("seriesurl:", seriesUrl);
            JSONObject jsonObject=null;
            try {
                String json = seriesParser.getJSONFromUrl(seriesUrl);
                try {
                    jsonObject = new JSONObject(json);
                }catch (JSONException e){
                    e.printStackTrace();
                }
                //JSONArray listArray = jsonObject.getJSONArray("Title");
                //JSONObject firstObj = jsonObject.getJSONObject("Title");
                title = jsonObject.getString("Title");
                genre = jsonObject.getString("Genre");
                year = jsonObject.getString("Year");
                resim = jsonObject.getString("Poster");

            }catch (JSONException e){
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {

            super.onPostExecute(aVoid);

            //Toast.makeText(getBaseContext(), title, Toast.LENGTH_LONG).show();
            seriesname.setText(title);
            seriesGenre.setText(genre);
            seriesYear.setText(year);
            //Log.e("GelenResimURL:", GetResim);
            Picasso.with(getApplicationContext())
                    .load(resim)
                    .into(seriesPoster);
        }
        protected List<String> filmveri(){
            List<String> listem = new ArrayList<>();
            listem.add(title);
            listem.add(genre);
            listem.add(year);
            return listem;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先添加全局变量&#34; lst&#34;在你的&#34; SeriesSearch&#34;类:

List<String> lst;

然后在你的&#34; SeriesSearch&#34;中添加构造函数如下所述:

public SeriesSearch (List<String> lst)
{
    this.lst = lst;
}

现在而不是#34; onCreate&#34;中的这一行new SeriesSearch().execute(arama);。函数使用下面的代码:

SeriesSearch sr = new SeriesSearch(listem );
sr.execute(arama);

我希望它会对你有所帮助。