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

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

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

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

*

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

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

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

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