【问题标题】:how to get a json information from a listview to other activity with sqlite (android studio)如何使用sqlite(android studio)从listview获取json信息到其他活动
【发布时间】:2016-12-01 23:38:16
【问题描述】:

所以我想做的是来自:
主屏幕---新活动--->INTERNETSEARCHSCREEN----新活动--->编辑屏幕

我正在获取 json,它在我的列表视图中显示良好,但现在我想从列表视图中选择一部电影并获取电影名称/年份/夏季/图像 URL 并将其显示在 EDITSCREEN 但是当我这样做时,我得到了所有json数据而不是我想要的指定字段....嗯,我不知道我是否应该将它存储在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();
    }*/
}

}

和编辑屏幕:

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();
    }
}
}

啊,如果有人知道为什么我需要单击两次“开始”按钮才能搜索?

感谢任何愿意/试图提供帮助的人:D

【问题讨论】:

    标签: android json sqlite listview


    【解决方案1】:

    您实际上可以将其放入您发送到编辑屏幕的意图中。

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

    然后在你的 EditMovie 的onCreate

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

    如果您想发送object,则更像是:

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

    只要确保你的对象是可序列化的。

    【讨论】:

    • 谢谢,但是嗯,我试图把它放在意图中,但我不明白应该是什么:intent.putExtra("json", myJson-->这是什么字符串/值?) ;抱歉这个新问题:)
    • 无论你想要什么,我只是向你展示如何在活动之间传递数据。我相信你想要 name/year/summery/imageurl ?
    • hmm 在活动之间传递数据不是问题,问题是我得到了所有被搜索的电影,而不是刚刚被点击的电影。我想我需要从中获取 imdbID:{"Search":[{"Title":"Borat: 美国文化学习使哈萨克斯坦光荣的国家受益","Year":"2006","imdbID": "tt0443453","Type":"movie","Poster":"ia.media-imdb.com/images/M/…"},{"Ti......... 然后在omdbapi.com**insted of "s " 我放了 "i",它提供了有关电影的更多详细信息
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多