2012-06-13 130 views
0

我已经创建了自定义列表视图,其中包含textview列表& Buttons.Now其显示Buttons列表,但它没有显示textview列表。我列出了来自SD卡内容。在自定义列表视图中没有显示TextView列表

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; 
     } 
    } 
} 

回答

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); 
     } 
    }); 
相关问题