В ListView при нажатии не загораются элементы

Интерфейс, диалоги, темы, стили, меню
Ответить
yarik7751
Сообщения: 21
Зарегистрирован: 25 сен 2014, 12:59

В ListView при нажатии не загораются элементы

Сообщение yarik7751 » 30 окт 2014, 12:03

Использую ViewBinder для ListView. При этом перестали выделятся синим элементы при нажатии.
Вот код:

Код: Выделить всё

package by.yarik.test9_player;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MainActivity extends Activity {
	
	final String LOG_TAG = "myLogs";
	final String ATTRIBUTE_NAME = "attribute_name";
	final String ATTRIBUTE_TIME = "attribute_time";
	final String ATTRIBUTE_LABEL = "attribute_label";
	
	static public String PREF = "mypref";
	
	ListView list;
	SharedPreferences sp;
	static SimpleAdapter adapter;
	
	public static ArrayList<File> files;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences(PREF, MODE_PRIVATE);
        list = (ListView) findViewById(R.id.list);
        
        
        
    	list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Editor edit = sp.edit();
				if (sp.getString("file", null) != null) {
					if (sp.getString("file", null).equalsIgnoreCase(files.get(position).getAbsolutePath())) {
						edit.putString("file", files.get(position).getAbsolutePath());
						edit.putString("name", files.get(position).getName());
						edit.putBoolean("isNewSound", false);
					} else {
						edit.putString("file", files.get(position).getAbsolutePath());
						edit.putString("name", files.get(position).getName());
						edit.putInt("position", 0);
						edit.putInt("numberSound", position);
						edit.putBoolean("isNewSound", true);
					}
				} else {
					edit.putString("file", files.get(position).getAbsolutePath());
					edit.putString("name", files.get(position).getName());
					edit.putInt("position", 0);
					edit.putInt("numberSound", position);
					edit.putBoolean("isNewSound", true);
				}
				edit.commit();
				startActivity(new Intent(getApplicationContext(), PlayActivity.class));
			}
    	});
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	File sd = Environment.getExternalStorageDirectory();
    	files = getAllFiles(sd, "mp3");
    	String music[] = new String[files.size()];
    	String time[] = new String[files.size()];
    	for (int i = 0; i < files.size(); i++) {
    		music[i] = files.get(i).getName();
    		MediaPlayer mp = new MediaPlayer();
    		try {
				mp.setDataSource(files.get(i).getAbsolutePath());
				mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
				mp.prepare();
				time[i] = PlayActivity.getTimeInText(mp.getDuration());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
    		
    	}
    	ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    	Map<String, Object> m;
    	for (int i = 0; i < files.size(); i++) {
    		m = new HashMap<String, Object>();
    		m.put(ATTRIBUTE_NAME, music[i]);
    		m.put(ATTRIBUTE_TIME, time[i]);
    		m.put(ATTRIBUTE_LABEL, i);
    		data.add(m);
    	}
    	String[] from = {ATTRIBUTE_NAME, ATTRIBUTE_TIME, ATTRIBUTE_LABEL};
    	int[] to = {R.id.tvName, R.id.tvTime, R.id.llFon};
    	
    	
    	adapter = new SimpleAdapter(this, data, R.layout.item, from, to);
    	adapter.setViewBinder(new BinderOfList());
    	list.setAdapter(adapter);
    }
    
	private static ArrayList<File> getAllFiles(File f, String extension) {
		ArrayList<File> names = new  ArrayList<File>();
		if (f.listFiles() != null) {
			for (File file : f.listFiles()) {
				if (file.isDirectory()) {
					names.addAll(getAllFiles(file, extension));
				}
				else {
					if (file.getName().endsWith("." + extension)) names.add(file);
				}
			}
		}
		return names;
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
        return super.onOptionsItemSelected(item);
    }
    
    class BinderOfList implements SimpleAdapter.ViewBinder {

		@Override
		public boolean setViewValue(View view, Object data,
				String textRepresentation) {
			switch (view.getId()) {
			case R.id.llFon:
				LinearLayout ll = (LinearLayout) findViewById(R.id.llFon);
				int i = ((Integer) data).intValue();
				if (sp.getInt("numberSound", -1) == i) {
					view.setBackgroundColor(Color.parseColor("#7fff00"));
				} else {
					view.setBackgroundColor(Color.parseColor("#3a3a3a"));
					//return false;
				}
				return true;
			}
			return false;
		}
    	
    }
}
Как это исправить?

Аватара пользователя
Leeroy
Сообщения: 67
Зарегистрирован: 12 дек 2013, 21:25

Re: В ListView при нажатии не загораются элементы

Сообщение Leeroy » 30 окт 2014, 21:53

может list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Java Core -> JDBC -> GoF -> Android SDK ->...
Телепрограмма в твоем смарте Телепрограмма

Ответить