在自定义列表视图中不显示TextViews列表

时间:2012-06-13 05:27:39

标签: android listview button textview

我创建了自定义列表视图,其中包含textview&列表。 Buttons.Now它显示Buttons列表,但它没有显示textview列表。我有sdcard内容的textviews列表。

    public class Downloadlist extends ListActivity {
        Bundle bundle=null;
        private List<String> item = null;
        private List<String> path = null;
        private String root="/sdcard/mydownloads";
        private TextView myPath;
        private OrderAdapter m_adapter;
        Button b1;
        ListView lv1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydownload);
        bundle = new Bundle();
        myPath = (TextView)findViewById(R.id.path);
        b1=(Button)findViewById(R.id.view);
        lv1=(ListView)findViewById(R.id.list);
        getDir(root);
    }
    private void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);
        item = new ArrayList<String>();
        path = new ArrayList<String>();
        File f = new File(dirPath);
        File[] files = f.listFiles();   
        if(!dirPath.equals(root))
        {
            item.add(root);
            path.add(root);
            item.add("../");
            path.add(f.getParent());
        }
        for(int i=0; i < files.length; i++)
        {
            File file = files[i];
            path.add(file.getPath());
            if(file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
        }
        Log.d("itemssssssss", item.toString());

        /*ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(this,R.id.rowtext, this.item);
        setListAdapter(fileList);*/
        this.m_adapter = new OrderAdapter(this,R.layout.rowmydownload, item);
        setListAdapter(this.m_adapter);     
    }
    @SuppressWarnings("rawtypes")
    private class OrderAdapter extends ArrayAdapter {

        private List<String> item;
        TextView tt=null;
        Button bt=null;
        ViewHolder holder = null;
        @SuppressWarnings("unchecked")
        public OrderAdapter(Context context, int textViewResourceId, List<String> items) {
            super(context, textViewResourceId, items);
            item = items;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.rowmydownload, null);
                holder = new ViewHolder();
            }
            if (item.get(position) != null) {

                tt = (TextView) v.findViewById(R.id.rowtext);
                Button bt = (Button) v.findViewById(R.id.view); 
                final String result=item.get(position);
                tt.setOnClickListener(new View.OnClickListener() {                              
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getBaseContext(), "text is clicked"+item.get(position).toString(), Toast.LENGTH_LONG).show();
                        tt.setText(result.toString());
                        bundle.putString("listresultfromsd", result);
                        Intent i1=new Intent(Downloadlist.this,ReadXml.class);
                        i1.putExtras(bundle);
                        startActivityForResult(i1,0);
                        Log.d("resulttttt",result);


                    }
                });
                bt.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Toast.makeText(getBaseContext(), "button clicked", Toast.LENGTH_LONG).show();
                        bundle.putString("listresultfromsd", result);
                        String strbundle=bundle.toString();
                        Log.d("bundleeeeeeeee",result);
                        if(strbundle.contains(".zip"))
                        {
                            Intent i2=new Intent(Downloadlist.this,LaunchZip.class);
                            i2.putExtras(bundle);
                            startActivityForResult(i2,0);
                        }
                        else
                        {
                            Intent i3=new Intent(Downloadlist.this,Launch.class);
                            i3.putExtras(bundle);
                            startActivityForResult(i3,0);
                        }
                    }
                });

            }
            return v;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

实际上,您忘了为TextView tt设置文字了,你在TextView的setOnClickListener中创建了文本,它永远不会执行,直到它显示在列表上然后你点击它。

刚刚放tt.setText(result.toString());tt.setOnClikListener之外的行..

现在尝试一下,

 tt = (TextView) v.findViewById(R.id.rowtext);
 Button bt = (Button) v.findViewById(R.id.view); 
 final String result=item.get(position);
 tt.setText(result.toString());
 tt.setOnClickListener(new View.OnClickListener() {                              
    @Override
     public void onClick(View v) {
       Toast.makeText(getBaseContext(), "text is clicked"+item.get(position).toString(), Toast.LENGTH_LONG).show();
        bundle.putString("listresultfromsd", result);
        Intent i1=new Intent(Downloadlist.this,ReadXml.class);
        i1.putExtras(bundle);
        startActivityForResult(i1,0);
        Log.d("resulttttt",result); 
       }
   });