如何从ListView的项目点击事件动态移动到硬编码的Java布局?

时间:2012-02-23 21:05:39

标签: android layout android-listview

下面的代码在单击ListView的Item后显示一个对话框。我想创建自己的动态布局,显示有关ListView中特定List Entry的文本视图。有人可以帮忙吗?

package com.android.XYZ;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import android.widget.ScrollView;
import android.widget.LinearLayout;


public class News_01 extends ListActivity {
    private LayoutInflater mInflater;
    private Vector<RowData> data;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        data = new Vector<RowData>();
        RowData rd; 
        rd = new RowData("Detailed Report 1", "Story 1");
        data.add(rd);
        rd = new RowData("Detailed Report 2", "Story 2");
        data.add(rd);
        rd = new RowData("Detailed Report 3", "Story 3");
        data.add(rd);
        rd = new RowData("Detailed Report 4", "Story 4");
        data.add(rd);
        rd = new RowData("Detailed Report 5", "Story 5");
        data.add(rd);
        rd = new RowData("Detailed Report 6", "Story 6");
        data.add(rd);
        rd = new RowData("Detailed Report 7", "Story 7");
        data.add(rd);
        rd = new RowData("Detailed Report 8", "Story 8");
        data.add(rd);
        rd = new RowData("Detailed Report 9", "Story 9");
        data.add(rd);
        rd = new RowData("Detailed Report 10", "Story 10");
        data.add(rd);

        CustomAdapter adapter = new CustomAdapter(this, R.layout.news_01,R.id.text_1, data);
        setListAdapter(adapter);        
        getListView().setTextFilterEnabled(true);       
    }


    public void onListItemClick(ListView parent, View v, int position, long id) {
        CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
            RowData row = adapter.getItem(position);

新代码 - &gt; (这是我想在有人点击ListView上的任何项目后显示的内容)
                ScrollView sv = new ScrollView(this);

            sv.setBackgroundColor(Integer.parseInt("d3d3d3", 16));

            LinearLayout ll = new LinearLayout(this);

            ll.setOrientation(LinearLayout.VERTICAL);

            sv.addView(ll);

            TextView tv = new TextView(this);

            tv.setText(row.mTitle);

            ll.addView(sv);

我想在点击时添加硬编码动态布局 列表视图的项目?

原始代码---&gt;

        Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(row.mDescription); 

        builder.setMessage(row.mTitle + " -> " + position );

        builder.setPositiveButton("ok", null);

        builder.show();

        }

    /**
     * Data type used for custom adapter. Single item of the adapter.      
     */
    private class RowData {
        protected String mTitle;
            protected String mDescription;

            RowData(String title, String description){
                mTitle = title;
                mDescription = description;                                  
        }

            @Override
            public String toString() {
                    return mTitle + " " +  mDescription;
            }
    }

    private class CustomAdapter extends ArrayAdapter<RowData> {

            public CustomAdapter(Context context, int resource,
                            int textViewResourceId, List<RowData> objects) {
                    super(context, resource, textViewResourceId, objects);

            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                    ViewHolder holder = null;

                    //widgets displayed by each item in your list
                    TextView item = null;
                    TextView description = null;

                    //data from your adapter
                    RowData rowData= getItem(position);


                    //we want to reuse already constructed row views...
                    if(null == convertView){
                            convertView = mInflater.inflate(R.layout.news_01, null);
                            holder = new ViewHolder(convertView);
                            convertView.setTag(holder);
                    }
                    // 
                    holder = (ViewHolder) convertView.getTag();
                    item = holder.getItem();
                    item.setText(rowData.mTitle);

                    description = holder.getDescription();          
                    description.setText(rowData.mDescription);

                    return convertView;
            }
    }

    /**
     * Wrapper for row data.
     *
     */
    private class ViewHolder {      
        private View mRow;
        private TextView description = null;
        private TextView item = null;

            public ViewHolder(View row) {
            mRow = row;
            }

            public TextView getDescription() {
                    if(null == description){
                            description = (TextView) mRow.findViewById(R.id.text_1);
                    }
                    return description;
            }

            public TextView getItem() {
                    if(null == item){
                            item = (TextView) mRow.findViewById(R.id.text_1);
                    }
                    return item;
            }       
    }}


  [1]: http://i.stack.imgur.com/q6YIO.png
  [2]: http://i.stack.imgur.com/vC65C.png

1 个答案:

答案 0 :(得分:0)

我想你想要一个自定义对话框。试试this