0

我在填充我的列表视图时遇到问题。我以为我已经相应地修改了我的 XML 和适配器,但显然没有。加载活动时,会弹出对话框,说明它正在加载,然后应用程序强制关闭。如果我删除说明 listview.setText(name) 的 3 行编码,那么它只会在我的 XML 文件中加载所有带有默认文本的项目。我的代码如下所示:

public class DisplayServiceActivity extends ListActivity {
    private ListView listOfServices;

    //JSONArrays?
    JSONArray directory;


    //JSON Node names
    private static String TAG_ID = "id";
    private static String TAG_NAME= "name";
    private static String TAG_DIRECTORY = "Categories";
    private final static String url= "API LINK HERE";
    JSONObject json;
jsonParser jParser = new jsonParser();
ArrayList<HashMap<String, String>> directoryList;

@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service);

    directoryList = new ArrayList<HashMap<String, String>>();
    Request request = new Request();
    request.execute();

    listOfServices = getListView(); //get builtin listView




    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Home Service Categories");

    }
}// end of onCreate Method
@SuppressWarnings("unused")
public class Request extends AsyncTask<String, Void, JSONObject> {

    private static final int REGISTRATION_TIMEOUT = 3 * 1000;
    private static final int WAIT_TIMEOUT = 30 * 1000;
    private ProgressDialog dialog = 
            new ProgressDialog(DisplayServiceActivity.this);


    protected void onPreExecute() {
        dialog = new ProgressDialog(DisplayServiceActivity.this);
        dialog.setMessage("Getting your info real quick... Please wait...");
        dialog.show();
    }

    protected JSONObject doInBackground(String... params) {

        json = jParser.getJSONfromURL(url);

        return json;

    }

    protected void onPostExecute(JSONObject s) {          
        super.onPostExecute(s);
        String name = null;
        String id = null;
        dialog.dismiss();

        try {
            directory = s.getJSONArray("Categories");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i = 0; i< directory.length(); i++){;
        try {
            id = directory.getJSONObject(i).getString(TAG_ID);
            name = directory.getJSONObject(i).getString(TAG_NAME);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        displayCatList(id, name);

        }

    }

}

public void displayCatList(String id, String name){                 
    //create new HashMap
    HashMap<String,String> map = new HashMap<String, String>();

    //add each child node to HashMap key
    //map.put(TAG_ID, id);
    map.put(TAG_NAME, name);

    //adding HashList to ArrarList
    directoryList.add(map);



    // set Adapter for ListView here
    ListAdapter adapter = new SimpleAdapter(this,
            directoryList, 
            R.layout.list_item, 
            new String[] { name }, 
            new int[] { android.R.id.text1});



    LayoutInflater inflator = (LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View  v = inflator.inflate(R.layout.list_item, null);
    TextView listName = (TextView) v.findViewById(R.id.listName);
    listName.setText(name);

    setListAdapter(adapter);

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

List_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="rubble rubble rubble"
        android:textColor="#FFFFFF"
        android:padding="10dip"
        android:textSize="20dip"
        android:textStyle="bold" >
</TextView>

服务.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <TextView
        android:id="@+id/listName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:text="rubble rubble rubble"
        android:textColor="#FFFFFF"
        android:textSize="20dip"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>
4

4 回答 4

1

// 改变显示方式

      public void displayCatList(String id, String name){                 
      //create new HashMap
    HashMap<String,String> map = new HashMap<String, String>();

    //add each child node to HashMap key
    //map.put(TAG_ID, id);
    map.put(TAG_NAME, name);

    //adding HashList to ArrarList
    directoryList.add(map);

    MySimpleAdapter adapter = new MySimpleAdapter(this, R.layout.list_item, android.R.id.text1, directoryList);
    setListAdapter(adapter);

    adapter.notifyDataSetChanged();
}

// 在活动中添加以下类

  public class MySimpleAdapter extends ArrayAdapter<HashMap<String, String>> {

    List<HashMap<String, String>> listItems;

    public MySimpleAdapter(Context context, int resource,
            int textViewResourceId, List<HashMap<String, String>> objects) {
        super(context, resource, textViewResourceId, objects);
        listItems = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null) {
                  LayoutInflater inflator = (LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.list_item, null);
        }

          TextView listName = (TextView) convertView.findViewById(R.id.listName);

        listName.setText(listItems.get(position).get(TAG_NAME));
        return convertView;
    }

}
于 2013-05-13T03:57:12.103 回答
0

您将视图设置为 R.layout.service。但是在 displayCatList 中,您正在调用 findViewById 以获取 listitem.xml 中的视图。那是行不通的——它不在你的视野中。如果该文本视图应该在列表中,则需要在调用适配器的 getView 时进行设置。或者让 simpleAdaptor 为你做这件事。如果您不希望它成为该行的一部分,则不要将其添加到 listitem.xml

于 2013-05-13T03:54:21.803 回答
0

listName是空的,因为他 TextViewR.id.listName不在主布局中。要获取 TextView 的值,请执行以下操作:-

LayoutInflator li = getLayoutInflator();
View listItems = li.inflate(R.layout.list_item, null);
// set Adapter for ListView here
        ListAdapter adapter = new SimpleAdapter(this,
                directoryList, 
                listItems , 
                new String[] { id,name }, 
                new int[] { android.R.id.text1,android.R.id.text2 });


        TextView listName = (TextView) listItems .findViewById(R.id.listName);
        Log.e("XML Variable", name);
        listName.setText(name);

在这里我改变的是我R.layout.list_item在一个名为 listItems 的视图中膨胀,并用它来查找 TextView listName。

希望这可以帮助。

于 2013-05-13T04:02:52.787 回答
0

在您的代码中,您首先调用 asynck 任务,该任务处理所有内容,包括 listview 的 setAdapter ,然后再次调用 setAdapter 与 directoryList ,因为您在异步任务之后初始化为空白。最好的解决方案是初始化关于异步任务调用的目录列表。

 directoryList = new ArrayList<HashMap<String, String>>(); 
 Request request = new Request();
    request.execute();

然后你的整个代码试试这个。我认为它会解决你的问题......我希望它有效。

于 2013-05-13T04:16:56.953 回答