我正在创建一个应用程序,我需要一个listview来显示任务列表。这是一个基于位置的提醒应用程序。但是在列表视图中,每当我将一个项目(任务)添加到列表中时,它就会出现在底部。 我的意思是,我希望将第一个项目添加到列表顶部,但它位于底部。那么,对此有何帮助?
这是列表的来源:
list:
first item
second item
这就是我想要的方式:
list:
first item
second item
..
以下是我设置适配器和列表视图的代码:
package com.example.fsdf;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
public class Second extends ActionBarActivity {
SQLiteDatabase mydatabase;
ListView lv;
int CheckedItem;
private Button button;
private Button button2;
double lat[] = new double[100];
double longt[] = new double[100];
ArrayAdapter <String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv = (ListView) findViewById(R.id.listView1);
mydatabase = openOrCreateDatabase("seproject", MODE_PRIVATE, null);
Cursor cursor = mydatabase.rawQuery("Select * from Store", null);
final ArrayList<String> list = new ArrayList<String>();
int i = 0;
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
String s= cursor.getString(0);
list.add(cursor.getString(0));
lat[i] = Double.parseDouble(cursor.getString(1));
longt[i++] = Double.parseDouble(cursor.getString(2));
} while (cursor.moveToNext());
mydatabase.close();
}
Collections.reverse(list); //Kept this here. It worked.
adapter = new marrayadapter(this,list);
lv.setAdapter(adapter);
lv.setItemChecked(0, true);
CheckedItem = 0;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedItem = (position);
}
});
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
mydatabase = openOrCreateDatabase("seproject", 0, null);
mydatabase.delete("Store", "Job" + "='" + list.get(CheckedItem) + "'", null);
Toast.makeText(Second.this, "Deleted", Toast.LENGTH_LONG).show();
mydatabase.close();
/* Intent i = new Intent(Second.this, Second.class);
startActivity(i);
finish(); Replaced with following*/
list.remove(CheckedItem);
adapter.notifyDataSetChanged();
}
catch (Exception e)
{
Toast.makeText(Second.this,"No tasks to delete.", Toast.LENGTH_SHORT).show();
}
}
});
button2 = (Button) findViewById(R.id.button1);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mydatabase = openOrCreateDatabase("seproject", 0, null);
mydatabase.delete("Store", "Job" + "='" + list.get(CheckedItem) + "'", null);
Intent i = new Intent(Second.this, Edit.class);
i.putExtra("job", list.get(CheckedItem));
i.putExtra("lat", lat[CheckedItem]);
i.putExtra("longt", longt[CheckedItem]);
startActivity(i);
mydatabase.close();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.menu_add:
Intent i = new Intent(Second.this, MainActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
/* if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
*/
}
}
自定义适配器代码(marrayadapter.java):
package com.example.fsdf;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class marrayadapter extends ArrayAdapter {
public marrayadapter(Context context, ArrayList list) {
super(context,R.layout.marrayadapter, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater amansinflater= LayoutInflater.from(getContext());
View customView = convertView; //changed this and added if condition
if(customView==null)
{
LayoutInflater amansinflater= LayoutInflater.from(getContext());
customView = amansinflater.inflate(R.layout.marrayadapter, parent, false);
} String singleTask=getItem(position).toString();
System.out.println("Here lies : " + singleTask);
TextView rowtext = (TextView)customView.findViewById(R.id.rowtext);
rowtext.setText(singleTask);
return customView;
}
}
答案 0 :(得分:1)
在设置适配器之前将Collections.reverse
功能移到list
上:
Collections.reverse(list);
adapter = new marrayadapter(this,list);
lv.setAdapter(adapter);
答案 1 :(得分:1)
您需要在第0个索引list.add(0, cursor.getString(0));
答案 2 :(得分:0)
int index = this.list.getFirstVisiblePosition() + list.size();
View v = this.list.getChildAt(list.getHeaderViewsCount());
int top = (v == null) ? 0 : v.getTop();
this.adapter.AddRangeToTop(comments);
this.adapter.notifyDataSetChanged();
this.list.setSelectionFromTop(index, top)
试试这个..