简体   繁体   中英

Making a clickable list view in android

I have a following Codes I want any one say to all of You How can make a clickable list view my code and when we click on my list view open a new Activity show full view of List View Content. Please Help me. These are following codes .

  • news.class activity name activity_news and content_news and list_view_activity
  • newsshow.java

/* news.java */
package com.synergywebdesigners.nima;


import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class news extends AppCompatActivity {
    public static final String JSON_URL = "http://www.synergywebdesigners.com/synergywebdesigners.com/ashish/nima/get_all_news.php";
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        listView = (ListView) findViewById(R.id.listView);
        sendRequest();
    }

    private void sendRequest() {
        StringRequest stringRequest = new StringRequest(JSON_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(news.this, error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

        com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void showJSON(String json) {
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        newsshow cl = new newsshow(this, ParseJSON.ids, ParseJSON.ttitle, ParseJSON.tmessage, ParseJSON.tcreated);
        listView.setAdapter(cl);
    }
}


----------

    package com.synergywebdesigners.nima;

    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.text.Html;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TableLayout;
    import android.widget.TextView;

    /**
     * Created by PC on 20-Dec-16.
     */

    public class newsshow  extends  ArrayAdapter<String>  {
        private String[] ids;
        private String[] ttitle;
        private String[] tmessage;
        private String[] tcreated;
        private Activity context;

        public newsshow(Activity context, String[] ids, String[] title, String[] message, String[] created) {
            super(context, R.layout.list_view_layout, ids);
            this.context = context;
            this.ids = ids;
            this.ttitle = title;
            this.tmessage = message;
            this.tcreated = created;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
            TextView id = (TextView) listViewItem.findViewById(R.id.id);
            TextView title = (TextView) listViewItem.findViewById(R.id.title);
            TextView message = (TextView) listViewItem.findViewById(R.id.message);
            TextView created = (TextView) listViewItem.findViewById(R.id.created);
            id.setText(ids[position]+".");
            title.setText(ttitle[position]);
            message.setText(Html.fromHtml(tmessage[position]));
            created.setText(tcreated[position]);
            return listViewItem;

        }
    }


    ----------
<!-- content news.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_news"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:paddingTop="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_news">
    <ScrollView android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillViewport="true">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="0dp"
            android:paddingBottom="10dp">

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="1dp"
                android:paddingBottom="16dp"
                tools:context=".news">

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:id="@+id/listView" />


            </LinearLayout>
        </LinearLayout></ScrollView>
</RelativeLayout>

<!-- list-view-layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:textColor="#000000"
            android:id="@+id/id"/>
        <TextView
            android:paddingLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000"
            android:textStyle="bold"
            android:id="@+id/title"
            />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textColor="#000000"
        android:id="@+id/message"
        android:maxLength="1000"
        android:paddingLeft="20dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="New Text"
        android:id="@+id/created"
        android:paddingLeft="20dp"/>

</LinearLayout>

If you want a clickable list, why don't you have a click listener on your list items ? If a click should activate an activity, consider something as:

CurrentActivity.this.startActivity(new Intent(CurrentActivity.this, NewActivity.class));

Perhaps you could also explain a little bit more what you have and what you expected from this code snippet...

Good luck :)

You can do this way in your main activity:

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class news extends AppCompatActivity {
    public static final String JSON_URL = "http://www.synergywebdesigners.com/synergywebdesigners.com/ashish/nima/get_all_news.php";
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        listView = (ListView) findViewById(R.id.listView);

// add these lines in your code 

 // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              // selected item 
              String product = ((TextView) view).getText().toString();

              // Launching new Activity on selecting single List Item
      Intent i = new Intent(getApplicationContext(), AnotherActivity.class);
              // sending data to new activity
              i.putExtra("item", item);
              startActivity(i);

          }
        });

        sendRequest();
    }

    private void sendRequest() {
        StringRequest stringRequest = new StringRequest(JSON_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(news.this, error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

        com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void showJSON(String json) {
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        newsshow cl = new newsshow(this, ParseJSON.ids, ParseJSON.ttitle, ParseJSON.tmessage, ParseJSON.tcreated);
        listView.setAdapter(cl);
    }
}

Now, In your other activity , you can get item of first activity like this, in onCreate() method :

 Intent i = getIntent();
        // getting attached intent data
        String item = i.getStringExtra("item");

Also, you can follow this tutorial for more detail: http://www.androidhive.info/2011/10/android-listview-tutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM