[英]android setvisibility for button in listview
I am parsing a json url inside a fragment and put the data into a listview. 我正在解析片段内的json网址,并将数据放入列表视图中。 For each element i have a hidden button with id button1 as you can see in my xml layout.
对于每个元素,我都有一个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>
Now, for every json element i have an entry called "Recordings" which has value Yes or No. If the value is Yes, it must set the button for the specific row visible. 现在,对于每个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);
.
.
.
But the app always crashes with this error: 但是应用程序始终会因以下错误而崩溃:
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)
And this is the code for the adapter: 这是适配器的代码:
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;
}
}
In Emi 在惠美
String recording;
public String getRecording() {
return recording;
}
public void setRecording(String recording) {
this.recording = recording;
}
In the for loop 在for循环中
if (obj.getString("Recordings").equals("Yes")) {
movie.setRecording("Yes");
}else{
movie.setRecording("No");
}
Remove this 删除这个
Button b = (Button) getView().findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
Then in adapter getView 然后在适配器getView中
Button b = (Button) convertView.findViewById(R.id.button1);
if(m.getRecording().equals("Yes"))
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.INVISIBLE);
Also you should use a ViewHolder pattern 另外,您应该使用ViewHolder模式
http://developer.android.com/training/improving-layouts/smooth-scrolling.html http://developer.android.com/training/improving-layouts/smooth-scrolling.html
I suppose in the xaml of the adapter you have also a button, i suggest you to initialize it in EmisiuniAdapter.java with 我想在适配器的xaml中也有一个按钮,建议您使用以下命令在EmisiuniAdapter.java中对其进行初始化
Button button = (Button) findViewById(R.Id.buttonName);
and than you can set the visibility property here: 然后您可以在此处设置可见性属性:
// I can only image the code, replace it with the correct name/method
if(!m.getRecordings){
//this getRecordings is the method that must give you the yes/no from object
button.setVisibility(View.GONE);
}
this should work, and you can remove the method you create to remove the visibility from page ;) 这应该可行,并且您可以删除创建的方法以从页面中删除可见性;)
What you need to do is hide/show your button in the adapter itself rather than in the loop. 您需要做的是在适配器本身而不是循环中隐藏/显示按钮。
In your bean class save the value which you got from JSON 在bean类中保存从JSON获得的值
movide.setRecordings(obj.getString("Recordings"));
and then in adapter put your logic something like, 然后在适配器中输入您的逻辑,
if (m.getString("Recordings").equals("Yes")) {
Button b = (Button) getView().findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
}
You can modify your logic accordingly. 您可以相应地修改逻辑。
try this ; 尝试这个 ;
button.setVisibility(View.GONE);//when you want to hide
button.setVisibility(View.VISIBILITY);//when you want to show
Step1) make the default visibility of button1 to invisible. 步骤1)将button1的默认可见性设置为不可见。
Step2) add one "Boolean mbutVisibility" field to class "Emi" . 步骤2)在类别“ Emi”中添加一个“ Boolean mbutVisibility”字段。
Step3) make true in your for loop(response one .you have mentioned above ) and save that data into arraylist 第三步:在您的for循环中设置为真(响应一个。您已经在上面提到过)并将该数据保存到arraylist中
Step4) pass data to adapter and check if the boolean value if true then make it visible else its already invisible. 步骤4)将数据传递给适配器,并检查布尔值是否为true,然后使其可见,否则它已经不可见。
i have myself did this .every thing working fine 我让自己做到了。一切正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.