【发布时间】:2016-08-05 04:59:18
【问题描述】:
我的应用程序中有一项服务和一项活动。服务通过用户的操作获取一个字符串,并将该字符串传递给活动。在这里,我成功获取了活动的字符串值并显示在活动屏幕上。
我有一个活动列表视图,字符串值应该有序地显示在该列表中。我的问题是我只能在最近输入的列表视图中显示一项。先前输入的字符串不显示。
所以每次我必须从服务中获取字符串并且该字符串应该与以前的列表项一起显示在列表视图中。 请帮帮我..谢谢。
我的活动是...
public class MainActivity extends AppCompatActivity {
String pichi;
SharedPreferences pref=null;
ListView list;
private ArrayList<String> strArray;
int i;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.offlineList);
pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
pichi=pref.getString("KEY","default");
strArray=new ArrayList<String>();
if (strArray.contains(pichi)){
Toast.makeText(getApplicationContext(),"Link already exists",Toast.LENGTH_LONG).show();
}else {
int count = list.getCount();
for ( i = 0; i < count; i++) {
strArray.add("Row" + i);
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, strArray);
if (!list.equals(i)) {
strArray.add(pichi);
adapter.notifyDataSetChanged();
}
list.setAdapter(adapter);
}
}
public void startService(View view) {
startService(new Intent(MainActivity.this, CheckService.class));
}
}
其中 startService(View view) 是按下按钮的动作。
我的服务是……
public class CheckService extends Service {
public CheckService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
String a;
Intent dialogIntent;
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received Start id" + startId + ":" + intent);
final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
public void onPrimaryClipChanged() {
a = clipboard.getText().toString();
if (Patterns.WEB_URL.matcher(a).matches()){
dialogIntent = new Intent(CheckService.this, DialogActivity.class);
dialogIntent.putExtra("text",a);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
else if (!URLUtil.isValidUrl(a)){
// Toast.makeText(CheckService.this, "text is not a url link", Toast.LENGTH_LONG).show();
}
}
});
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(Intent intent, int startId) {
// Perform your long running operations here.
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
stopService(dialogIntent);
}
}
在服务中,我有一个警报对话框,我可以从中获取字符串值...
我的对话活动是.....
public class DialogActivity extends Activity{
AlertDialog alert;
String receivedText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle=getIntent().getExtras();
receivedText=bundle.getString("text","default");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("yoLobee")
.setMessage("save to yoLobee offline..?" )
.setIcon(R.drawable.logo)
.setCancelable(true)
.setNeutralButton("SHARE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, receivedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent,receivedText));
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
alert.dismiss();
alert.dismiss();
builder.setCancelable(true);
}
})
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(DialogActivity.this, "Link saved to yoLobee..", Toast.LENGTH_LONG).show();
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor edit = pref.edit();
edit.putString("KEY",receivedText);
edit.commit();
// edit.apply();
}
});
alert = builder.create();
alert.setCanceledOnTouchOutside(true);
setFinishOnTouchOutside(true);
alert.setCancelable(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alert.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
Window window = alert.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
alert.cancel();
alert.dismiss();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
super.onBackPressed();
alert.cancel();
alert.dismiss();
}
}
因此,我从对话框活动中获取字符串值并显示在列表中。 该列表仅显示服务中最近的字符串值,因为它一次获取一个值。我希望列表显示从服务中检索的字符串列表。
【问题讨论】:
-
请发布您的适配器代码...
-
我没有任何适配器代码...我使用数组适配器作为适配器在列表视图中显示项目。因为我猜适配器是在列表中显示数组数据,所以我需要数组数据正常工作。
-
我对此知之甚少,如果有任何需要更改的地方,请建议我。
标签: java android string listview arraylist