ListView with image from file

customlistviewss

MainActivity.java

package ru.devhead.flashcardsen_ru.app;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {

    String[] text = { "Afghanistan", "Algeria" };

    Bitmap bmp = BitmapFactory.decodeFile("/mnt/sdcard/cards/cat.png");
    Bitmap bmp1 = BitmapFactory.decodeFile("/mnt/sdcard/cards/dog.png");
    Bitmap[] image={bmp,bmp1};

    // int [] image = {  R.drawable.ic_launcher,R.drawable.ic_launcher};

    ListItemDetails item_details;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<ListItemDetails> result = GetSearchResults();
        ListView lv = (ListView)findViewById(R.id.listView);
        lv.setAdapter(new CustomListAdapter(result,getApplicationContext()));

    }
    private ArrayList<ListItemDetails> GetSearchResults() {
        // TODO Auto-generated method stub
        ArrayList<ListItemDetails> results = new ArrayList<ListItemDetails>();
        ImageView imageview = (ImageView) findViewById(R.id.imageView1);

        for(int i=0;i<text.length;i++)
        {
            item_details= new ListItemDetails();
            item_details.setName(text[i]);
            item_details.setImage(image[i]);
            results.add(item_details);
        }

        return results;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

 

CustomListAdapter.java

package ru.devhead.flashcardsen_ru.app;

        import java.util.ArrayList;
        import android.app.Activity;
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {

    private static ArrayList<ListItemDetails> itemDetailsrrayList;

    LayoutInflater layoutInflator;
    String[] countryName;
    int[] countryFlag;
    Context context;

    public CustomListAdapter(ArrayList<ListItemDetails> result, Context c) {
        // TODO Auto-generated constructor stub
        itemDetailsrrayList = result;
        context = c;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return itemDetailsrrayList.size();
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return itemDetailsrrayList.get(arg0);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        layoutInflator =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflator.inflate(R.layout.item_list, parent, false);

        TextView textview = (TextView) row.findViewById(R.id.textView1);
        ImageView imageview = (ImageView) row.findViewById(R.id.imageView1);

        textview.setText(itemDetailsrrayList.get(position).getName());
        imageview.setImageBitmap(itemDetailsrrayList.get(position).getImage());

        return (row);
    }
}

ListItemDetails.java

package ru.devhead.flashcardsen_ru.app;

import android.graphics.Bitmap;

public class ListItemDetails {

    private String name;
    private Bitmap image;

    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Bitmap getImage()
    {
        return image;
    }
    public void setImage(Bitmap images)
    {
        this.image = images;
    }
}

layout/activity_main.xml

<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="ru.devhead.flashcardsen_ru.app.MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

layout/item_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="vertical" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dip">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="8dip"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000" />

    </LinearLayout>

</LinearLayout>

Теги: , ,

Дата: 22 мая 2014

Автор: Аноним

Комментарии: 470

Комментарии: 470

  1. what is best for erectile dysfunction free coupons for groceries no registration male enlargement pills gnc buy viagra online usa india pharmacy how long does sildenafil 20 mg last

  2. interactions for cialis cialis side effects long term discount viagra funny cialis commercial low libido women

  3. what happens if a woman takes viagra cost of viagra 100mg cvs cialis 20mg for sale cialis free samples purple viagra pill generic daily use cialis vs viagra

  4. viagra cost per pill 2016 women's viagra samples walgreens viagra coupon discount cialis pills sildenafil pill identifier viagra trial samples best makeup for african american women

  5. viagra for women fda approved viagra vision symptoms top ten male enhancers cialis 2.5 mg taking cialis everyday best prescription ed pills maxcare patient assistance program

  6. fda adhd medication best supplement for women's libido correcting erectile dysfunction buy cialis cheap cialis prior authorization form sildenafil citrate tablets 100 mg

  7. medication samples for physician offices sildenafil generic price at cvs over counter ed pills walmart buy generic viagra generic cialis tadalafil 20mg herbal breast enhancement supplements

  8. viagra substitute at walmart can marijuana cause erectile dysfunction buy viagra online viagra 100mg price walmart buy little blue pill cialis 30 mg dosage

  9. herbal viagra green pill energy pills the golden pill buying viagra online top settlement class action 100 mg viagra reviews coupons for viagra at cvs

Оставить комментарий

*

Комментарий:

Ваш e-mail не будет опубликован.
Обязательные поля помечены *

Разрешенные HTML теги:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>