[英]Android Listview in Baseadapter how can I save changes to the list
我一直在這里閱讀有關baseadapters的各種文章,並嘗試學習以便能夠解決我的問題,但我無法解決它。 在我的BaseAdapter上,我有一個名為post的字符串,該字符串用於列表視圖的一列中。 如果帖子長於13個字符,則當用戶單擊已縮短的帖子時,帖子會自動縮短,並顯示其全長,但是問題是,一旦您向下滾動列表視圖並返回到同一帖子,它仍會被縮短即使用戶之前單擊以顯示完整的帖子。 我認為這是Listview或Baseadapter回收或緩存機制的問題,無論如何我都可以解決此問題嗎? 此圖片將清除所有內容。此帖子超過13個字符,因此顯示了縮短的版本
如果用戶想完整閱讀它,那么他們將單擊“閱讀更多”,然后將顯示所有看起來像這樣的內容
當用戶向下或向上滾動時,相同的長帖子將返回到此位置而無需用戶再次單擊它,我想避免
我知道Listview會回收,但是如何更新呢? 這是我的下面的代碼
public class LocalFeed_CustomView extends BaseAdapter {
JSONObject names;
Context ctx;
Activity m;
// More is the default value
String result="More";
@Override
public int getCount() {
try {
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
return jaLocalstreams.length();
} catch (Exception e) {
Toast.makeText(ctx, "Error: Please try again", Toast.LENGTH_LONG).show();
return names.length();
}
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
MyViewHolder holder=null;
try {
if (row == null) {
LayoutInflater li = (LayoutInflater) m.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.customadapter, null);
holder = new MyViewHolder(row);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
}
final MyViewHolder finalHolder1=holder;
// Json data has been read
JSONArray jaLocalstreams = names.getJSONArray("localstreams");
final JSONObject jsonObject = jaLocalstreams.getJSONObject(position);
// if post length is more than 14 then shorten it
if (jsonObject.getString("post").length() > 14) {
holder.post.setText(jsonObject.getString("post").substring(0, 13) + "...Read More");
holder.post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
// if Result is More then show full post
if (result.equals("More")) {
finalHolder1.post.setText(jsonObject.getString("post") + "... Read Less");
result = "Less";
}
else
{
//Result is Less so shorten it again
finalHolder1.post.setText(jsonObject.getString("post").substring(0, 13) + "... Read More");
result = "More";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} else{
// This Post is already less than 14 characters so no Onclick here
holder.post.setText(jsonObject.getString("post"));
}
return row;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
class MyViewHolder{
TextView post;
MyViewHolder(View v)
{
post = (TextView)v.findViewById(R.id.post);
}
}
}
適配器表示在任何給定時刻的列表模型。
這對您意味着什么,如果用戶單擊一個TextView進行擴展以使視圖保持擴展狀態,則該擴展的TextView是狀態信息,必須在適配器中捕獲。
適配器應始終分為兩個階段:
onClick()
)將更新適配器中的狀態並調用notifyDataSetChanged()
。 getView()
使用當前狀態創建視圖。 因此,假設在適配器構造函數中,我們創建了一個標志數組
boolean expanded[] = new boolean[size];
其中size
是列表的長度。
然后,您可以執行以下操作:
// use the current state to create the view
String text;
if (expanded[position]) {
text = jsonObject.getString("post") + "... Read Less";
} else {
text = jsonObject.getString("post").substring(0, 13) + "...Read More";
}
holder.post.setText(text);
holder.post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// update the current state and request list refresh
expanded[position] = ! expanded[position]; // toggle
notifyDataSetChanged();
}
});
這段代碼與您的代碼不完全一樣,我只是想給您基本的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.