I need to control the click on the custom Listview so that it performs an operation depending on which one is pressed. I already have the OnItemClickListener.
But, how can I pass the data contained in the Item to another Activity?
I leave the main code
public class Noticias extends AppCompatActivity {
ArrayList<Product> arrayList;
ListView lv;
ProgressDialog pdialog = null;
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_noticias);
android:setTitle("Noticias");
arrayList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listView);
context = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
pdialog = ProgressDialog.show(context, "", "Buscando Noticias...", true);
new ReadJSON().execute("http://xxxxxxx/xxxxx/xxxxxx.php");
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getApplicationContext(), "Click en la posición " + position, Toast.LENGTH_SHORT).show();
}
});
}
class ReadJSON extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
pdialog.dismiss();
try {
JSONArray jsonarray = new JSONArray(content);
for(int i =0;i<jsonarray.length(); i++){
JSONObject productObject = jsonarray.getJSONObject(i);
arrayList.add(new Product(
productObject.getString("nombre"),
productObject.getString("contenido"),
productObject.getString("extra1")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adapter);
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
// create a url object
URL url = new URL(theUrl);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
// read from the urlconnection via the bufferedreader
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
I've been searching for several days and I only find information with locally created Arrays
This is the code of the new activity where I receive the Item data
public class Noticia extends AppCompatActivity {
TextView tvname,tvcontenido;
ImageView imgview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_noticia);
Intent intent = getIntent();
String nombre = intent.getStringExtra("nombre");
String contenido = intent.getStringExtra("contenido");
String extra1 = intent.getStringExtra("extra1");
tvname = (TextView)findViewById(R.id.tvname);
tvcontenido = (TextView)findViewById(R.id.tvcontenido);
imgview = (ImageView)findViewById(R.id.imgview);
tvname.setText(nombre);
tvcontenido.setText(contenido);
imgview.setImageURI(Uri.parse(extra1));
}
}
But it only shows me the fields with text ... the Image does not show it