2015-06-04 123 views
0

在我的應用程序中,我有一個ListView列出一些textviews和按鈕,頂部也有一個ImageView,我已經在此之間添加了視差滾動視圖,現在我想在ListView中動態添加一些textviews和按鈕。我有這個代碼,但它不工作。我是一個初學者,所以我找不到實際的問題。任何人都可以幫助我,請原諒我的語言問題?如何在android中的ListView中動態添加textview和按鈕?

MainActivity.java

public class MainActivity extends Activity { 
    private int lastTop = 0; 
    //ImageView image; 
    ListView listView; 
    private static int RESULT_LOAD_IMAGE = 1; 
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView; 
    ArrayAdapter adapter; 
    ArrayList<String> items = new ArrayList<>(); 
    public void parallax(final View v) { 
     final Rect r = new Rect(); 
     v.getLocalVisibleRect(r); 

     if (lastTop != r.top) { 
      lastTop = r.top; 
      v.post(new Runnable() { 
       @Override 
       public void run() { 
        v.setY((float) (r.top/2.0)); 
       } 
      }); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.listView); 
     //Dynamic textvieww 
     final LinearLayout lm = (LinearLayout) findViewById(R.id.linear); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       AbsListView.LayoutParams.WRAP_CONTENT,  AbsListView.LayoutParams.WRAP_CONTENT); 

     //Create four 
     for(int j=0;j<=4;j++) 
     { 
      // Create LinearLayout 
      LinearLayout ll = new LinearLayout(this); 
      ll.setOrientation(LinearLayout.HORIZONTAL); 

      // Create TextView 
      TextView product = new TextView(this); 
      product.setText(" Product"+j+" "); 
      ll.addView(product); 

      // Create TextView 
      TextView price = new TextView(this); 
      price.setText(" $"+j+"  "); 
      ll.addView(price); 

      // Create Button 
      final Button btn = new Button(this); 
      // Give button an ID 
      btn.setId(j+1); 
      btn.setText("Add To Cart"); 
      // set the layoutParams on the button 
      btn.setLayoutParams(params); 

      final int index = j; 
      // Set click listener for button 
      btn.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

        Log.i("TAG", "index :" + index); 

        Toast.makeText(getApplicationContext(), 
          "Clicked Button Index :" + index, 
          Toast.LENGTH_LONG).show(); 

       } 
      }); 

      //Add button to LinearLayout 
      ll.addView(btn); 
      //Add button to LinearLayout defined in XML 
      lm.addView(ll); 
     } 
     //EOF Dynamic 
     View view = getLayoutInflater().inflate(R.layout.header, null, false); 
     //Image view block 
     Button buttonLoadImage = (Button) view.findViewById(R.id.buttonLoadPicture); 
     buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 


       Intent i = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     }); 
     this.imageView = (ImageView)view.findViewById(R.id.imgView); 
     Button photoButton = (Button) view.findViewById(R.id.button2); 
     photoButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } 
     }); 

     //imageview block end 
     // image = (ImageView) view.findViewById(R.id.image); 
     listView.addHeaderView(view); 

     adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); 
     listView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
     listView.setOnScrollListener(new AbsListView.OnScrollListener() { 
      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 
       parallax(imageView); 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
       parallax(imageView); 
      } 
     }); 
     } 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      ImageView imageView = (ImageView) findViewById(R.id.imgView); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     } 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      imageView.setImageBitmap(photo); 
     } 

    } 
    } 

activity_main.xml中

<FrameLayout xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="in.zoid.parallaxtutorial.MainActivity"> 
<LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<ListView 
    android:id="@+id/listView" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 
</LinearLayout> 
</FrameLayout> 

header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linear" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView android:id="@+id/imgView" 
    android:layout_width="wrap_content" 
    android:layout_height="200dip" 
    android:scaleType="centerCrop" ></ImageView> 
<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/black" > 
    <Button android:id="@+id/buttonLoadPicture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:text="Load Picture" 
     android:layout_gravity="center"></Button> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Take Picture" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
</LinearLayout> 
+0

把看你的ListItem的內部和動態創建按鈕,文本查看並添加到該視圖 – Hasnain

回答

相關問題