2016-11-14 333 views
-1

Firnds我使用Android的凌空图书馆从JSONAndroid的凌空列表视图onclicklistener

获取数据我已经成功地将这一部分,但不能点击的项目

enter image description here

我的代码编号

UpcomingJourney.java

package com.mssinfotech.dropjourney; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Cache; 
import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.mssinfotech.dropjourney.adapter.UpcommingJourneyListAdapter; 
import com.mssinfotech.dropjourney.app.AppController; 
import com.mssinfotech.dropjourney.data.FeedItem; 
import com.mssinfotech.dropjourney.data.UpcommingJourneyFeedItem; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 

public class UpcomingJourney extends ActionBarActivity { 

    private static final String TAG = UpcomingJourney.class.getSimpleName(); 
    private ListView listView; 
    private UpcommingJourneyListAdapter listAdapter; 
    private List<UpcommingJourneyFeedItem> feedItems; 
    private DBHelper mydb; 
    private static String id=""; 
    private String URL_FEED = ""; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mydb = new DBHelper(this); 
     Cursor MyData = mydb.getMyData(); 
     MyData.moveToFirst(); 
     id = MyData.getString(MyData.getColumnIndex("id")); 
     setContentView(R.layout.activity_upcoming_journey); 

     Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); 
     mActionBarToolbar.setTitle("Up-comming Journey"); 

     setSupportActionBar(mActionBarToolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     URL_FEED = CommonUtilities.API_URL+"?type=DriverUpcommingJourney&vid="+id; 
     listView = (ListView) findViewById(R.id.list); 
     feedItems = new ArrayList<UpcommingJourneyFeedItem>(); 
     listAdapter = new UpcommingJourneyListAdapter(this, feedItems); 
     listView.setAdapter(listAdapter); 
     listView.setFastScrollEnabled(true); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
       Toast.makeText(getApplicationContext(),"You clicked"+position,Toast.LENGTH_SHORT).show(); 
      } 

     }); 
     // We first check for cached request 
     Cache cache = AppController.getInstance().getRequestQueue().getCache(); 
     Cache.Entry entry = cache.get(URL_FEED); 
     Log.d("url mss",URL_FEED); 
     if (entry != null) { 
      // fetch the data from cache 
      try { 
       String data = new String(entry.data, "UTF-8"); 
       try { 
        parseJsonFeed(new JSONObject(data)); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 

     } else { 
      // making fresh volley request and getting json 
      JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET, 
        URL_FEED, null, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        VolleyLog.d(TAG, "Response: " + response.toString()); 
        if (response != null) { 
         parseJsonFeed(response); 
        } 
       } 
      }, new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       } 
      }); 
      // Adding request to volley request queue 
      AppController.getInstance().addToRequestQueue(jsonReq); 
     } 
    } 
    private void parseJsonFeed(JSONObject response) { 
     try { 
      JSONArray feedArray = response.getJSONArray("feed"); 
      String ststus=response.getString("status"); 
      if(ststus.equals("success")) { 
       for (int i = 0; i < feedArray.length(); i++) { 
        JSONObject feedObj = (JSONObject) feedArray.get(i); 

        UpcommingJourneyFeedItem item = new UpcommingJourneyFeedItem(); 
        item.setId(feedObj.getInt("id")); 
        item.setName(feedObj.getString("name")); 

        // Image might be null sometimes 
        String image = feedObj.isNull("image") ? null : feedObj 
          .getString("image"); 
        item.setImge(image); 
        item.setStatus(feedObj.getString("status")); 
        item.setProfilePic(feedObj.getString("profilePic")); 
        item.setTimeStamp(feedObj.getString("timeStamp")); 
        // url might be null sometimes 
        String feedUrl = feedObj.isNull("url") ? null : feedObj 
          .getString("url"); 
        item.setUrl(feedUrl); 
        feedItems.add(item); 

        // notify data changes to list adapater 
        listAdapter.notifyDataSetChanged(); 

       } 
      }else{ 
       LinearLayout layone= (LinearLayout) this.findViewById(R.id.errorImage);// change id here 
       layone.setVisibility(View.VISIBLE); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

UpcommingJourneyListAdapter.java

package com.mssinfotech.dropjourney.adapter; 
import com.mssinfotech.dropjourney.FeedImageView; 
import com.mssinfotech.dropjourney.R; 
import com.mssinfotech.dropjourney.app.AppController; 
import com.mssinfotech.dropjourney.data.UpcommingJourneyFeedItem; 

import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.text.Html; 
import android.text.TextUtils; 
import android.text.format.DateUtils; 
import android.text.method.LinkMovementMethod; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.NetworkImageView; 

public class UpcommingJourneyListAdapter extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<UpcommingJourneyFeedItem> feedItems; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
    public UpcommingJourneyListAdapter(Activity activity, List<UpcommingJourneyFeedItem> feedItems) { 
     this.activity = activity; 
     this.feedItems = feedItems; 
    } 

    @Override 
    public int getCount() { 
     return feedItems.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return feedItems.get(location); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.feed_item, null); 

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 

     TextView name = (TextView) convertView.findViewById(R.id.name); 
     TextView timestamp = (TextView) convertView.findViewById(R.id.timestamp); 
     TextView statusMsg = (TextView) convertView.findViewById(R.id.txtStatusMsg); 
     TextView url = (TextView) convertView.findViewById(R.id.txtUrl); 
     NetworkImageView profilePic = (NetworkImageView) convertView.findViewById(R.id.profilePic); 
     FeedImageView feedImageView = (FeedImageView) convertView 
       .findViewById(R.id.feedImage1); 

     UpcommingJourneyFeedItem item = feedItems.get(position); 

     name.setText(item.getName()); 

     // Converting timestamp into x ago format 
     CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
       Long.parseLong(item.getTimeStamp()), 
       System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS); 
     timestamp.setText(timeAgo); 

     // Chcek for empty status message 
     if (!TextUtils.isEmpty(item.getStatus())) { 
      statusMsg.setText(item.getStatus()); 
      statusMsg.setVisibility(View.VISIBLE); 
     } else { 
      // status is empty, remove from view 
      statusMsg.setVisibility(View.GONE); 
     } 

     // Checking for null feed url 
     if (item.getUrl() != null) { 
      url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">" + item.getUrl() + "</a> ")); 
      // Making url clickable 
      url.setMovementMethod(LinkMovementMethod.getInstance()); 
      url.setVisibility(View.VISIBLE); 
     } else { 
      // url is null, remove from the view 
      url.setVisibility(View.GONE); 
     } 

     // user profile pic 
     profilePic.setImageUrl(item.getProfilePic(), imageLoader); 

     // Feed image 
     if (item.getImge() != null) { 
      feedImageView.setImageUrl(item.getImge(), imageLoader); 
      feedImageView.setVisibility(View.VISIBLE); 
      feedImageView 
        .setResponseObserver(new FeedImageView.ResponseObserver() { 
         @Override 
         public void onError() { 
         } 

         @Override 
         public void onSuccess() { 
         } 
        }); 
     } else { 
      feedImageView.setVisibility(View.GONE); 
     } 

     return convertView; 
    } 

} 

AppController.java

package com.mssinfotech.dropjourney.app; 

import com.mssinfotech.dropjourney.volley.LruBitmapCache; 
import android.app.Application; 
import android.text.TextUtils; 

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.Volley; 

public class AppController extends Application { 

    public static final String TAG = AppController.class.getSimpleName(); 

    private RequestQueue mRequestQueue; 
    private ImageLoader mImageLoader; 
    LruBitmapCache mLruBitmapCache; 

    private static AppController mInstance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 

    public static synchronized AppController getInstance() { 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     if (mRequestQueue == null) { 
      mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
     } 

     return mRequestQueue; 
    } 

    public ImageLoader getImageLoader() { 
     getRequestQueue(); 
     if (mImageLoader == null) { 
      getLruBitmapCache(); 
      mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache); 
     } 

     return this.mImageLoader; 
    } 

    public LruBitmapCache getLruBitmapCache() { 
     if (mLruBitmapCache == null) 
      mLruBitmapCache = new LruBitmapCache(); 
     return this.mLruBitmapCache; 
    } 

    public <T> void addToRequestQueue(Request<T> req, String tag) { 
     req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
     getRequestQueue().add(req); 
    } 

    public <T> void addToRequestQueue(Request<T> req) { 
     req.setTag(TAG); 
     getRequestQueue().add(req); 
    } 

    public void cancelPendingRequests(Object tag) { 
     if (mRequestQueue != null) { 
      mRequestQueue.cancelAll(tag); 
     } 
    } 
} 

UpcommingJourneyFeedItem.java

package com.mssinfotech.dropjourney.data; 

public class UpcommingJourneyFeedItem { 
    private int id; 
    private String name, status, image, profilePic, timeStamp, url; 

    public UpcommingJourneyFeedItem() { 
    } 

    public UpcommingJourneyFeedItem(int id, String name, String image, String status, 
            String profilePic, String timeStamp, String url) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.image = image; 
     this.status = status; 
     this.profilePic = profilePic; 
     this.timeStamp = timeStamp; 
     this.url = url; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getImge() { 
     return image; 
    } 

    public void setImge(String image) { 
     this.image = image; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public String getProfilePic() { 
     return profilePic; 
    } 

    public void setProfilePic(String profilePic) { 
     this.profilePic = profilePic; 
    } 

    public String getTimeStamp() { 
     return timeStamp; 
    } 

    public void setTimeStamp(String timeStamp) { 
     this.timeStamp = timeStamp; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 
} 

LruBitmapCache.java

package com.mssinfotech.dropjourney.volley; 

import com.android.volley.toolbox.ImageLoader.ImageCache; 

import android.graphics.Bitmap; 
import android.support.v4.util.LruCache; 

public class LruBitmapCache extends LruCache<String, Bitmap> implements 
     ImageCache { 
    public static int getDefaultLruCacheSize() { 
     final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 
     final int cacheSize = maxMemory/8; 

     return cacheSize; 
    } 

    public LruBitmapCache() { 
     this(getDefaultLruCacheSize()); 
    } 

    public LruBitmapCache(int sizeInKiloBytes) { 
     super(sizeInKiloBytes); 
    } 

    @Override 
    protected int sizeOf(String key, Bitmap value) { 
     return value.getRowBytes() * value.getHeight()/1024; 
    } 

    @Override 
    public Bitmap getBitmap(String url) { 
     return get(url); 
    } 

    @Override 
    public void putBitmap(String url, Bitmap bitmap) { 
     put(url, bitmap); 
    } 
} 

activity_upcoming_jour ney.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:descendantFocusability="blocksDescendants" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="true" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.mssinfotech.dropjourney.UpcomingJourney"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/headBars" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar_actionbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 
    <ListView 
     android:layout_below="@+id/headBars" 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:divider="@null" /> 


    <LinearLayout 
     android:visibility="invisible" 
     android:id="@+id/errorImage" 
     android:background="#fff" 
     android:orientation="vertical" 
     android:textAlignment="center" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:background="@drawable/ic_mood_bad_black_24dp" 
      android:layout_width="160dp" 
      android:layout_height="160dp" /> 
     <TextView 
      android:textColor="#ccc" 
      android:textAlignment="center" 
      android:text="No Ride found" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:textSize="30dp" 
      android:textAlignment="center" 
      android:textColor="#f00" 
      android:text="No Record Found" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</RelativeLayout> 

feed_item.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:background="@color/feed_bg" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <com.android.volley.toolbox.NetworkImageView 
       android:id="@+id/profilePic" 
       android:layout_width="@dimen/feed_item_profile_pic" 
       android:layout_height="@dimen/feed_item_profile_pic" 
       android:scaleType="fitCenter" > 
      </com.android.volley.toolbox.NetworkImageView> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textSize="@dimen/feed_item_profile_name" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/timestamp" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textColor="@color/timestamp" 
        android:textSize="@dimen/feed_item_timestamp" /> 
      </LinearLayout> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/txtStatusMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:paddingTop="@dimen/feed_item_status_pad_top" /> 

     <TextView 
      android:id="@+id/txtUrl" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:linksClickable="true" 
      android:paddingBottom="10dp" 
      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:textColorLink="@color/link" /> 
     <com.mssinfotech.dropjourney.FeedImageView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:scaleType="fitXY" 
      android:visibility="visible" /> 
    </LinearLayout> 

</LinearLayout> 
+0

将listView.setOnItemSelectedListener更改为listView.setOnItemClickListener并尝试? – Raghavendra

+0

没有它的不工作你有任何其他suggetion http://prntscr.com/d704md – Mragank

+0

你可以发布你的更新代码? – Raghavendra

回答

0

这不是 “setOnItemSelectedListener”。改为使用listView.setOnItemClickListener(this)。

0

listView.setOnItemSelectedListener是喜欢使用微调列表中,以便为列表视图,你必须改为使用listView.setOnItemClickListener

0

的对于捕获点击事件ListView你应该使用onItemClickListener代替onItemSelectedListener。所以在你的代码中用setOnItemClickListener替换setOnItemSelectedListener

+0

它不工作时,我点击项目记录发生请确认我做了什么。我发布完整的代码 – Mragank

0

在ListView上使用setOnItemClickListener而不是setOnItemSelectedListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      Toast.makeText(getApplicationContext(),"You clicked"+position,Toast.LENGTH_SHORT).show(); 
     } 

    }); 
+0

它不工作,当我点击项目记录发生请确认我做了什么。我发布完整代码 – Mragank

+0

在Toast中使用'UpcomingJourney.this'而不是'getApplicationContext()'。 –

0

朋友感谢您的支持,但没有现在的作品 finaly我找到了解决方案

你应该定义所有的项目列表视图中的子对象(TextView的,ImageView的等):

android:clickable="false" 
android:focusable="false" 
android:focusableInTouchMode="false" 

而对于根项目的RelativeLayout /的LinearLayout等等,定义:

android:clickable="false" 
android:descendantFocusability="blocksDescendants" 
android:focusable="false" 
android:focusableInTouchMode="false" 

它现在有效