Turkcell | Geleceği Yazanlar

Soru & Cevap

extends Activity İken onListItemClick Özelliğini kullanmak?

17.12.2016 - 02:25

Arkadaşlar soruda da belirttiğim üzere extends'i ListActivity yapmadan bu şekildeyken onListItemClick özelliğini nasıl kullanabilirim?

1758 Görüntülenme

1 Cevap

aliosman küçükkaraaslan
18.12.2016 - 18:45

Örnek Projemi Kopyalıyorum. İşini gödeceğine inanıyorum

public class MainActivity extends AppCompatActivity {
    private  ListView list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView)findViewById(R.id.listViewHaberler);
        final haberlerAdapter adapter = new haberlerAdapter(this.getApplicationContext(), R.id.img, R.id.txt);
        for (int i = 0; i<adapter.gazeteler.length; i++)
        {
            adapter.add(new haberlerList(adapter.imageId[i], adapter.gazeteler[i]));
        }

        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                screenMessage(adapter.gazeteler[i]);
            }
        });

    }
    private void screenMessage(String message) {
       Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

//ADAPTER

 

public class haberlerAdapter extends ArrayAdapter<haberlerList> {

    private TextView chatText;
    Context context;
    private List<haberlerList> hList = new ArrayList<haberlerList>();

    String[] gazeteler = {
            "Hürriyet",
            "Posta",
            "Sözcü"
    } ;

    Integer[] imageId = {
            R.mipmap.hurriyet,
            R.mipmap.posta,
            R.mipmap.sozcu
    };


    public haberlerAdapter(Context context, int resource, int textViewResourceId)
    {
        super(context, resource, textViewResourceId);
        this.context = context;

    }

    @Override
    public int getCount() {
        return this.hList.size();
    }

    @Nullable
    @Override
    public haberlerList getItem(int position) {
        return this.hList.get(position);
    }

    @Override
    public void add(haberlerList object) {
        hList.add(object);
        super.add(object);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView= inflater.inflate(R.layout.listiconview, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(gazeteler[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;

    }

}

 

//List

public class haberlerList {

    public int img;
    public String name;

    public haberlerList(int img, String name)
    {
        super();
        this.img = img;
        this.name = name;
    }
}

 

//listconview.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:text="Haber Adi"
            android:elevation="0dp"
            android:gravity="left|center"
            android:textSize="24sp"
            android:textStyle="normal|bold"
            android:textColor="@android:color/black" />


    </TableRow>
</TableLayout>

 

 

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.aokdev.haberler.MainActivity"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewHaberler" />
</LinearLayout>

 

İşine yaradı ise sorunun cevabını doğru olarak işaretlemeyi unutmazsan sevinirim. İyi çalışmalar dilerim

  • Filtered HTML

    Plain text

    Basic HTML

  • Dikey Sekmeler