Код: Выделить всё
import android.widget.AdapterView.OnItemClickListener
Код: Выделить всё
import android.widget.AdapterView.OnItemClickListener
Ну. да - может быть импорт не на тот слушатель.KamiSempai писал(а):Может быть Вы забыли указать слушателя в импорте?
Код: Выделить всё
import android.widget.AdapterView
Код: Выделить всё
AdapterView.OnItemClickListener spinnerListner = new AdapterView.OnItemClickListener() { ... };
Код: Выделить всё
public class MainActivity extends Activity {
private static final int CM_DELETE_ID = 1;
ArrayAdapter<CharSequence> adapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] strings = new String[]{"One", "Two", "Three"};
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, android.R.id.text1, strings);
ListView list = (ListView) this.findViewById(R.id.list);
list.setAdapter(adapter);
registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, CM_DELETE_ID, 0, "Удалить запись");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == CM_DELETE_ID) {
// получаем инфу о пункте списка
AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
// удаляем Map из коллекции, используя позицию пункта в списке
adapter.remove(adapter.getItem(acmi.position));
// уведомляем, что данные изменились
adapter.notifyDataSetChanged();
return true;
}
return super.onContextItemSelected(item);
}
}
А исходники на что?rezak90 писал(а): лучше писать кастомные адаптеры и всегда будешь знать где что не работает.
Код: Выделить всё
public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
init(context, textViewResourceId, 0, Arrays.asList(objects));
}
Из чего нетрудно догадаться почему адаптер не может модифицировать источник данных для нашего ListView.Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.
Код: Выделить всё
...
String[] strings = new String[]{"One", "Two", "Three"};
ArrayList<CharSequence> array= new ArrayList(Arrays.asList(itemList));
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array);
...
Огромное спасибо за наводку (http://www.ibm.com/developerworks/ru/ed ... tion9.html). Я и слыхом не слыхивал про родовые типы в Java когда знакомился с языком в 2003 году. Поэтому конструкции типа "AdapterView<?> parent" вызывали недоумение.damager82 писал(а):Возможно мало опыта работы с Java? Попробуйте что-нибудь прочесть из этой темы: viewtopic.php?f=17&t=147
Код: Выделить всё
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvMain = (ListView) findViewById(R.id.lvMain);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.names,
android.R.layout.simple_list_item_1);
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(LOG_TAG, "itemClick: position = " + position + ", id = " + id);
Intent intent = new Intent(MainActivity.this, ViewActivity.class);
intent.putExtra("position", position);
intent.putExtra("id", id);
startActivity(intent);
}
});
}
Код: Выделить всё
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
tvView = (TextView) findViewById(R.id.tvView);
Intent intent = getIntent();
String fName = intent.getStringExtra("position");
String lName = intent.getStringExtra("id");
tvView.setText("Your name is: " + fName + " " + lName);
}
Код: Выделить всё
intent.putExtra("position", Integer.toString(position));
intent.putExtra("id", Long.toString(id));
Хороший вопрос! По ходу ошибка копипаста ... Пофиксю, спасибо!xottabut писал(а):Скажите пожалуйста, зачем в донном уроке TextView ?
Код: Выделить всё
@Override
public void onResume() {
setContentView(R.layout.main);
filesList = (ListView) findViewById(R.id.filesList);
filesList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("myLogs", "itemClick: position = " + position + ", id = "
+ id);
}
});
String[] from = { "filename", "creationDate" , "id" };
int[] to = { R.id.tvFileName, R.id.tvDate , R.id.bDelete };
files = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(Main.this, files, R.layout.files_item2, from, to);
mfiles = new HashMap<String, Object>();
mfiles.put("filename", "dvsds");
mfiles.put("creationDate", "svdvsdv");
mfiles.put("id", 1);
mfiles.put("file_id", 2);
files.add(mfiles);
filesList.setAdapter(adapter);
super.onResume();
}
Код: Выделить всё
<?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="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#ff6600"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:text="date"
android:textColor="#000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/tvFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="FileName"
android:textColor="#000"
android:textSize="18sp" />
<Button
android:id="@+id/bDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="X" />
</LinearLayout>
</LinearLayout>