2016-07-27 11 views
0

だから何をしようとしているのですか?
HOMESCREEN --- newactivity ---> INTERNETSEARCHSCREEN ---- newactivity --- > EDITINGSCREENリストビューからsqlite(アンドロイドスタジオ)で他のアクティビティにjson情報を取得する方法

imはjsonを取得していますが、リストビューに表示されますが、今はリストビューから1つの映画を選択して映画名/年/夏/ imageurlを取得してEDITSCREENに表示します私はすべてのjsonのデータを取得し、私が望んでいた指定されたフィールドではありません....そしてhmmm私はSQLにそれを保存するか、編集画面で私のデータベースに追加する任意の意見は歓迎される場合:D

public class AddFromInternet extends AppCompatActivity { 
MovieDataSource mds; 
public EditText EtSearch; 
public ListView listViewInternetScreen; 
Button btnCancel; 
Button btnGo; 
ArrayAdapter<String> arrayAdapter; 
ArrayList<String> nameOfMovie = new ArrayList<>(); 
ArrayList<String> yearOfMovie = new ArrayList<>(); 
ArrayList<String> id = new ArrayList<>(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mds = new MovieDataSource(this); 
    setContentView(R.layout.activity_add_from_internet); 

    EtSearch = (EditText) findViewById(R.id.EtSearch); 
    listViewInternetScreen = (ListView) findViewById(R.id.listViewInternetScreen); 
    btnGo = (Button) findViewById(R.id.btnGo); 
    assert btnGo != null; 
    btnGo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 



      final String search = "http://www.omdbapi.com/?s=" + EtSearch.getText().toString(); 
      Log.e("JSON", search); 
      new JSONParser().execute(search); 
      ArrayList<String> list = new ArrayList<>(nameOfMovie); 
      arrayAdapter = new ArrayAdapter<>(AddFromInternet.this, 
        android.R.layout.simple_list_item_1, list); 
      arrayAdapter.notifyDataSetChanged(); 
      listViewInternetScreen.setAdapter(arrayAdapter); 
      Log.e("JSON MOVIE", String.valueOf(nameOfMovie)); 

     } 
    }); 

    btnCancel = (Button) findViewById(R.id.btnCancelSearch); 
    btnCancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      listViewInternetScreen.setAdapter(null); 
      nameOfMovie.clear(); 
      arrayAdapter.notifyDataSetChanged(); 
      EtSearch.setText(""); 
      Log.e("JSON MOVIE", String.valueOf(nameOfMovie)); 
     } 
    }); 

    listViewInternetScreen.setClickable(true); 
    listViewInternetScreen.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long i) { 
      //Movies newmovie= arrayAdapter.getItem(position); 
      String moviename = String.valueOf(nameOfMovie); 
      String moviesummery = String.valueOf(yearOfMovie); 
      String imageUrl = String.valueOf(id); 
      Movies newmovie = new Movies(moviename,moviesummery,imageUrl,0); 
      mds.open(); 
      //will send all data to DB 
      newmovie = mds.createMovies(newmovie); 
      Log.e("JSON TO SQL", String.valueOf(mds.allColumns)); 
      mds.close(); 
      Intent intenttoEditScreen=new Intent(AddFromInternet.this, EditMovie.class); 
      setResult(RESULT_OK,intenttoEditScreen); 
      intenttoEditScreen.putExtra("rowId", newmovie.getMovieId()); 
      startActivity(intenttoEditScreen); 


     } 

     }); 

} 


//will create the jsonparser and the asynctask in this class for ease of use 
public class JSONParser extends AsyncTask<String, String , String> { 

    ProgressDialog dialogue; 
    Context context; 


    @Override 
    protected void onProgressUpdate(String... values) { 


     ProgressDialog progdialog = ProgressDialog.show(AddFromInternet.this,"dsa","das",true); 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected String doInBackground(String... params) { 


     HttpURLConnection httpConnection = null; 
     BufferedReader reader = null; 
     StringBuffer buffer = new StringBuffer(); 
     try { 
      URL url = new URL(params[0]); 
      httpConnection = (HttpURLConnection) url.openConnection(); 
      Log.e("Json error"," try conntect to internet"); 
      httpConnection.connect(); 
      Log.e("Json error","sucsses connecting to the internet"); 
      InputStream stream = httpConnection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      return buffer.toString(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if (httpConnection != null) 
       httpConnection.disconnect(); 
      try { 
       if (reader != null) 
        reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return "Json wasn't downloaded..."; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //dialogue.dismiss(); 
     arrayAdapter.notifyDataSetChanged(); 


     String finalJson = result; 

     try { 
      JSONObject jsonObject = new JSONObject(finalJson); 
      JSONArray parentArray = jsonObject.getJSONArray("Search"); 
      StringBuffer finalStringBuffer = new StringBuffer(); 
      for (int i=0; i<parentArray.length(); i++){ 
       JSONObject finalJsonObject = parentArray.getJSONObject(i); 
       String movieName = finalJsonObject.getString("Title"); 
       nameOfMovie.add(movieName); 
       String year = finalJsonObject.getString("Year"); 
       yearOfMovie.add(year); 
       String omdbID = finalJsonObject.getString("imdbID"); 
       id.add(omdbID); 
       finalStringBuffer.append(movieName + " , " + year + " , " + omdbID + "\n"); 






      } 

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

    } 

    /* @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialogue = new ProgressDialog(); 
     dialogue.setTitle("Loading items.."); 
     dialogue.show(); 
    }*/ 
} 

}

とEDITSCREEN:

public class EditMovie extends AppCompatActivity { 

EditText etMovieName ; 
EditText etMoviewSummery; 
EditText etURL ; 
Button btnsave; 
Button btnshow; 
Button btncancel; 
MovieDataSource mds; 
long id ; 

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


    //Instantiate DB query class 
    mds=new MovieDataSource(this); 
    //Run init method 
    init(); 

    //method to save/update customer to DB 
    btnsave.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      String moviename=etMovieName.getText().toString(); 
      String moviesummery=etMoviewSummery.getText().toString(); 
      String imageurl=etURL.getText().toString(); 

      //Instantiate , values from Intent 
      Movies newmovie=new Movies(moviename,moviesummery,imageurl,id); 
      mds.open(); 
      //will access the CustomerDataSource class to updateByRow method 
      mds.updateByRow(newmovie); 
      mds.close(); 
      //Transfering result to MainActivity 
      Intent i=new Intent(); 
      setResult(RESULT_OK,i); 
      finish(); 

     } 
    }); 

} 

public void init() 
{ 
    etMovieName=(EditText) findViewById(R.id.etMovieNameEDIT); 
    etMoviewSummery=(EditText) findViewById(R.id.etSummeryEDIT); 
    etURL=(EditText) findViewById(R.id.etURLEDIT); 

    btnsave=(Button) findViewById(R.id.btnsaveEDIT); 
    //btndelete=(Button) findViewById(R.id.updateBtnDelete); 
    // tvId=(TextView) findViewById(R.id.updatetvId); 

    //get the intent with the row id number & request code 
    id = getIntent().getLongExtra("rowId", 0); 
    //If DB row number is not 0 
    if(id!=0) { 
     //open DB connection 
     mds.open(); 
     //Instantiate and get the movie id from calling screen 
     Movies c = mds.getMovieById(id); 
     etMovieName.setText(c.getMovieName()); 
     etMoviewSummery.setText(c.getMovieSummery()); 
     etURL.setText(c.getImageURL()); 
     mds.close(); 
    } 
} 
} 

ああ、誰でも私はそれを検索するための二回Goボタンをクリックする必要がある理由のアイデアを持っている場合は?

と支援しよう/喜んで、誰のための感謝:D

答えて

0

は、あなたが実際にあなたが編集画面にオフに送信意思でそれを置くことができます。ちょうどあなたのオブジェクトが直列であることを確認してください

myObj = (MyObj) intent.getSerializableExtra("PUT_EXTRA_KEY"); 

:あなたの代わりにobjectを送信したい場合は

Intent intent = new Intent(this, EditMovie.class); 
intent.putExtra("json", myJson); 
startActivity(intent); 

は、その後、あなたのEditMovieのonCreate

Intent intent = getIntent(); 
String myJson = intent.getStringExtra("json"); 

に、それはより多くのようなものになるだろう。

+0

ありがとう、しかし、私はそれをインテントに入れようとしていますが、私はどんなものかを知りません。 intent.putExtra( "json"、myJson - >この文字列/値は何ですか? と新生児の質問には申し訳ありません:) – rollcage

+0

あなたは何をしたいと思っています。あなたは名前/年/夏/イメージュールがほしいと思っていますか? – Bill

+0

hmmアクティビティ間でデータを渡すことは問題ではありません。問題は、ちょうどクリックされたものの代わりに検索されたすべてのムービーを取得することです。 私はこのからimdbIDを取得する必要があると思う: {"検索": "{" "タイトル:"カザフスタンの栄光の国を恩恵を受けるためのアメリカの文化の学習 "、"年 ":" 2006 "、" imdbID ":" tt0443453 "、"タイプ ":"ムービー "、"ポスター ":" http://ia.media-imdb.com/images/M/[email protected]@.V1_SX300.jpg "}、{" Ti .. ....... そして次に "http://www.omdbapi.com/**insted"の別の検索をします "私は"私は "映画"についての詳細を与える – rollcage

関連する問題