长话短说我试图将下面代码上半部分的可点击功能添加到下面的自定义适配器。
我发现这个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-text/
我想做的就是通过列表视图保持我使用现有数据结构(ArrayList w.hash maps)的能力,这将允许我点击
*缩略图imageview *大胆的标题文字(在图像中的文字视图中写着“像你这样的人” *列表行(除了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 :(得分:2)
Ruware非常适合与我一起进行Google环聊,以帮助我解决问题。非常尴尬,我无法自己解决这个问题:
步骤1.从原始描述&amp;中的链接2导入项目dl。将其导入Eclipse。
步骤2.LazyAdapter类w。可点击的Imageview&amp; 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” &GT; 机器人:layout_width = “50dip” 机器人:layout_height = “50dip” 机器人:SRC = “@绘制/ default_thumb”/&GT;
<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" />
你现在有一个listview w。每行中的可点击视图也允许您设置每行中多个视图的值,类似于许多流行的社交网络(Facebook,Twitter,路径)再次感谢Ruware花时间帮助我。
答案 1 :(得分:1)
如果将行的部分设置为可聚焦(android:focusable =“true”)(默认),则ListView的OnItemClickListener不响应。
尝试将它们全部设为false