我在custom adapter
中有image
row
。点击image
我想显示title
row
图像是。的一部分。
我在interface
中实施了activity
,只要点击relative layout
,image
显示行textview
,就会显示relative layout
标题存在。这个activity_main.xml.
位于我的 listView.setOnItemSelectedListener(this);
问题:除了我的列表视图中的所有其他图片点击中仅显示第一行的标题外,我已经能够完成所有这些。
另外:我知道设置position
是最好的方法,因为这可以处理title
的{{1}},但我不能将其用作根据我的要求。有办法破解这个吗?
这样我的textview
会显示每种情况下不同行的标题吗?
MainActivity.java:
public class MainActivity extends ActionBarActivity implements FeedListAdapter.AdapterCallback {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
RelativeLayout error, player;
private List<FeedItem> feedItems;
private String URL_FEED = "http://10.0.3.2/main_feed_warship/main_feed.js";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
TextView mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title);
mToolBarTextView.setText("Home");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
player = (RelativeLayout) findViewById(R.id.player);
player.setVisibility(View.GONE);
ImageView cancel = (ImageView) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setVisibility(View.GONE);
}
});
listView = (ListView) findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(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(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);
}
}
/**
* Parsing json reponse and passing the data to feed view list adapter
*/
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
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.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();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onMethodCallback() {
//On Image click this relative layout is called
player.setVisibility(View.VISIBLE);
//And the title of the row with the image that is clicked is displayed
//Only the first row title is dispalyed across all image clicks
String name = ((TextView) findViewById(R.id.name))
.getText().toString();
TextView title=(TextView)findViewById(R.id.music_title);
title.setText(name);
}
}
FeedListAdapter.java:
public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private AdapterCallback mAdapterCallback;
private LayoutInflater inflater;
String title;
private List<FeedItem> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
try {
this.mAdapterCallback = ((AdapterCallback) activity);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}
}
@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);
FeedImageView feedImageView = (FeedImageView) convertView
.findViewById(R.id.feedImage1);
final FeedItem item = feedItems.get(position);
name.setText(item.getName());
String fontPath = "fonts/Lato-Light.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(activity.getAssets(), fontPath);
// Applying font
name.setTypeface(tf);
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(item.getTimeStamp()),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
timestamp.setText(timeAgo);
timestamp.setVisibility(View.GONE);
// Chcek for empty status message
if (!TextUtils.isEmpty(item.getStatus())) {
statusMsg.setText(item.getStatus());
statusMsg.setVisibility(View.GONE);
} 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.GONE);
} else {
// url is null, remove from the view
url.setVisibility(View.GONE);
}
// 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);
}
//The image in each row that is clicked to call the method below from my activity
ImageView play=(ImageView)convertView.findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mAdapterCallback.onMethodCallback();
} catch (ClassCastException exception) {
}
}
});
return convertView;
}
public static interface AdapterCallback {
void onMethodCallback();
}
}
答案 0 :(得分:0)
这是一个很长的镜头,但你尝试使用标签。它是这样的:
1-在适配器中,将ImageView标记设置为TextView(标题)id:
feedImageView.setTag(name.getId());
2-单击imageView时,您将获得标记(即TextView的ID)。
feedImageView.getTag();
3-现在,您对标题有正确的引用。
我不确定传递TextView ID是否足够,但我认为使用Tag是可行的方法。你只需要弄清楚你应该传递什么,以及你应该在哪里传递它。 FYI ,您可以在标记中传递标题(作为字符串)!