0

长话短说,我试图将下面代码上半部分的可点击功能添加到下面的自定义适配器中。

我发现了这个 tut(下面代码顶部的源代码的修改版本 - 当前不起作用):

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

但是,我的整个项目都使用了这个 tut 中的自定义适配器(下面代码底部的自定义适配器): http ://www.androidhive.info/2012/02/android-custom-listview-with-image-and-文本/

我想做的就是保持我通过列表视图使用现有数据结构(ArrayList w. hash maps)的能力,这将允许我点击

*缩略图图像视图 *粗体标题文本(在图像中它的文本视图读取“像你一样的人”*列表行(除了图像视图和文本视图之外的所有区域)

我宁愿将我的自定义适配器保留在第二个链接中,并仅添加第一个链接中的功能,但是如果这会使事情变得复杂,那么我会向任何允许我插入现有数据集 (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/

4

2 回答 2

2

Ruware 很乐意与我进行 Google Hangout 来帮助我解决问题。非常尴尬,我自己无法弄清楚这一点:

步骤 1. 从我的原始描述中的链接 2 导入 dl 项目并将其导入 Eclipse。

步骤 2. LazyAdapter 类 w。Clickable 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:

android:layout_alignParentLeft="true" android:layout_marginRight="5dip"> android:layout_width="50dip" android:layout_height="50dip" android:src="@drawable/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、path)再次感谢 Ruware 抽出时间来帮助我。

于 2013-02-14T18:10:34.337 回答
1

如果将行的部分设置为可聚焦 (android:focusable="true")(默认),则 ListView 的 OnItemClickListener 不会响应。

尝试将它们全部设置为 false

于 2013-02-14T10:19:34.827 回答