gpt4 book ai didi

android - 修改后的按钮在滚动 ListView 时重置

转载 作者:行者123 更新时间:2023-11-29 00:28:16 26 4
gpt4 key购买 nike

我正在使用 Json 数据创建 ListView 。我正在填充一些图像、文本字段和一个按钮。当用户单击按钮时,按钮标题会发生变化。

问题是如果按钮被修改并且用户滚动 ListView 按钮重置为第一个状态。请查看此视频以更好地解释:https://www.youtube.com/watch?v=57ZraP7ED2E&feature=youtu.be

适配器

public class FilmRequestAdapter extends ArrayAdapter<FilmRequest>{
Context context;
List<FilmRequest> data;

public FilmRequestAdapter(Context context, int resource, List<FilmRequest> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub

this.context=context;
this.data=objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;

if (row==null) {
LayoutInflater inflater=((Activity)context).getLayoutInflater();
row=inflater.inflate(R.layout.film_request_list,null);
}

((TextView)row.findViewById(R.id.FilmRequestFilmTitle)).setText(((FilmRequest)data.get(position)).title);
((TextView)row.findViewById(R.id.FilmRequestRating)).setText("Imdb Puanı : "+((FilmRequest)data.get(position)).rating);
((TextView)row.findViewById(R.id.FilmRequestOrder)).setText("Sıra : "+((FilmRequest)data.get(position)).order);
((TextView)row.findViewById(R.id.FilmRequestVoteCount)).setText("Oya adeti : "+((FilmRequest)data.get(position)).voteCount);

Button button=(Button)row.findViewById(R.id.FilmRequestActionButton);
String status=((FilmRequest)data.get(position)).status;
boolean requested=((FilmRequest)data.get(position)).requested;
boolean voted=((FilmRequest)data.get(position)).voted;
if (status.equalsIgnoreCase("A") || status.equalsIgnoreCase("")) {

if (requested) {
if (voted) {
button.setText("Oylanmış");
button.setEnabled(false);
}else {
button.setText("+ Oyla");
button.setEnabled(true);
button.setTag(R.string.film_request_action, "VOTEABLE");
button.setTag(R.string.film_request_id,((FilmRequest)data.get(position)).id);
}
} else {
button.setText("Filmi İste");
button.setEnabled(true);
button.setTag(R.string.film_request_action, "REQUESTABLE");
button.setTag(R.string.film_request_id,((FilmRequest)data.get(position)).imdbId);
}

} else {
button.setText("Eklenmiş");
button.setEnabled(false);
}

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FilmRequestActivity.ButtonClickFunction(context,(Button)v);
}
});

((ImageView)row.findViewById(R.id.FilmRequestPoster)).setImageBitmap(((FilmRequest)data.get(position)).bm);

return row;

}

}

异步类

public class FilmRequestAsync extends AsyncTask<String, Void, List<FilmRequest>>{

Context context;
ListView listView;
private ProgressDialog progressDialog;

public FilmRequestAsync (Context context) {
this.context=context;
this.listView=(ListView) ((Activity)context).findViewById(R.id.FilmRequestList);
}

protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(context, "Lütfen Bekleyin...", "Fimler Yükleniyor...",true);
}

@Override
protected List<FilmRequest> doInBackground(String... params) {
Log.d("debug","FilmRequestAsync param[0]"+params[0]);
List<FilmRequest> filmRequests=null;
String jsonString;

try {
jsonString=GeneralFunctions.getUrl(params[0]);
if (jsonString!=null) {

filmRequests=this.getFilmRequestListFromJsonString(jsonString);

}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return filmRequests;


}

private List<FilmRequest> getFilmRequestListFromJsonString(String jsonString){
List<FilmRequest> filmRequests=new ArrayList<FilmRequest>();
try {
Log.d("debug","Json Verisi oluşturuluyor.");
JSONArray json=new JSONArray(jsonString);
Log.d("debug","Json Verisi Oluşturuldu");

int i;
for (i=0;i<json.length();i++) {
JSONObject tmpJson=(JSONObject)json.get(i);
filmRequests.add(
new FilmRequest(
tmpJson.getString("id"),
tmpJson.getString("imdb_id"),
tmpJson.getString("poster"),
tmpJson.getString("title"),
tmpJson.getString("rating"),
tmpJson.getString("order"),
tmpJson.getString("status"),
tmpJson.getString("vote_count"),
tmpJson.getBoolean("requested"),
tmpJson.getBoolean("voted")
)
);
tmpJson=null;
}

} catch (JSONException e) {
Log.d("debug","Json Verisi HATASI:");

filmRequests=null;
e.printStackTrace();
}

return filmRequests;
}

@Override
protected void onPostExecute(List<FilmRequest> result) {
this.progressDialog.dismiss();
if (result==null) return;

// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d("debug", "FilmRequestAsync onPostExecute Başı");

FilmRequestAdapter filmRequestAdapter=new FilmRequestAdapter(context, R.layout.film_request_list, result);
Log.d("debug","FilmRequestAsync:Adapter set ediliyor.");
listView.setAdapter(filmRequestAdapter);
Log.d("debug", "onPostExecute Sonu");
}
}

列出 Xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/film_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="@drawable/film_list_style"
android:orientation="horizontal"
android:padding="3dp" >

<!-- ListRow Left sied Thumbnail image -->

<!-- Title Of Song-->

<!-- Artist Name -->

<!-- Rightend Duration -->

<ImageView
android:id="@+id/FilmRequestPoster"
android:layout_width="83dp"
android:layout_height="106dp" />

<Button
android:id="@+id/FilmRequestActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="+ Oyla" />

<TextView
android:id="@+id/FilmRequestRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/FilmRequestFilmTitle"
android:layout_below="@+id/FilmRequestFilmTitle"
android:text="Rating" />

<TextView
android:id="@+id/FilmRequestVoteCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/FilmRequestOrder"
android:layout_below="@+id/FilmRequestOrder"
android:text="VoteCount" />

<TextView
android:id="@+id/FilmRequestFilmTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/FilmRequestActionButton"
android:layout_toRightOf="@+id/FilmRequestPoster"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />

<TextView
android:id="@+id/FilmRequestOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/FilmRequestRating"
android:layout_toRightOf="@+id/FilmRequestPoster"
android:text="Order" />

</RelativeLayout>

点击函数

    public static void ButtonClickFunction(Context context, Button button){
//Burası Ne işlem yapabileceğimizi gösteriyor.
//REQUESTABLE veya VOTABLE değerleri alabilir.
String action=(String)button.getTag(R.string.film_request_action);
String id=(String)button.getTag(R.string.film_request_id);

Log.d("debug", "String action="+action);

if (action.equalsIgnoreCase("VOTEABLE")) {
VoteFilm(context, button,id);
} else if (action.equalsIgnoreCase("REQUESTABLE")) {
RequestFilm(context, button, id);
}
}

public static void VoteFilm(Context context, final Button button, final String id){
final String callUrl=context.getString(R.string.api_url)+"?request=vote_film&user_id=1&id="+id;
Log.d("debug", "FilmRequestActivity->VoteFilm->callUrl:"+callUrl);
new Thread(new Runnable() {
@Override
public void run() {
try {
GeneralFunctions.getUrl(callUrl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.post(new Runnable() {
@Override
public void run() {
button.setText("Oylanmış");
button.setEnabled(false);
}
});
}
}).start();
Toast.makeText(context, "Film Oylandı", Toast.LENGTH_SHORT).show();
}

public static void RequestFilm(Context context, final Button button,final String id){
final String callUrl=context.getString(R.string.api_url)+"?request=request_film&user_id=1&id="+id;
Log.d("debug", "FilmRequestActivity->RequestFilm->callUrl:"+callUrl);

new Thread(new Runnable() {

@Override
public void run() {
String jsonString;
final JSONObject jsonObject;
try {
jsonString = GeneralFunctions.getUrl(callUrl);
jsonObject=GeneralFunctions.StringToJSONObject(jsonString);
if (jsonObject!=null) {
button.post(new Runnable() {
@Override
public void run() {

try {
Log.d("debug","Inside Thread");
JSONObject data=(JSONObject) jsonObject.get("data");
String new_id=data.getString("id");
button.setTag(R.string.film_request_action,"VOTEABLE");
button.setTag(R.string.film_request_id,new_id);
button.setText("+Oyla");
button.setEnabled(true);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

}
}

最佳答案

问题是您在调用 ButtonClickFunction 时永远不会修改适配器的内容。这样,当您滚动时,会遇到保持状态,并且旧字符串会设置为您的按钮。我建议你更好地考虑你的设计。作为快速修复,您可以为 Button 设置另一个标签,在位置添加 FilmRequest 对象,例如:

    final FilmRequest filmRequest = (FilmRequest)data.get(position);
String status = filmRequest.status;
boolean requested = filmRequest.requested;
boolean voted = filmRequest.voted;

然后,在 ButtonClickFunction 中,检索此标记并相应地更改其 status

关于android - 修改后的按钮在滚动 ListView 时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17797757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com