0
我有一个ListView,显示用户从活动内动态生成的数组。基本上,该程序是一个计时器,用户可以选择一个他们想要报警发生的时间,并提供关于当该报警发生时应该做什么的描述。如何清除列表视图
我有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的的知名度,走了这么不显示为报警列表上的空白。当我尝试从活动中设置此参数时,我得到一个NullPointerException,因为我假设xml文件实际上不是一个对象,但或多或少只是用作ListView的引用。有没有办法做到这一点?
到目前为止,我通过将文本设置为“没有自定义通知集”来解决问题,但我希望有一个更清洁的选项,并在不使用时将它们全部删除。
如果您为正在使用的编程语言/工具添加标签,您可能会获得更多帮助。 – DuncanKinnear