簡體   English   中英

我創建了一個動態的ListView-如何將其連接到XML布局文件?

[英]I have created a dynamic ListView - how do I connect it to my XML layout file?

大家好,謝謝您對此的關注。 我使用Android的開發人員指南創建了動態ListView。 當前,ListView活動似乎可以在屏幕上顯示自己,而無需XML文件或setContentView ...這是一個問題。

我想使用XML布局,所以我可以將其他視圖添加到屏幕上,而不是將整個活動專門用於顯示列表。 我創建了一個XML布局,其中包含一個空白的ListView等,我希望我的列表進入分配的空間...所以我的問題是:如何獲取ListActivity以使用布局XML文件?

    public class MainList extends ListActivity {
    static SharedPreferences statusSettings;
    String jsonString;
    static JSONObject json;
    static JSONArray arrayTest;
    static int bob = 3;



    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        try {
            JSONObject e = arrayTest.getJSONObject(position);
            Intent intent = new Intent();
            intent.putExtra("clientName", e.getString("proj_name"));
            intent.putExtra("clientAddress", e.getString("c_address"));
            intent.putExtra("clientComments", e.getString("comments"));
            intent.putExtra("clientOrder", e.getString("order"));
            intent.setClass(MainList.this, ClientDetails.class);
            MainList.this.startActivity(intent);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("JSON", "Problem creating object from array!");
            e.printStackTrace();
        }
    }

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;


        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

        }

        public int getCount() {
            return arrayTest.length();

        }

        public Object getItem(int position) {
            return position;
        }

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

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

            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.mainlist, null);

                holder = new ViewHolder();
                holder.textName = (TextView) convertView.findViewById(R.id.tvMainName);
                holder.textAddress = (TextView) convertView.findViewById(R.id.tvMainAddress);


                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }


            JSONObject e;
            try {
                e = arrayTest.getJSONObject(position);
                holder.textName.setText(e.getString("proj_name"));
                holder.textAddress.setText(e.getString("c_address"));   


                switch (statusSettings.getInt(e.getString("order"), 0)){
                    case 1:
                        convertView.setBackgroundColor(0x00000000);
                        break;
                    case 2:
                        if(bob == 3){
                            convertView.setBackgroundColor(0xFFFF6600);
                            bob = 5;
                        }
                        break;
                    case 3:
                        convertView.setBackgroundColor(0xFF00CC00);
                        break;
                    case 4:
                        convertView.setBackgroundColor(0xFFCC0000);
                        break;
                }



            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                Log.e("JSON", "Couldn't put one of JSON arrays into object");
                e1.printStackTrace();
            }



            return convertView;
        }

        static class ViewHolder {
            TextView textName;
            TextView textAddress;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        jsonString = getIntent().getExtras().getString("jsonString");
        try {
            json = new JSONObject(jsonString);
            arrayTest = json.getJSONArray("client_list");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("JSON", "Couldn't create the JSON Array");
            e.printStackTrace();
        }
        setListAdapter(new EfficientAdapter(this));

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bob = 3;

        statusSettings = getSharedPreferences("status", 0);



        setListAdapter(new EfficientAdapter(this));
    }


}

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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_weight="1">
    </ListView>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

事實是,他們無需使用任何XML布局即可創建它-我尚不完全了解該怎么做,但我認為它與LayoutInflater有關。 我試過了:convertView = mInflater.inflate(R.id.list,null);

而不是convertView = mInflater.inflate(R.layout.mainlist,null);

我放置了setContentView(R.layout.test); 在我的onCreate中...但是沒有用。 您提供的任何幫助將不勝感激,謝謝!

這很簡單。 我已經快速生成了一些代碼供您參考。

public class ListandtextActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final String [] str = {"ONE","TWO","THREE"};
        final ListView lv = (ListView) findViewById(R.id.listView1);
        final TextView tv = (TextView)findViewById(R.id.tv1);
        lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str));
        lv.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                tv.setText("You Clicked Something");
            }
        });
    }
}

在您的XML中

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

隨時詢問您是否有任何疑問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM