2013-02-13 82 views
0

長話短說,我試圖將下面代碼的上半部分中的可點擊的功能添加到下面的自定義適配器。多個可點擊區域(Thumb imageview + textview + listview row)w。 Base Adapter

我發現這個嘖嘖(在代碼的頂部源的修改版本低於目前沒有工作):

http://wiresareobsolete.com/wordpress/2011/08/clickable-zones-in-listview-items/

但是,我的整個項目採用了自定義適配器從這個嘖嘖(通過列表視圖,讓我點擊 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

所有我想做的是維持我用我現有的數據結構(ArrayList的W¯¯能力的哈希映射):在代碼的下方底部的定製適配器)。

*縮略圖imageview的 *粗體標題文本(圖像的TextView的在讀「有人喜歡你」 *列表行(除了ImageView的& TextView的所有區域)

我寧願保留我的自定義適配器從第二個鏈接,並添加從第一個鏈接的功能,但如果這使複雜的事情冷靜與建議任何事情,讓我插入我現有的數據集(Arraylist>),同時提供所述的功能。

import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MyActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener 
{ 

    // All static variables 
    // XML node keys 
    static final String KEY_FEED = "feed"; 
    // parent node 
    static final String KEY_UID_FK = "uid_fk"; 
    static final String KEY_FIRST_NAME = "first_name"; 
    static final String KEY_LAST_NAME = "last_name"; 
    static final String KEY_NAME = "name"; 
    static final String KEY_MESSAGE = "message"; 
    static final String KEY_CREATED = "created"; 
    static final String KEY_THUMB_URL = "thumb_img"; 
    private static final String TAG = "MyApp"; 
    ListView list; 
    LazyAdapter adapter; 
    JSONArray feed = null; 
    Button add; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     ListView list = new ListView(this); 
     setContentView(list); 



     ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String, String>>(); 

     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(URL); 

     try { 
     // Getting Array of Contacts 
     feed = json.getJSONArray(KEY_FEED); 

     // looping through All Contacts 
     for(int i = 0; i < feed.length(); i++){ 
      JSONObject c = feed.getJSONObject(i); 

      // Storing each json item in variable 
      String uid = c.getString(KEY_UID_FK); 
      String first_name = c.getString(KEY_FIRST_NAME); 
      String last_name = c.getString(KEY_LAST_NAME); 
      String name = first_name + last_name; 
      String http = "http://10.0.2.2/CI_BUHZ/IMGS/"; 
      String base_url = c.getString(KEY_THUMB_URL); 
      String thumb_url = http + base_url; 
      String message = c.getString(KEY_MESSAGE); 
      String created = c.getString(KEY_CREATED); 


      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(KEY_UID_FK, uid); 
      map.put(KEY_NAME, name); 
      map.put(KEY_MESSAGE, message); 
      map.put(KEY_CREATED, created); 
      map.put(KEY_THUMB_URL, thumb_url); 


      // adding HashList to ArrayList 
      feedList.add(map); 
     } 
     } catch (JSONException e) { 
     e.printStackTrace(); 
     } 

      Log.i(TAG, "I am logging something informational!"); 


     //Supply this adapter with either R.layout.row_button, R.layout.row_view, or R.layout.row_view_noparent 
     ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String,String>>(this, R.layout.row_view, 
feedList) { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View row = super.getView(position, convertView, parent); 

       View left = row.findViewById(R.id.left); 
       left.setTag(position); 
       left.setOnClickListener(MyActivity.this); 
       View text = row.findViewById(R.id.text); 
       text.setTag(position); 
       text.setOnClickListener(MyActivity.this); 

       return row; 
      } 
     }; 

     list.setAdapter(adapter); 
     list.setOnItemClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.left: 
      Toast.makeText(this, "Left Accessory "+v.getTag(), Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.text: 
      Toast.makeText(this, "text Accessory "+v.getTag(), Toast.LENGTH_SHORT).show(); 
      break; 
     default: 
      break; 
     } 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     Toast.makeText(this, "Item Click "+position, Toast.LENGTH_SHORT).show(); 
    } } 

//previously I was using this custom adapter that extends base adapter: 


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

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 





public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.list_row, null); 

     TextView name = (TextView)vi.findViewById(R.id.name); // title 
     TextView message = (TextView)vi.findViewById(R.id.message); // artist name 
     TextView created = (TextView)vi.findViewById(R.id.created); // duration 
     ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image 

     HashMap<String, String> update = new HashMap<String, String>(); 
     update = data.get(position); 

     // Setting all values in listview 
     name.setText(update.get("name")); 
     message.setText(update.get("message")); 
     created.setText(update.get("created")); 
     imageLoader.DisplayImage(update.get("thumb_url"), thumb_image); 

     return vi; 
    } 
} 

從本教程:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

+0

請提出您的問題。問題標題說*多個可點擊區域瓦特。 Base Adapter *和你說的問題*我試圖讓ListView接受hashmaps的ArrayList並在每個行內設置每個視圖。*。事實上,我在你寫的內容中看不到一個問題。 – Luksprog 2013-02-14 12:27:34

+0

我認爲我修正了/明確了。 – ChuckKelly 2013-02-14 16:20:30

回答

1

如果您設置行的部分爲可聚焦(機器人:可調焦= 「真」)(默認值)比OnItemClickListener的ListView控件不響應。

嘗試全部設置爲false

+0

我想要做的就是從此鏈接添加功能:http://wiresareobsolete.com/wordpress/2011/08/clickable-zones-in-listview-items/到第二個鏈接中的自定義適配器:http: //www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/。而已。 – ChuckKelly 2013-02-14 16:22:27

+0

Ruware的原創評論dint解決了我的問題,但他後來私下聯繫我並幫我解決了這個問題。所有功勞都歸於Ruware,但是我的問題+工作源代碼的解決方案詳見下面的答案。 – ChuckKelly 2013-02-14 18:14:12

2

Ruware是不夠好做了谷歌的Hangouts和我一起幫我解決我的問題。非常尷尬,我無法自己弄清楚這一點:

第1步。在我原來的描述中將鏈接2導入dl項目&將它導入到Eclipse中。

第2步。LazyAdapter類w。可點擊的ImageView &的TextView - 只需使用下面的代碼替換從鏈接2懶惰適配器在我的原始描述:

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

import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 





public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.list_row, null); 

     TextView name = (TextView)vi.findViewById(R.id.name); // title 
     TextView message = (TextView)vi.findViewById(R.id.message); // artist name 
     TextView created = (TextView)vi.findViewById(R.id.created); // duration 
     ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image 

     HashMap<String, String> update = new HashMap<String, String>(); 
     update = data.get(position); 

     // Setting all values in listview 
     name.setText(update.get("name")); 
     message.setText(update.get("message")); 
     name.setOnClickListener(new myOnClickListener(position)); 
     created.setText(update.get("created")); 
     imageLoader.DisplayImage(update.get("thumb_url"), thumb_image); 
     thumb_image.setOnClickListener(new myOnClickListener(position)); 

     return vi; 
    } 

    public class myOnClickListener implements OnClickListener{ 
     private int position; 
     public myOnClickListener(int position){ 
      this.position=position; 
     } 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      HashMap<String, String> update = new HashMap<String, String>(); 
      update = data.get(position); 
      Log.d("Testing Click", update.get("message")); 
     } 

    } 
} 

步驟3. list_row.xml - 更換list_row佈局W上。下面的XML:

機器人:layout_alignParentLeft = 「真」 機器人:layout_marginRight = 「5dip」> 機器人:layout_width = 「50dip」 機器人:layout_height = 「50dip」 機器人:SRC = 「@繪製/ default_thumb」/>

<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/thumbnail" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="Chuck Kelly" 
    android:textColor="#040404" 
    android:typeface="sans" 
    android:textSize="15dip" 
    android:textStyle="bold"/> 


<TextView 
    android:id="@+id/message" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/name" 
    android:textColor="#343434" 
    android:textSize="10dip" 
    android:layout_marginTop="1dip" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="This is a status Update" /> 


<TextView 
    android:id="@+id/created" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/message" 
    android:layout_below="@+id/message" 
    android:layout_marginRight="5dip" 
    android:text="5:45" 
    android:textColor="#10bcc9" 
    android:textSize="10dip" 
    android:textStyle="bold" /> 



<ImageView 
    android:id="@+id/close" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/closeicon" 
    android:visibility="invisible" /> 

您現在有一個列表視圖w。每行中的可點擊的視圖,也允許您設置每行中多個視圖的值,類似於許多流行的社交網絡(Facebook,Twitter,路徑)。再次感謝Ruware花時間幫助我。