如何在没有膨胀

时间:2015-12-12 23:34:16

标签: android class android-listview

我对此代码有疑问。代码在Main Activity中,ArtGeneralButton是一个填充特定列表视图的按钮。因此,有一个负责更新的类(UpdateAnsList)。我使用一个名为StartUpdateAnsList的方法。但是

有问题
 listView = (ListView) findViewById(R.id.listView11);

我需要为名为SelectAtivity的Activity充气,因为listview11位于此Activity(布局)中。我不想使用所有代码来膨胀。可以使用不同的东西吗?

MainActivity

  ...
public void ArtGeneralButton(View view){
     selsub = view.getId();
     UpdateAnsList myUpdate = new UpdateAnsList();      
     myUpdate.StartUpdateAnsList(this, selsub);


}
...

UpdateAnsList

...

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;
    }



}

1 个答案:

答案 0 :(得分:0)

I solve the problem just to use :

listView = (ListView) this.activity.findViewById(R.id.listView11);

to use this in line to solve the problem, until now i do not have problem.

:)

相关问题