gpt4 book ai didi

java - android 为 ListView 中的按钮设置可见性

转载 作者:行者123 更新时间:2023-11-30 01:56:02 28 4
gpt4 key购买 nike

我正在解析 fragment 中的 json url 并将数据放入 ListView 中。对于每个元素,我都有一个带有 id button1 的隐藏按钮,如您在我的 xml 布局中所见。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ler"
>

<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"/>

<!-- Movie Title -->
<TextView
android:id="@+id/title"
android:paddingTop="40dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/title"
android:textColor="@color/bastru"
android:textStyle="bold" />
<TextView
android:id="@+id/desc"
android:layout_below="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/thumbnail"

android:textSize="@dimen/desc"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vezi inregistrari"
android:layout_toRightOf="@+id/thumbnail"
android:layout_below="@+id/desc"
android:visibility="invisible"/>
<TextView
android:id="@+id/link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/title"
android:textStyle="bold"
android:visibility="gone"/>

<TextView
android:id="@+id/cand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/desc"
android:textColor="@color/verde"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
/>


</RelativeLayout>

现在,对于每个 json 元素,我都有一个名为“Recordings”的条目,其值为 Yes 或 No。如果值为 Yes,则必须将特定行的按钮设置为可见。

  for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Emi movie = new Emi();

if (obj.getString("Recordings").equals("Yes")) {
Button b = (Button) getView().findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
}
movie.setTitle(obj.getString("Titlu"));
movie.setDesc(obj.getString("Descriere"));
movie.setCand(obj.getString("Detalii"));
movie.setThumbnailUrl(obj.getString("PozaModerator"));
movie.setLink(obj.getString("iurl"));
// adding movie to movies array
movieList.add(movie);
.
.
.

但应用程序总是因以下错误而崩溃:

08-25 11:44:48.615  10248-10248/com.testapp.aacplay E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.testapp.aacplay.Emisiuni$2.onResponse(Emisiuni.java:105)
at com.testapp.aacplay.Emisiuni$2.onResponse(Emisiuni.java:88)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)

这是适配器的代码:

public class EmisiuniAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Emi> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public EmisiuniAdapter(Activity activity, List<Emi> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}


public int getCount() {
return movieItems.size();
}


public Object getItem(int location) {
return movieItems.get(location);
}

public long getItemId(int position) {
return position;
}


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.list_emisiuni, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView link = (TextView) convertView.findViewById(R.id.link);
TextView desc = (TextView) convertView.findViewById(R.id.desc);
TextView cand = (TextView) convertView.findViewById(R.id.cand);



// getting movie data for the row
Emi m = movieItems.get(position);

// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

// title
title.setText(m.getTitle());

link.setText(m.getLink());
desc.setText(m.getDesc());
cand.setText(m.getCand());
return convertView;
}

}

最佳答案

在惠美

String recording;
public String getRecording() {
return recording;
}

public void setRecording(String recording) {
this.recording = recording;
}

在for循环中

   if (obj.getString("Recordings").equals("Yes")) {
movie.setRecording("Yes");
}else{
movie.setRecording("No");
}

删除这个

  Button b = (Button) getView().findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);

然后在适配器getView中

  Button b = (Button) convertView.findViewById(R.id.button1);
if(m.getRecording().equals("Yes"))
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.INVISIBLE);

你还应该使用 ViewHolder 模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

关于java - android 为 ListView 中的按钮设置可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32199650/

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