Hello, that just did not do not get it. You need to open a new screen when you click on the ListView. But pass the id from (content_contests_list.xml) TextView android: id = "@ + id / id"

Contests.java

public class Contests extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {

private String TAG = ContestsView.class.getSimpleName(); private SwipeRefreshLayout swipeRefreshLayout; private ProgressDialog pDialog; private int offSet = 0; private ListView lv; public int OffMoreButtom = 0; // URL to get contacts JSON private static String urlStatic = "https://unkop.com/api/contests?id=9&token=$1$vLyWeNSm$tMhTzT5N7KKu4nznlCZ1Q.&type=2&offset="; private static String url = "https://unkop.com/api/contests?id=9&token=$1$vLyWeNSm$tMhTzT5N7KKu4nznlCZ1Q.&type=2&offset=0"; private static String ContestsCount = "0"; private static String BackUpCount = "0"; private static String contetsEnd = "0"; ArrayList<HashMap<String, String>> contactList; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { //swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); super.onCreate(savedInstanceState); setContentView(R.layout.activity_contests); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); contactList = new ArrayList<>(); lv = (ListView) findViewById(R.id.list); new Contests.GetContacts().execute(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorScheme(R.color.blue, R.color.green, R.color.yellow, R.color.red); swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { new Contests.CountContests().execute(); swipeRefreshLayout.setRefreshing(false); } } ); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.buttom_new_contests, null); lv.addFooterView(view); final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); final NavigationView navigationViewRight = (NavigationView) findViewById(nav_view_right); navigationViewRight.setNavigationItemSelectedListener(this); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Drawer.openDrawer(nav_view); //drawer.closeDrawer(navigationViewRight); } }); } public void hideMoreButtom() { OffMoreButtom = 1; } public void onMyButtonClick(View view) { // выводим сообщение //Toast.makeText(this, "Зачем вы нажали?", Toast.LENGTH_SHORT).show(); setContentView(R.layout.activity_contests); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); contactList = new ArrayList<>(); lv = (ListView) findViewById(R.id.list); new Contests.GetContacts().execute(); } public void openContestsView(View v){ Intent toy = new Intent(Contests.this, ContestsView.class); startActivity(toy); } public void MenuRight(MenuItem item){ DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); NavigationView navigationViewRight = (NavigationView) findViewById(nav_view_right); navigationViewRight.setNavigationItemSelectedListener(this); drawer.openDrawer(navigationViewRight); } public void loadNewContests(View view){ if(OffMoreButtom == 0) { new Contests.GetContacts().execute(); } } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.contests, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.menuRight) { //Toast.makeText(this, "Зачем вы нажали?", Toast.LENGTH_SHORT).show(); } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } public Action getIndexApiAction() { Thing object = new Thing.Builder() .setName("ContestsView Page") // TODO: Define a title for the content shown. // TODO: Make sure this auto-generated URL is correct. .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) .build(); return new Action.Builder(Action.TYPE_VIEW) .setObject(object) .setActionStatus(Action.STATUS_TYPE_COMPLETED) .build(); } @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); AppIndex.AppIndexApi.start(client, getIndexApiAction()); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. AppIndex.AppIndexApi.end(client, getIndexApiAction()); client.disconnect(); } @Override public void onRefresh() { } public class MyAdapter extends SimpleAdapter { public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } public View getView(int position, View convertView, ViewGroup parent) { // here you let SimpleAdapter built the view normally. View v = super.getView(position, convertView, parent); // Then we get reference for Picasso ImageView img = (ImageView) v.getTag(); if (img == null) { img = (ImageView) v.findViewById(R.id.ivBasicImage); v.setTag(img); // <<< THIS LINE !!!! } // get the url from the data you passed to the `Map` // do Picasso // maybe you could do that by using many ways to start String url = (String) ((Map)getItem(position)).get("photo"); boolean ifyoutube = url.contains("/vi/"); com.squareup.picasso.Transformation transformation = new RoundedTransformationBuilder() .borderColor(Color.BLACK) .borderWidthDp(0) .cornerRadiusDp(100) .oval(false) .build(); if (ifyoutube) { Picasso.with(getBaseContext()) .load(url) .fit() .centerCrop() .transform(transformation) .into(img); } else { url = url.replaceAll(".jpg", "_square.jpg"); Picasso.with(getBaseContext()) .load(url) .fit() .centerCrop() .transform(transformation) .into(img); } // return the view return v; } } /** * Async task class to get json by making HTTP call */ public class GetContacts extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(Contests.this); pDialog.setMessage("Loading..."); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { HttpHandler sh = new HttpHandler(); // Making a request to url and getting response String jsonStr = sh.makeServiceCall(url); Log.e(TAG, "Response from url: " + jsonStr); if (jsonStr != null) { try { offSet = offSet + 5; url = urlStatic + offSet; Log.e(TAG, "URL: " + url); JSONObject jsonObj = new JSONObject(jsonStr); contetsEnd = jsonObj.getString("contetsEnd"); Contests.BackUpCount = Contests.ContestsCount; Contests.ContestsCount = jsonObj.getString("Count"); // Getting JSON Array node JSONArray contests = jsonObj.getJSONArray("contestsAll"); //ContestsCount = jsonObj.getString("Count"); // looping through All Contacts for (int i = 0; i < contests.length(); i++) { JSONObject c = contests.getJSONObject(i); String id = c.getString("id"); String type = c.getString("type"); String title = c.getString("title"); String text = c.getString("text"); String count = c.getString("count"); String photo = c.getString("img"); // Phone node is JSON Object JSONObject user_id = c.getJSONObject("user_id"); String id_user = user_id.getString("id"); String name = user_id.getString("name"); String username = user_id.getString("username"); // Phone node is JSON Object JSONObject time = c.getJSONObject("time"); String start = time.getString("start"); String finish = time.getString("finish"); // tmp hash map for single contact HashMap<String, String> contact = new HashMap<>(); // adding each child node to HashMap key => value contact.put("id", id); contact.put("type", type); contact.put("title", title); contact.put("text", text); contact.put("count", count); contact.put("id_user", id_user); contact.put("name", name); contact.put("username", username); contact.put("start", start); contact.put("finish", finish); contact.put("photo", photo); // adding contact to contact list contactList.add(contact); int BazaId = Integer.parseInt(id); int BazaContetsEnd = Integer.parseInt(contetsEnd); if (BazaId == BazaContetsEnd) { hideMoreButtom(); } } } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG) .show(); } }); } } else { Log.e(TAG, "Couldn't get json from server."); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errors!", Toast.LENGTH_LONG) .show(); } }); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) { pDialog.dismiss(); } /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new Contests.MyAdapter( Contests.this, contactList, R.layout.content_contests_list, new String[]{"ids", "type", "title", "text", "count", "id_user", "name", "username", "start", "finish"}, new int[]{R.id.id, R.id.type, R.id.title, R.id.text, R.id.count, R.id.id_user, R.id.name, R.id.username, R.id.start, R.id.finish}); lv.setAdapter(adapter); } } private class CountContests extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { HttpHandler sh = new HttpHandler(); // Making a request to url and getting response String jsonStr = sh.makeServiceCall(urlStatic+"0"); if (jsonStr != null) { try { Log.e(TAG, "URL: " + urlStatic+"0"); JSONObject jsonObj = new JSONObject(jsonStr); // Getting JSON Array node Contests.BackUpCount = Contests.ContestsCount; Contests.ContestsCount = jsonObj.getString("Count"); Log.e(TAG, "ContestsCount: " + ContestsCount); Log.e(TAG, "BackUpCount: " + BackUpCount); } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG) .show(); } }); } } else { Log.e(TAG, "Couldn't get json from server."); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errors!", Toast.LENGTH_LONG) .show(); } }); } return null; } protected void onPostExecute(Void result) { int BackCount = Integer.parseInt(BackUpCount); int Count = Integer.parseInt(ContestsCount); if(BackCount != Count) { OffMoreButtom = 0; contactList.clear(); url = urlStatic; offSet = 0; new Contests.GetContacts().execute(); swipeRefreshLayout.setRefreshing(false); } else { Toast.makeText(Contests.this, "Nothing new", Toast.LENGTH_SHORT).show(); swipeRefreshLayout.setRefreshing(false); } } } @Override protected void onRestart() { super.onRestart(); // Always call the superclass method first // Activity being restarted from stopped state } 

}

content_contests_list.xml

 <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:paddingTop="10dp" android:paddingRight="10dp" android:paddingLeft="10dp"> <ImageView android:layout_height="85dp" android:id="@+id/ivBasicImage" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_width="85dp" android:layout_marginBottom="10dp" android:layout_marginRight="10dp"> </ImageView> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="Title:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:textSize="18sp" /> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000" android:textStyle="bold" android:text="Title:" android:textSize="18sp" android:maxLines="1" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView2" android:layout_toEndOf="@+id/textView2" android:layout_marginLeft="11dp" android:layout_marginStart="11dp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="Posted by:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView5" android:textSize="14sp" /> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/colorPrimary" android:textSize="14sp" android:textStyle="bold" android:text="Name" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView5" android:layout_toEndOf="@+id/textView5" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:maxLines="1" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="Type:" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/type" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorAccent" android:text="Type:" android:layout_marginLeft="39dp" android:layout_marginStart="39dp" android:textStyle="normal|bold" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:maxLines="1" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="Date finish:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" /> <TextView android:id="@+id/finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/common_plus_signin_btn_text_dark_disabled" android:text="26.12.2016" android:maxLines="1" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView4" android:layout_toEndOf="@+id/textView4" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" /> </RelativeLayout> </LinearLayout> </LinearLayout> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingTop="10dp" android:textColor="@color/colorPrimaryDark" android:textSize="16sp" android:text="sffad fdasf asdf asdfsad f af adsf asdf sdaf dsaf adsf dasf dasf asdfsffad fdasf asdf asdfsad f af adsf asdf sdaf dsaf adsf dasf dasf asdf" android:textAlignment="viewStart" android:background="#fff" android:paddingLeft="10dp" android:paddingRight="10dp" android:maxLines="2" /> <Button android:text="View" android:layout_width="match_parent" android:layout_height="32dp" android:id="@+id/View" android:background="@color/colorAccent" android:textColor="@color/common_google_signin_btn_text_dark_default" android:onClick="closePreload" android:layout_weight="0.10" android:visibility="gone" /> <TextView android:id="@+id/id" android:layout_width="fill_parent" android:layout_height="0dp" android:paddingBottom="2dip" android:paddingTop="6dip" android:textColor="@color/colorPrimaryDark" android:textSize="16sp" android:textStyle="bold" android:visibility="gone" /> <TextView android:id="@+id/count" android:layout_width="fill_parent" android:layout_height="0dp" android:paddingBottom="2dip" android:textColor="@color/colorAccent" android:visibility="gone" /> <TextView android:id="@+id/start" android:layout_width="match_parent" android:layout_height="0dp" android:textColor="@color/common_plus_signin_btn_text_dark_disabled" android:textStyle="bold" android:visibility="gone" /> <TextView android:id="@+id/username" android:layout_width="match_parent" android:layout_height="0dp" android:paddingBottom="2dip" android:textColor="@color/colorAccent" android:visibility="gone" /> <TextView android:id="@+id/id_user" android:layout_width="wrap_content" android:layout_height="0dp" android:textColor="#5d5d5d" android:textStyle="bold" android:visibility="gone" /> 

  content_contests_view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_contests" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.unkop.unkop.Contests" tools:showIn="@layout/app_bar_contests" android:orientation="vertical" android:background="#EBEBEB" android:layout_marginLeft="-20dp" android:layout_marginRight="-20dp"> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="0dp"/> </android.support.v4.widget.SwipeRefreshLayout> 

  • why do you need to pass this widget ID, it is the same for all list items, and it is useless outside of item markup. Maybe you need to transfer the ID of the record, not the widget? - pavlofff

1 answer 1

  • To ListView add OnItemClickListener;
  • Then, when a click on one of the ListView elements, then get a link to the TextView android: id = "@ + id / id" from the clicked element;
  • After that, create a new Intent to open the corresponding Activity, using putExtra put the id from the TextView;
  • In the open Activity get the id.

OnItemClickListener

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView textView_id = ((TextView)view.findViewById(R.id.id)); Intent i = new Intent(Contests.this, NEW_ACTIVITY.class); i.putExtra("EXTRA_ID", textView_id.getText()); startActivity(i); } }); 

Getting an id in an open Activity

 Intent intent = getIntent(); String id = intent.getStringExtra("EXTRA_ID");