Android 如何在不充气的情况下获取id(ListView)
我对这个密码有疑问。代码在主活动中,ArtGeneralButton是一个填充特定listview的按钮。因此,有一个类负责更新(UpdateAnsList)。我使用一个名为StartUpdateAnsList的方法。然而,目前存在问题Android 如何在不充气的情况下获取id(ListView),android,class,android-listview,Android,Class,Android Listview,我对这个密码有疑问。代码在主活动中,ArtGeneralButton是一个填充特定listview的按钮。因此,有一个类负责更新(UpdateAnsList)。我使用一个名为StartUpdateAnsList的方法。然而,目前存在问题 listView = (ListView) findViewById(R.id.listView11); 我需要放大一个名为Selectivity的活动,因为listview11在这个活动(布局)中。我不想使用所有代码来充气。可以使用不同的东西吗 主要活动
listView = (ListView) findViewById(R.id.listView11);
我需要放大一个名为Selectivity的活动,因为listview11在这个活动(布局)中。我不想使用所有代码来充气。可以使用不同的东西吗
主要活动
...
public void ArtGeneralButton(View view){
selsub = view.getId();
UpdateAnsList myUpdate = new UpdateAnsList();
myUpdate.StartUpdateAnsList(this, selsub);
}
...
更新列表
...
public class UpdateAnsList {
/**
* @param args
*/
final static String ARG_POSITION_ANSWER = "position";
private String jsonResult;
private ListView listView;
private Context context;
public int selsub;
public UpdateAnsList(){
}
public void StartUpdateAnsList(Context c, int v){
listView = (ListView) findViewById(R.id.listView11);
context = c;
selsub = v;
selectItemAns(selsub);
accessWebService();
}
private void selectItemAns(int position) {
switch(position){
case R.id.button001:
\\action
break;
case 0:
\\action
break;
case 2:
\\action
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
//Toast.makeText(getApplicationContext(),
// "Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("employee name");
String number = jsonChildNode.optString("employee no");
String outPut = name + "-" + number;
employeeList.add(createEmployee("employees", outPut));
}
} catch (JSONException e) {
//Toast.makeText(getApplicationContext(), "Error" + e.toString(),
// Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(context, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "employees" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
//Toast.makeText(getApplication(), "c", Toast.LENGTH_SHORT).show();
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
。。。
公共类更新列表{
/**
*@param args
*/
最终静态字符串ARG\u POSITION\u ANSWER=“POSITION”;
私有字符串jsonResult;
私有列表视图列表视图;
私人语境;
公共国际秘书处;
公共更新列表(){
}
公共无效StartUpdateAnsList(上下文c,int v){
listView=(listView)findViewById(R.id.listView11);
上下文=c;
selsub=v;
selectItemAns(selsub);
accessWebService();
}
私有void selectItemAns(int位置){
开关(位置){
案例R.id.button001:
\\行动
打破
案例0:
\\行动
打破
案例2:
\\行动
打破
案例3:
打破
案例4:
打破
案例5:
打破
案例6:
打破
}
}
//访问web的异步任务
私有类JsonReadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(参数[0]);
试一试{
HttpResponse response=httpclient.execute(httppost);
jsonResult=inputStreamToString(
response.getEntity().getContent()).toString();
}
捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
私有StringBuilder inputStreamToString(InputStream为){
字符串rLine=“”;
StringBuilder answer=新建StringBuilder();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
试一试{
而((rLine=rd.readLine())!=null){
答:追加(rLine);
}
}
捕获(IOE异常){
//e.printStackTrace();
//Toast.makeText(getApplicationContext(),
//“错误…”+e.toString(),Toast.LENGTH_LONG).show();
}
返回答案;
}
@凌驾
受保护的void onPostExecute(字符串结果){
ListDrwaer();
}
}//结束异步任务
public void accessWebService(){
JsonReadTask=新建JsonReadTask();
//传递URL字符串数组的值
执行(新字符串[]{url});
}
//为列表视图生成哈希集
公共无效列表{
List employeeList=新建ArrayList();
试一试{
JSONObject jsonResponse=新的JSONObject(jsonResult);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“emp_info”);
for(int i=0;i
我解决问题只是为了使用:
listView = (ListView) this.activity.findViewById(R.id.listView11);
用这条线来解决问题,直到现在我都没有问题
:)