Merhaba,

Android içerisinde fragment içerisinde dinamik olarak satır ekleyebileceğim bir yapı kurmak isitiyorum ancak hata alıyorum.

Java Kodu

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.hakkimizda_iletisim, container, false);

     TableLayout stk = (TableLayout)view.findViewById(R.id.table_main);
     TableRow tbrow0 = new TableRow(getActivity());
     TextView tv0 = new TextView(getActivity());
     tv0.setText(" Sl.No ");
     tv0.setTextColor(Color.WHITE);
     tbrow0.addView(tv0);
     TextView tv1 = new TextView(getActivity());
     tv1.setText(" Product ");
     tv1.setTextColor(Color.WHITE);
     tbrow0.addView(tv1);
     stk.addView(tbrow0);

    return view;
}

XML Kısmı

<?xml version="1.0" encoding="utf-8"?>
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#3d455b" android:layout_alignparentleft="true">
    <HorizontalScrollView
        android:id="@+id/hscrll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/table_main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" >
            </TableLayout>
        </RelativeLayout>
    </HorizontalScrollView>
</scrollview>

Debug kısmından baktığımda stk.addView(tbrow0); kısmında hata ile karşılaşıldığını gördüm.

HATA alt text

soruldu: 31 Eki '14, 04:41

zidan650's gravatar image

zidan650
375465260
cevap kabul oranı: 77%


Dinamik bir yapı oluşturmak istiyorsan listview bu yapı için oldukça uygundur. Aşağıdaki örnek işini görür.

MainActivity.java

package com.listview.example;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    ListView list;
     String[] teams = {
                "Galatasaray",
                "Fenerbahçe",
                "Beşiktaş",
                "Trabzonspor",
                "Malatyaspor"
              } ;
     Integer[] imageId = {
                  R.drawable.gs,
                  R.drawable.fb,
                  R.drawable.bjk,
                  R.drawable.ts,
                  R.drawable.mlt,
              };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new ListViewAdapter(this, teams, imageId));
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, teams[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

    class ListViewAdapter extends ArrayAdapter<String>{
        private final Activity context;
        private final String[] teams;
        private final Integer[] imageId;

        public ListViewAdapter(Activity context, String[] teams, Integer[] imageId) {
            super(context, R.layout.custom_list, teams);
            this.context = context;
            this.teams = teams;
            this.imageId = imageId;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            ViewHolder holder;
            if(view == null){
                view = inflater.inflate(R.layout.custom_list, parent, false);
                holder = new ViewHolder();
                holder.tv = (TextView) view.findViewById(R.id.team_name);
                holder.img = (ImageView) view.findViewById(R.id.team_icon);
                view.setTag(holder);
            }
            else
                holder = (ViewHolder) view.getTag();

            holder.tv.setText(teams[position]);
            holder.img.setImageResource(imageId[position]);

            return view;
        }

    }
    class ViewHolder{
        TextView tv;
        ImageView img;
    }

}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

</LinearLayout>

custom_list.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="horizontal" >

    <ImageView
        android:id="@+id/team_icon"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:padding="2dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/team_name"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:gravity="center"
        android:layout_weight="5" />

</LinearLayout>
permanent link

cevaplandı: 31 Eki '14, 11:03

mc44's gravatar image

mc44
1.1k222732
cevap kabul oranı: 42%

Cevabınız
toggle preview

Bu soruyu takip et

E-Posta üzerinden:

Üyelik girişi yaptıktan sonra abonelik işlemlerini yapabilirsiniz

RSS üzerinden:

Cevaplar

Cevaplar ve Yorumlar

Yazı Formatlama

  • *italic* ya da _italic_
  • **bold** ya da __bold__
  • link:[text](http://url.com/ "başlık")
  • resim?![alt text](/path/img.jpg "başlık")
  • liste: 1. Foo 2. Bar
  • temel HTML etiketleri de kullanılabilir

Bu sorunun etiketleri:

×819
×16

Soruldu: 31 Eki '14, 04:41

Görüntüleme: 556 kez

Son güncelleme: 31 Eki '14, 11:03

powered by BitNami OSQA