I have following code in which I am struggling to pass value of unique id from the parsed data from json to the DetailActivity.java
. I want to use that value of unique_id from Mainactivity.java
in DetailActivity.java
as a parameter to the URL .I am a newbie so help me out on this.
I am putting the hole Mainactivity.java
and DetailActivty.java
public class MainActivity extends AppCompatActivity {
private TextView text1;
private ListView lisView;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions).build();
ImageLoader.getInstance().init(config); // Do it on Application start
lisView = (ListView) findViewById(R.id.lvnews);
new JSONTask().execute("http://cricapi.com/api/cricket");
Button button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute("http://cricapi.com/api/cricket");
}
});
}
public class JSONTask extends AsyncTask<String, String, List<NewsModel>> {
@Override
protected List<NewsModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("data");
List<NewsModel> newsModelList = new ArrayList<>();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
NewsModel newsModel = new NewsModel();
newsModel.setHeadline(finalObject.getString("unique_id"));
newsModel.setAuthor(finalObject.getString("description")); // newsModel.setImage(finalObject.getString("Image"));
newsModel.setDescription(finalObject.getString("title"));
newsModelList.add(newsModel);
}
return newsModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(final List<NewsModel> result) {
super.onPostExecute(result);
if(result != null) {
final NewsAdapter newsAdapter = new NewsAdapter(getApplicationContext(), R.layout.row, result);
lisView.setAdapter(newsAdapter);
lisView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
startActivity(intent);
}
});
}
}
}
public class NewsAdapter extends ArrayAdapter{
public List<NewsModel> newsModelList;
private int resource;
private LayoutInflater inflater;
public NewsAdapter(Context context, int resource, List<NewsModel> objects) {
super(context, resource, objects);
newsModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row, null);
holder.headline = (TextView)convertView.findViewById(R.id.headlineview);
holder.author = (TextView)convertView.findViewById(R.id.authorview);
//holder.image = (ImageView)convertView.findViewById(R.id.imageView);
holder.description = (TextView)convertView.findViewById(R.id.descriptionview);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//holder.headline.setText(newsModelList.get(position).getHeadline());
holder.author.setText(newsModelList.get(position).getAuthor());
holder.description.setText(newsModelList.get(position).getDescription());
return convertView;
}
private class ViewHolder { // private ImageView image;
private TextView headline;
private TextView author;
private TextView description;
}
}
}
Now to the DetailActivity in which I would like to use unique_id as a parameter to URL
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// here I want to access that unique_id and pass it as id to url
//Example http://cricapi.com/api/cricketScore?unique_id=946765
}
}
Thanks in Advance