Android Base Adapter无法写入文本视图

时间:2014-10-09 14:02:39

标签: android

我遇到问题,类DataListAdapter没有写入给它的文本视图,而是将文本视图留空。因此关注的活动是:

public class LoadData extends Activity
{
    ListView dataList;
    MachineDataAdapter adapter;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load_data);

        dataList = (ListView) findViewById(R.id.machineList);

        //Log.d("intent", getIntent().getStringExtra("json"));
        dataList.setAdapter(new DataListAdapter(getIntent().getStringExtra("json")));
        context = this;

    }

    public void backClicked(View view)
    {
        Intent mainMenu = new Intent(getApplicationContext(), MainMenu.class);
        startActivity(mainMenu);
    }

    public void launchScan(View view)
    {
        Intent scanLaunch = new Intent(getApplicationContext(), ScanTag.class);
        startActivity(scanLaunch);
    }

    class DataListAdapter extends BaseAdapter
    {
        private JSONObject json;
        private Iterator<String> keys;
        LayoutInflater inflater;

        public DataListAdapter(String jsonString)
        {
            try
            {
                json = new JSONObject(jsonString);
            } catch (JSONException e)
            {
                e.printStackTrace();
            }

            keys = json.keys();
            inflater = getLayoutInflater();

        }


        public int getCount() {
            return json.names().length();
        }

        public Object getItem(int arg0) {
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int i, View convertView, ViewGroup parent) {


            if(convertView == null)
                convertView = inflater.inflate(R.layout.machine_data_row, parent, false);

            TextView title = (TextView) convertView.findViewById(R.id.machineRowTitle);
            TextView data = (TextView) convertView.findViewById(R.id.machineRowData);

            if(keys.hasNext())
            {
                String key = keys.next();
                String value = null;
                try
                {
                    value = json.getString(key);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
                Log.d("json", key);
                title.setText(key);
                data.setText(value);
            }

            return (convertView);
        }
    }



}

这是完整性的machine_data_row.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:orientation="vertical" >

    <TextView
            android:id="@+id/machineRowTitle"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#0000FF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

    <TextView
            android:id="@+id/machineRowData"
            android:textColor="#5C002E"
            android:textSize="17sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />

</LinearLayout>

json工作正常,我使用Log.d成功打印每个密钥。任何帮助将不胜感激。

编辑:这是json数据:

{"a":"b","c":"d","e":"f"}

2 个答案:

答案 0 :(得分:1)

通常我喜欢在getView中执行类似的操作:

if (convertView == null) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          row = inflater.inflate(layoutResourceId, parent, false);
          //Make sure the textview exists in this xml
} else {
          row = convertView;
}
TextView titleLabel = (TextView) row.findViewById(R.id.titleText);

答案 1 :(得分:1)

我不知道为什么会这样,但是使用key = json.names().getString(i);获取密钥 解决了这个问题。