如何清除listview

时间:2015-09-09 00:03:02

标签: android android-layout

我有一个ListView,它显示一个由用户在活动中动态生成的数组。基本上,该程序是一个计时器,用户可以选择他们希望闹钟响起的时间,并提供他们在闹钟响起时应该做什么的描述。

我使用SimpleAdapter进行ListView设置,该SimpleAdapter调用alarm_list_item.xml,其中包含两个TextView和一个我希望为列表中的每个项目显示的图标。

package com.example.sa_one.api;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class MainActivity extends Activity {
    public String str;
    String data2[] = new String[50];
    String data;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AsyncHttpClient client = new AsyncHttpClient();
                client.get("http://api.androidhive.info/contacts/", null, new api());

                CustomList adapter = new CustomList(getApplicationContext(),data2);
                ListView list = (ListView) findViewById(R.id.list);
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at " + data2, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);

    }

    class api extends AsyncHttpResponseHandler {

        @Override
        public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
            try {
                str = new String(bytes, "UTF-8");
                Log.e("responce ss", "" + str);


                try {
                    JSONObject jsonRootObject = new JSONObject(str);
                    JSONArray jsonArray = jsonRootObject.optJSONArray("contacts");
                    int lengthJsonArr = jsonArray.length();

                    for (i = 0; i < lengthJsonArr; i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        String id = jsonObject.optString("id");
                        String name = jsonObject.optString("name");
                        String email = jsonObject.optString("email");
                        String address = jsonObject.optString("address");
                        String gender = jsonObject.optString("gender");

                        data = "Node" + i + " : \n id= " + id + " \n Name= " + name + " \n Email= " + email + " \n address= " + address + "\n gender=" + gender + "\n\n";
                        Log.i("responce s1", "" + data);

                        data2[i] = data;


                        Log.i("responce s2", "" + Arrays.toString(data2));
                    }


                } catch (JSONException ignored) {

                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {

        }
    }
}

此代码给我错误 当用户选择不为警报添加自定义对话框时,我想将alarm_list_item.xml中TextView的可见性设置为GONE,因此它不会在列表中显示为空白区域。当我尝试从活动中设置此参数时,我得到一个NullPointerException,因为我假设xml文件实际上不是一个对象,但或多或​​少只是用作ListView的引用。有没有办法实现这个目标?

到目前为止,我通过将文本设置为“无自定义通知集”来解决问题,但我想要一个更干净的选项,只需在不使用时将所有行一起删除。

1 个答案:

答案 0 :(得分:0)

你可以通过findViewById()获取TextView。所以在你的onItemClick()中使用以下代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    TextView tv = (TextView)parent.findViewById(R.id.your_textview);
    tv.setVisibility(GONE);
    Toast.makeText(MainActivity.this, "You Clicked at " + data2,     
    Toast.LENGTH_SHORT).show();
}