【问题标题】:display string data in listview as string value changes every yime在列表视图中显示字符串数据,因为字符串值每次都会更改
【发布时间】: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


【解决方案1】:

我认为 onCreate 中的这条线 int count = list.getCount(); 返回 0,因为到目前为止您还没有向 list 添加任何项目。

【讨论】:

  • 是的,我没有将项目添加到列表中,这是我的问题。每次如果我将字符串传递给 strArray,它只会显示该项目。所以最多列表将只包含一项。如何根据我传递的值制作字符串数组列表。
  • 检查stacktips.com/tutorials/android/…。如果您想使用服务和更新列表,那么您也应该使用接收器。
【解决方案2】:

如果您提供一段代码来查看它会很有帮助。 阅读您的问题,您似乎正在编写用于填充列表的数组。

只需从活动中获取字符串并将其添加到用于显示列表视图并调用 notifyDataSetChanged() 的数组中。它会工作

【讨论】:

  • 我也尝试调用 notifyDataSetChanged()。它没有用。如何在数组中添加字符串以显示列表视图,并且该数组应包含我每次输入新字符串时的所有值。
猜你喜欢
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2018-06-07
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多